jQuery DataTables 获取选定的行值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29191760/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 16:34:16  来源:igfitidea点击:

jQuery DataTables Getting selected row values

jquerydatatables

提问by Aravinth Kumar

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

我正在使用 jQuery 数据表我使用http://www.datatables.net/examples/api/select_row.html完成了现在我想获取选定的行值 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:

和 HTML 代码:

 <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>0,800</td>
        </tr>
        <tr>
            <td>2</td>
            <td>Accountant</td>
            <td>Tokyo</td>
            <td>63</td>
            <td>2011/07/25</td>
            <td>0,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.

现在我可以选择 no.of.rows。现在我想获得选定的行 ID。谁能指导我实现它。

回答by Arun P Johny

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

演示:小提琴

回答by leole

More a comment than an answer - but I cannot add comments yet: Thanks for your help, the count was the easy part. Just for others that might come here. I hope that it will save you some time.

更多的是评论而不是答案 - 但我还不能添加评论:感谢您的帮助,计数是最简单的部分。只为其他可能来这里的人。我希望它能为你节省一些时间。

It took me a while to get the attributesfrom the rows and to understand how to access them from the data()Object (that the data() is an Array and the Attributes can be read by adding them with a dot and not with brackets:

我花了一些时间从行中获取属性并了解如何从data()对象访问它们(data() 是一个数组,可以通过添加点而不是括号来读取属性:

$('#button').click( function () {
    for (var i = 0; i < table.rows('.selected').data().length; i++) { 
       console.log( table.rows('.selected').data()[i].attributeNameFromYourself);
    }
} );

(by the way: I get the data for my table using AJAX and JSON)

(顺便说一句:我使用 AJAX 和 JSON 获取我的表的数据)

回答by Ileana Martínez González

var table = $('#myTableId').DataTable();
var a= [];
$.each(table.rows('.myClassName').data(), function() {
a.push(this["productId"]);
});

console.log(a[0]);