简体   繁体   English

了解具有响应数据表的选定行

[英]Know selected row with responsive datatables

I have a problem with responsive datatables . 我有一个敏感问题数据表 When I resize the window and button was hidden this code, used to know which row is selected, doesn't work: 当我调整窗口大小并隐藏按钮时,此代码(用来知道选择了哪一行)不起作用:

$('#usersTable tbody').on( 'click', 'button', function () {
        usernameSelected = (userTable.row( $(this).parents('tr') ).data().username);
    } );

Maybe the problem is on parents('tr'), but how can I get information when table was resized? 也许问题出在parents('tr')上,但是在调整表格大小后如何获取信息? I use the returned value to recognize the button of which row is clicked. 我使用返回的值来识别单击了哪一行的按钮。 My table use ajax call like this: 我的表使用像这样的ajax调用:

if ( ! $.fn.DataTable.isDataTable( '#licensesTable' ) ) {
    licenseTable = $('#licensesTable').DataTable({
        responsive: true,
        //disable order and search on column
        columnDefs: [
             {
                 targets: [4,5],
                 orderable: false,
                 searchable: false,
             }
         ],
         //fix problem with responsive table
         "autoWidth": false,
         "ajax": "table",
         "columns": [
             { "data": "user" },
             { "data": "startDate",
                 "render": function (data) {
                     return (moment(data).format("DD/MM/YYYY"));                     
                 }
             },
             { "data": "endDate",
                 "render": function (data) {
                     return (moment(data).format("DD/MM/YYYY"));                     
                 }
             },
             { "data": "counter" },
             { data:null, render: function ( data, type, row ) {
                 return '<button type="button" class="btn btn-primary" id="updadteLicense" data-toggle="modal"'
                 +'data-target="#updateLicenseModal">Update</button>'

             }
             },
             { data:null, render: function ( data, type, row ) {
                 return '<button type="button" class="btn btn-danger" id="deleteLicense" data-toggle="modal"'
                 +'data-target="#deleteLicenseModal">Delete</button>'                                   
             }
             }
        ],
    });
}
else {
    licenseTable.ajax.url("table").load();
}

This is the HTML code relative datatable: 这是HTML代码的相对数据表:

<table id="licensesTable"
class="table table-bordered table-striped">
    <thead>
        <tr>
            <th>User</th>
            <th>Start date</th>
            <th>Expire date</th>
            <th>Max execution</th>
            <th>Delete</th>
            <th>Update</th>
        </tr>
    </thead>
</table>

I dont know if this is a good way, but i solved it by adding id to the button. 我不知道这是否是个好方法,但是我通过在按钮上添加id来解决它。

in Laravel 在Laravel

   ->addColumn('Datalist',function($loadData){
      return '<center><button id='.$loadData->rowID.' type="button" class="btn-showdata"><i class="fa fa-list"></i></button></center>'; 
   })

in javascript: 在javascript中:

$(this).on('click','#tableid .btn-showdata',function(){
  console.log(this.id);
});   

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM