0

I have created link in datatable as:

$('#example').dataTable( {
    "columnDefs": [ {
        "targets": 1,
        "render": function ( data, type, full, meta ) {
            return '<a href="'+data+'" data-id="'+full.id+'">Download</a>';
         }
      } ]
} );

I can access id on clicking <a> tag using jQuery click event, but I have several fields to access. And I don't want to use data- attribute to access each field.

How can I access row object i.e.(full) , in jQuery event ?

What I have tried is:

"render": function ( data, type, full, meta ) {
    alert(full);
    return '<a href="'+data+'" data-full="'+full+'">Download</a>';
}

In jQuery event alert( $(this).data('full') ); I can see only [Object object].
I have tried to convert it to String but no success.

4
  • Instead of alert use console.log($(this).data('full')) Commented Mar 15, 2017 at 10:01
  • ok, I will try and let u know Commented Mar 15, 2017 at 10:04
  • @OffirPe'er still printing [object Object] Commented Mar 15, 2017 at 10:07
  • Its ajax call, will take time to create fiddle Commented Mar 15, 2017 at 10:09

2 Answers 2

1

Use row().data() API method to get data for any given row.

For example:

$('#example').on('click', 'tbody a', function(){
   var $tr = $(this).closest('tr');
   var data = table.row($tr).data();
   console.log(data);
});

See this example for code and demonstration.

Sign up to request clarification or add additional context in comments.

4 Comments

I already tried this it only gives me visible column values, but one of my requirement is to get hidden field also
@shantaram_t, It's not true, it will return the array/object that was received from the server including data fields that are not shown in the table.
then I will give another try , and let u know
Thanks it worked now, problem I tried to access it using array notation [ ]
0

Try jquery .data() to set / get data

$(your_component).data("full", full)

or

JSON.stringify(full) // convert json to string

9 Comments

I just want to get it also tried JSON.stringify( $(this).data(full) ) , but no success
To get data, you should use $(this).data('full'), the param is not an object. You can try alert(JSON.stringify($(this).data('full'))) to check your object is right. If you are using Chrome, developer tools for debugging is better.
Change return '<a href="'+data+'" data-full="'+full+'">Download</a>'; to return '<a href="'+data+'" data-full="'+JSON.stringify(full)+'">Download</a>';
This will do trick, but I have large data, only need to access 5-6 attributes, then It will be better if I use data- for these fields
JSON.stringify(full) will be long String, can't I pass it as an Obect ?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.