Accessing data in a DataTable from a button click

2019-04-12 23:58发布

I can access the data row by the following simple method:

$('#received-body tr').click( function(){
    aData = received_table.fnGetData(this);
    selected_received_id = aData[0];
    alert( selected_received_id );
});

but I can't access them from the button called .received-update in one of the rows:

$('#received-body .received-update').click( function(){
    alert( 'update' ); // works
    aData = received_table.fnGetData( $(this).parents('tr')); // fails
    selected_received_id = aData[0];
    alert( 'update:' + selected_received_id );                      
});

Any help appreciated

5条回答
萌系小妹纸
2楼-- · 2019-04-13 00:02

You may need to use closest as parents() might be giving more then one rows. While closest get the first match up the DOM hierarchy.

aData = recieved_table.fnGetData($(this).closest('tr')); // fails
查看更多
乱世女痞
3楼-- · 2019-04-13 00:09

You can solve your problem by replacing

    aData = received_table.fnGetData($(this).parents('tr'));

with

    aData = received_table.fnGetData($(this).parents('tr')[0]);


This same syntax is also required for calls to received_table.fnGetPosition()

查看更多
相关推荐>>
4楼-- · 2019-04-13 00:14

Below piece of code will help,

var table = $('#tableID').DataTable();
var row_data;
$('#tableID tbody').on( 'click', 'button', function () {
    row_data = table.row( $(this).parents('tr') ).data();
    console.log(row_data); // you will get the row data
});
查看更多
The star\"
5楼-- · 2019-04-13 00:20
aData = recieved_table.fnGetData($(this).parent('tr')); 

Try .parent() instead of .parents() because .parents() get the parent of each element in the current set of matched elements. .parent() will only work if the tr is a direct children of the tr. In that scenario .closest() will be ideal as shown below.

aData = recieved_table.fnGetData($(this).closest('tr'));

If above didn't work out for you, try filter method to reduce the set of matched elements.

查看更多
仙女界的扛把子
6楼-- · 2019-04-13 00:29

@Stephen Brown, could you please provide the solution if you found how to get the value. I am having exactly the same problem. I have already tried all the ways in this post but no luck. I have the following code.

   "fnDrawCallback": function() {
        $(".select_row ").click(function(e){
            dataId = $(this).data("id");
            tr=$(this).closest("tr")[0];
            getSelected(tr, true);
        }); 
    }

and the function is (studentDataTable is the datatable)

    function getSelected(tr, tableLoaded){
        console.log($(tr));
        console.log(tr);            
        if (tableLoaded){
             data = studentDataTable.fnGetPosition($(tr));
        }
    }

The console.log print the following

  Object[tr.even]  and <tr class="even"> respectively

I tried $(tr) , tr and many other ways but it always throws the error TypeError: a.nodeName is undefined ...ta[d]!==m?c.aoData[d]._aData:null}return Y(c)};this.fnGetNodes=function(a){var b...

Any help is much appreciated

Edit
Got it working this way (not sure what I was doing wrong before)

      "aoColumnDefs": [
                 { "fnRender": function(oObj) {
                            return '<input  type="checkbox" onClick=getSelected(this);  data-id="' + oObj.aData.id + '" value="' + oObj.aData.id + '" />';
                        },
                "bSortable": false,
                "aTargets": [0]
                 },

and the function

    function getSelected(checkbox){ 
         console.log($(checkbox));
         if ($(checkbox).is(':checked')) {
              tr=$(checkbox).closest("tr")[0];
              console.log(tr);
              data = studentDataTable.fnGetData(tr);
              console.log(data['groupsList']);
            }
    }

Your Answer

By clicking "Post Your Answer", you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.

查看更多
登录 后发表回答
相关问题
查看全部
相关文章
查看全部
收藏的人(4)