2017-07-17 174 views
0

我有一個表字段,我動態填充。創建一個對象數組Jquery

<table id="list" class="table table-striped ui-widget ui-widget-content"> 
<thead> 

    <tr class="ui-widget-header"> 
    <th>Id</th> 
    <th>Name</th> 
    <th>Type</th> 
    <th>Expression</th> 
    <th>Variable</th> 
    <th>Default</th> 
    <th>Date pattern</th> 
    <th>Readable</th> 
    <th>Writable</th> 
    <th>Required</th> 
    </tr> 
</thead> 
<tbody> 

</tbody> 

我與我的數據從對話框模式得到填充它。 這是我使用來填充該表

$("#list tbody").append("<tr>"+ 
      "<td>" + id.val() + "</td>" + 
      "<td>" + name_re.val() + "</td>" + 
      "<td>" + type.val() + "</td>" + 
      "<td>" + expression.val() + "</td>" + 
      "<td>" + variable.val() + "</td>" + 
      "<td>" + defaut.val() + "</td>" + 
      "<td>" + pattern.val() + "</td>" + 
      "<td>" + readable + "</td>" + 
      "<td>" + writable + "</td>" + 
      "<td>" + required + "</td>" + 
      "<td>" +"<button type='button' class='removebutton' title='Remove this propertie'>"+"</button>" + 
      "<button type='button' class='editbutton' title='Edit this propertie'>"+"</button>" + 
      "<button type='button' class='savebutton' title='save propertie changes'>"+"</button>" +"</td>"+ 
      "</tr>" 
      ); 

現在我創建的函數從表中獲取的數據,並將它們推入對象的數組,因爲每個對象表示單個行的從數據的功能桌子。 這是我使用來獲取數據的功能

function getPropertiesListData(){ 
var propertiesList=[]; 
var id, 
type, 
expression, 
variable, 
defaut, 
pattern, 
readable, 
writable, 
required, 
name; 

$("#list").find('tr').each(function(){ 
    var $tds=$(this).find('td'); 
    propertiesList.push({ 
      id:$tds.eq(0).text(); 
      name:$tds.eq(1).text(); 
      type:$tds.eq(2).text(); 
      expression:$tds.eq(3).text(); 
      variable:$tds.eq(4).text(); 
      defaut:$tds.eq(5).text(); 
      pattern:$tds.eq(6).text(); 
      readable:$tds.eq(7).text(); 
      writable:$tds.eq(8).text(); 
      required:$tds.eq(9).text(); 

    }); 
}); 
return propertiesList;} 

但是當我跑,我在導航控制檯

Uncaught SyntaxError: Unexpected token ; 

這個錯誤是關於該行的輸出這個錯誤:

id:$tds.eq(0).text(); 

你能幫我解決這個問題,或者告訴我我錯在哪兒,或者告訴我一種從表中獲取數據並將它們存儲到數組中的方法。

+1

使用','分隔對象屬性,而不是';'。以打字錯誤結束。 –

+1

在對象中用逗號','替換分號';'。 – Jer

+0

是的,取代了;與,解決了我的錯誤;謝謝 –

回答

1

更改您這樣的代碼:

propertiesList.push({ 
      id:$tds.eq(0).text(), 
      name:$tds.eq(1).text(), 
      type:$tds.eq(2).text(), 
      expression:$tds.eq(3).text(), 
      variable:$tds.eq(4).text(), 
      defaut:$tds.eq(5).text(), 
      pattern:$tds.eq(6).text(), 
      readable:$tds.eq(7).text(), 
      writable:$tds.eq(8).text(), 
      required:$tds.eq(9).text(), 

    }); 
0

請嘗試以下解決方案:

$("#list tbody").append("<tr>"+ 
 
      "<td>1</td>" + 
 
      "<td>test</td>" + 
 
      "<td>test-type</td>" + 
 
      "<td>test-expr</td>" + 
 
      "<td>test-val</td>" + 
 
      "<td>test-def</td>" + 
 
      "<td>test-patt</td>" + 
 
      "<td>test-r</td>" + 
 
      "<td>test-w</td>" + 
 
      "<td>test-req</td>" + 
 
      "<td>" +"<button type='button' class='removebutton' title='Remove this propertie'>"+"</button>" + 
 
      "<button type='button' class='editbutton' title='Edit this propertie'>"+"</button>" + 
 
      "<button type='button' class='savebutton' title='save propertie changes'>"+"</button>" +"</td>"+ 
 
      "</tr>" 
 
      ); 
 
function getPropertiesListData(){ 
 
var propertiesList=[]; 
 
var id, 
 
type, 
 
expression, 
 
variable, 
 
defaut, 
 
pattern, 
 
readable, 
 
writable, 
 
required, 
 
name; 
 

 
$("#list").find('tr').each(function(){ 
 
    var $tds=$(this).find('td'); 
 
    propertiesList.push({ 
 
      "id":$tds.eq(0).text(), 
 
      "name":$tds.eq(1).text(), 
 
      "type":$tds.eq(2).text(), 
 
      "expression":$tds.eq(3).text(), 
 
      "expression":$tds.eq(4).text(), 
 
      "defaut":$tds.eq(5).text(), 
 
      "pattern":$tds.eq(6).text(), 
 
      "readable":$tds.eq(7).text(), 
 
      "writable":$tds.eq(8).text(), 
 
      "required":$tds.eq(9).text(), 
 

 
    }); 
 
}); 
 
return propertiesList;}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<table id="list" class="table table-striped ui-widget ui-widget-content"> 
 
<thead> 
 

 
    <tr class="ui-widget-header"> 
 
    <th>Id</th> 
 
    <th>Name</th> 
 
    <th>Type</th> 
 
    <th>Expression</th> 
 
    <th>Variable</th> 
 
    <th>Default</th> 
 
    <th>Date pattern</th> 
 
    <th>Readable</th> 
 
    <th>Writable</th> 
 
    <th>Required</th> 
 
    </tr> 
 
</thead> 
 
<tbody> 
 

 
</tbody>

0

@Himanshu Upadhyay的解決方案的工作對我很好,我剛剛刪除的最後的逗號和那個作品。 工作的代碼是:

propertiesList.push({ 
      id:$tds.eq(0).text(), 
      name:$tds.eq(1).text(), 
      type:$tds.eq(2).text(), 
      expression:$tds.eq(3).text(), 
      variable:$tds.eq(4).text(), 
      defaut:$tds.eq(5).text(), 
      pattern:$tds.eq(6).text(), 
      readable:$tds.eq(7).text(), 
      writable:$tds.eq(8).text(), 
      required:$tds.eq(9).text() 

    });