2016-08-03 35 views
1

我想遍历Jquery Bootgrid表中的项目列表,并提取要在别处使用的值。这里是我的伪代码:迭代JQuery Bootgrid表中的行并提取值?

for (each row in or-table) { 
 
    var code = the value in data-column-id="code"; 
 
    var latitude = the value in data-column-id="lat"; 
 
    var longitude = the value in data-column-id="long"; 
 
    
 
    Console.log("code: " + code); 
 
    Console.log("latitude: " + latitude); 
 
    Console.log("longitude: " + longitude); 
 
}
<table id="or-table" class="table table-condensed table-hover table-striped" data-toggle="bootgrid"> 
 
    <thead> 
 
    <tr> 
 
     <th data-column-id="code" >Symbol Code</th> 
 
     <th data-column-id="lat" >Latitude</th> 
 
     <th data-column-id="long" >Longitude</th> 
 
    </tr> 
 
    </thead> 
 
    <tbody></tbody> 
 
</table>

我只是想通过表中的行中循环,并在细胞中的值保存到一个变量。我一直无法找到一个使用Bootgrid的例子。

回答

2

您可以遍历所有行并访问它所在的子元素。

$("#or-table tr").each(function(i, row){ 
     var code = $(":nth-child(1)", row).html(); 
     var latitude = $(":nth-child(2)", row).html(); 
     var longitude = $(":nth-child(3)", row).html(); 

     Console.log("code: " + code); 
     Console.log("latitude: " + latitude); 
     Console.log("longitude: " + longitude); 
    }); 

如果不是,添加类每种细胞类型像.code_value, .lat_value, .lng_value并访问他们each()$(row).find(".code_value").html()
或由PARAM名$(row).find("[data-column-id='code']").html()

+0

Bootgrid已经有能力选择行作为对象http://www.jquery-bootgrid.com/Documentation#properties - 请参阅下面的答案 - 您的解决方案取决于列的排列 - –

1

这个代码不承担th标签每套tr标签内的位置,顺序也没有排他性找到它们。

$("tr").each(function(row){ 
    var code = row.find("th[data-column-id='code']").text() 
    var latitude = row.find("th[data-column-id='lat']").text() 
    var longitude = row.find("th[data-column-id='long']").text() 

    Console.log("code: " + code); 
    Console.log("latitude: " + latitude); 
    Console.log("longitude: " + longitude); 
}); 
0

我想你是在寻找BootGrid的select方法。

http://www.jquery-bootgrid.com/Documentation#methods

var rows = $("#or-table").bootgrid("select");

+0

我很难找到一个如何使用select方法的例子。 –

+0

我认为我给出的例子会起作用。 Bootgrid文档说它返回一个jQuery对象。我从来没有使用过Bootgrid,所以我有点儿兴奋。上面的答案基本上做同样的事情,所以你可以使用其中的一个。 – Will

+0

那么我将如何访问这些值? console.log(“code:”+ rows。?); –

2

这里假设你的<td>元素具有data-column-id属性:

$('tbody tr').each(function(idx, row) { 
 
    var code = $(row).find('[data-column-id="code"]').html(); 
 
    var latitude = $(row).find('[data-column-id="lat"]').html(); 
 
    var longitude = $(row).find('[data-column-id="long"]').html(); 
 

 
    console.log("code: " + code); 
 
    console.log("latitude: " + latitude); 
 
    console.log("longitude: " + longitude); 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<table id="or-table" class="table table-condensed table-hover table-striped" data-toggle="bootgrid"> 
 
    <thead> 
 
    <tr> 
 
     <th data-column-id="code">Symbol Code</th> 
 
     <th data-column-id="lat">Latitude</th> 
 
     <th data-column-id="long">Longitude</th> 
 
    </tr> 
 
    </thead> 
 
    <tbody> 
 
    <tr> 
 
     <td data-column-id="code">1</td> 
 
     <td data-column-id="lat">2</td> 
 
     <td data-column-id="long">3</td> 
 
    </tr> 
 
    <tr> 
 
     <td data-column-id="code">4</td> 
 
     <td data-column-id="lat">5</td> 
 
     <td data-column-id="long">6</td> 
 
    </tr> 
 
    </tbody> 
 
</table>

+0

我厌倦了将这个改为“$('or-table tbody tr')。each(function(idx,row){”但它一直返回undefined。 –

+0

选择器应该是'#或 - table' –

+0

对不起, (函数(idx,row){“。我不知道为什么,因为$(”#或 - 表tr“)工作得很好,当使用$(“:nth-​​child(1)”,row).html();上面的方法而不是$(row).find('[data-column-id =“code”]')。html();。 –

1

即使您选择了一个答案,正确的方法塞莱CT使用jQuery Bootgrid库中的所有行是这样的(Fiddle):

// The Rows from The Table 
console.log(dt.data('.rs.jquery.bootgrid').rows) 

//With Ajax + Pagination 
console.log(dt.data('.rs.jquery.bootgrid').currentRows) 

的数据表:

<table id="employeeList" class="table table-bordered table-condensed table-hover"> 
    <thead> 
    <tr> 
     <th data-column-id="iEmployeeId" data-type="numeric" data-visible="false" data-identifier="true" data-noresize>Id</th> 
     <th data-column-id="sName" data-order="desc">Name</th> 
     <th data-column-id="sAddress">Address</th> 
    </tr> 
    </thead> 
    <tbody> 
    <tr> 
     <td>1</td> 
     <td>dsa</td> 
     <td>asd</td> 
    </tr> 
    <tr> 
     <td>2</td> 
     <td>sss</td> 
     <td>assd</td> 
    </tr> 

    <tr> 
     <td>3</td> 
     <td>asd</td> 
     <td>aaaaasd</td> 
    </tr> 

    <tr> 
     <td>4</td> 
     <td>asd</td> 
     <td>aaaaasd</td> 
    </tr> 

    <tr> 
     <td>5</td> 
     <td>asd</td> 
     <td>aaaaasd</td> 
    </tr> 

    <tr> 
     <td>6</td> 
     <td>asd</td> 
     <td>aaaaasd</td> 
    </tr> 
    <tr> 
     <td>7</td> 
     <td>asd</td> 
     <td>aaaaasd</td> 
    </tr> 
    <tr> 
     <td>8</td> 
     <td>asd</td> 
     <td>aaaaasd</td> 
    </tr> 
    <tr> 
     <td>9</td> 
     <td>asd</td> 
     <td>aaaaasd</td> 
    </tr> 
    <tr> 
     <td>10</td> 
     <td>asd</td> 
     <td>aaaaasd</td> 
    </tr> 

    <tr> 
     <td>11</td> 
     <td>asd</td> 
     <td>aaaaasd</td> 
    </tr> 

    </tbody> 
</table> 

然后初始化BootGrid对象:

var dt = $('#employeeList').bootgrid({ 
     selection: true, 
     rowSelect: true, 
     converters: {}, 
    }); 

然后访问行和Bootgrid DataTable Object

// the DT object 
console.log(dt.data('.rs.jquery.bootgrid')) 

// The Rows from The Table 
console.log(dt.data('.rs.jquery.bootgrid').rows) 

//With Ajax + Pagination 
console.log(dt.data('.rs.jquery.bootgrid').currentRows) 

var rows = dt.data('.rs.jquery.bootgrid').rows; 

for(var i = 0; i < rows.length; i++) 
{ 
    console.log(rows[i].iEmployeeId); 
    console.log(rows[i].sName); 
} 
+0

但是,我该如何从td获取值?这只是给了我 –

+0

我已经发布了答案,你可以使用for循环访问这些值,看看答案中的最后一部分 –