jQuery DataTables获取选定的行值 [英] jQuery DataTables Getting selected row values

查看:232
本文介绍了jQuery DataTables获取选定的行值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是jQuery datatable。我使用 http://www.datatables.net/examples/api /select_row.html
现在我想要获取所选的行值ids



脚本:



数据表();
$('#)$($)$($)示例tbody')on('click','tr',function(){
$(this).toggleClass('selected');
});

$('#button')。click(function(){
alert(table.rows('。selected')。data()。length +'row(s)selected');
} );
});

和HTML代码:

 < table id =exampleclass =displaycellspacing =0width =100%> 
< thead>
< tr>
< th> Id< / th>
< th> Position< / th>
< th> Office< / th>
< th>年龄< / th>
< th>开始日期< / th>
< th>工资< / th>
< / tr>
< / thead>

< tfoot>
< tr>
< th> Id< / th>
< th> Position< / th>
< th> Office< / th>
< th>年龄< / th>
< th>开始日期< / th>
< th>工资< / th>
< / tr>
< / tfoot>

< tbody>
< tr>
< td> 1< / td>
< td>系统架构师< / td>
< td>爱丁堡< / td>
< td> 61< / td>
< td> 2011/04/25< / td>
< td> $ 320,800< / td>
< / tr>
< tr>
< td> 2< / td>
< td>会计< / td>
< td>东京< / td>
< td> 63< / td>
< td> 2011/07/25< / td>
< td> $ 170,750< / td>
< / tr>
< / table>

现在我可以选择no.of.rows.Now我想要获取所选的行ID可以让任何人指导我实现它。

解决方案

您可以迭代行数据

  $('#button')。click(function(){
var ids = $ .map(table.rows('。selected' data(),function(item){
return item [0]
});
console.log(ids)
alert(table.rows('。selected' .data()。length +'row(s)selected');
});

演示:小提琴


I am using jQuery datatable.I done it using http://www.datatables.net/examples/api/select_row.html Now I want to get selected row values ids

Script:

 $(document).ready(function() {
 var table = $('#example').DataTable();
 $('#example tbody').on( 'click', 'tr', function () {
    $(this).toggleClass('selected');
} );

$('#button').click( function () {
    alert( table.rows('.selected').data().length +' row(s) selected' );
} );
} );

And HTML Code:

 <table id="example" class="display" cellspacing="0" width="100%">
    <thead>
        <tr>
            <th>Id</th>
            <th>Position</th>
            <th>Office</th>
            <th>Age</th>
            <th>Start date</th>
            <th>Salary</th>
        </tr>
    </thead>

    <tfoot>
        <tr>
            <th>Id</th>
            <th>Position</th>
            <th>Office</th>
            <th>Age</th>
            <th>Start date</th>
            <th>Salary</th>
        </tr>
    </tfoot>

    <tbody>
        <tr>
            <td>1</td>
            <td>System Architect</td>
            <td>Edinburgh</td>
            <td>61</td>
            <td>2011/04/25</td>
            <td>$320,800</td>
        </tr>
        <tr>
            <td>2</td>
            <td>Accountant</td>
            <td>Tokyo</td>
            <td>63</td>
            <td>2011/07/25</td>
            <td>$170,750</td>
        </tr>
         </table>

Now I am able to get selected no.of.rows.Now I want to get selected row Ids.Can anyone guide me to achieve it.

解决方案

You can iterate over the row data

$('#button').click(function () {
    var ids = $.map(table.rows('.selected').data(), function (item) {
        return item[0]
    });
    console.log(ids)
    alert(table.rows('.selected').data().length + ' row(s) selected');
});

Demo: Fiddle

这篇关于jQuery DataTables获取选定的行值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆