Accessing data in a DataTable from a button click

2019-04-12 23:42发布

问题:

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

回答1:

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()



回答2:

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
});


回答3:

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


回答4:

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.



回答5:

@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']);
            }
    }
share|improve this answer

Your Answer

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.

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 jquery datatables or ask your own question.

收藏的人(0)

Ta的文章 更多文章
登录 后发表评论
0条评论
还没有人评论过~