3

I have initialised a simple Datatable:

//initialise table
var dataTable = $('#example').DataTable({
    searching: false,
    responsive: true

});
//hide unnecessary columns
dataTable.columns(1).visible(false);
dataTable.columns(2).visible(false);
dataTable.columns(3).visible(false);
dataTable.columns(4).visible(false);
dataTable.columns(5).visible(false);
dataTable.columns(6).visible(false);
dataTable.columns(7).visible(false);
dataTable.columns(8).visible(false);

It can contain any number of records but I would like to take the values from all of the columns (only 1 is displayed to the user) and insert them into input fields (which may or may not be visible). I have successfully been able to select the rows using:

$('#example tbody').on( 'click', 'tr', function () {

       if ( $(this).hasClass('selected') ) {
           $(this).removeClass('selected');
       }
       else {
           dataTable.$('tr.selected').removeClass('selected');
           $(this).addClass('selected');
       }

   });

I have been looking into the Datatables API, row(), cells() etc and whilst I can view the data() method I simply can't see how to extract data from EACH cell on the row into the input text fields on the same webpage. I have also looked at fnGetSelectedData but I didn't get far as it always returned undefined via the console.

To explain the use case, it's essentially an Address Lookup. Each column in the table represents part of the address, I want to take the cells from the selected row and insert it into the form as a users selected address.

Any help is appreciated

3
  • I don't get your question. rows().data() and row().data() are the right way to do this. They both return arrays ordered by column, and it should be easy to work with that, right? Commented Apr 1, 2016 at 16:03
  • So within the click function, I should be using row().data() to return the data? Essentially I want to be able to grab all of the cells on a selected row and insert it into a text field. I am a bit of a newbie and appreciate the help. Commented Apr 1, 2016 at 16:20
  • Yes, row().data() is how you do it. Gyrocode's answer more than covers it. Commented Apr 1, 2016 at 16:27

2 Answers 2

8

SOLUTION

Use the code below to get data for the selected row:

var data = $('#example').DataTable().row('.selected').data();

Then you can populate your input fields as shown below:

$('#name').val(data[0]); 
$('#email').val(data[1]); 

See this jsFiddle for demonstration.

NOTES

You can simplify your initialization code:

var dataTable = $('#example').DataTable({
    searching: false,
    responsive: true
    columnDefs: [
       {
          targets: [1,2,3,4,5,6,7,8],
          visible: false
       }
    ]
});
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for the notes, that has worked a treat and it is easier. I have also tried the solution but it reports "data" is undefined when I reference it like data[1]. Bit of a newbie so I appreciate the help. I was pointed to this API page but struggling to get it working: datatables.net/extensions/select/integration
@n34_panda, since you're not using Select extension the link doesn't apply to you. If you use Select extension, see this example. I also added a simple demo to demonstrate the code.
Yup, perfect - I don't know why I struggled 30 mins ago. I have got to a different PC and it worked first time. Really appreciate the detail, explanation and fiddle for the help.
2

To get an object with the values use:

yourTableVariable.rows({ selected: true }).data();

Then you can do something like this to get specific value(example id):

yourTableVariable.rows({ selected: true }).data()[0].id;

Comments

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.