在jquery Datatable中获取选定行/行的列值

hjzp0vay  于 2023-10-17  发布在  jQuery
关注(0)|答案(2)|浏览(123)

我只能跟着代码走。我想在一个数据表中获取选定行/行的列值。我已经使用了这个代码
DataTable代码:

var table = $('#tableId').DataTable({
        "ajax": {
            "url": "Content",
            "dataSrc": "",
            data: {sectionUrl: "", siteUrl: siteurl}
        },
        "columns": [
//            {"defaultContent": "<input type='checkbox' name='vehicle' id='checkID'>"},
            {"data": "postIMAGE", "render": function (data) {
                    return '<img src=' + data + ' width="154" height="115"/>';
                }},
            {"data": "postTITLE"},
            {"data": "postURL", "render": function (data) {
                    return '<a href=' + data + ' target="_blank"/>' + data + '</a>';
                }},
            {"data": "postSection"}

        ]
    });

.

$('#tableId tbody').on('click', 'tr', function () {
    $(this).toggleClass('selected');
});

$('#button').click(function () {

    var selectedRows = table.rows('.selected').data();
    var results = "";

    for (i = 0; i < selectedRows.length; i++) {
        alert();
    }
});

我想得到列的值

ruoxqz4g

ruoxqz4g1#

您可以从对象访问值,

$('#button').click(function () {

   var selectedRows = table.rows('.selected').data();

 //if you are getting array of objects inside main object
   alert(selectedRows[0].postTITLE);
   alert(selectedRows[0].postURL);

  // if you are getting just plain object you can access it as
    alert(selectedRows.postTITLE);
    alert(selectedRows.postURL);
});
8ftvxx2r

8ftvxx2r2#

您可以通过Datatable中的用户名访问服务器端呈现中的选定行

$('#button').click(function (){
   let rows = $('#tableElement').rows( { selected: true } ).data().map(x=>x.cols_name).toArray();
         console.log(rows);
  });

相关问题