We help IT Professionals succeed at work.

get cell value in dataTable

UniqueData
UniqueData asked
on
I am using datatable.net plugin.  i need to get the value of data that is in the dataset but not displayed in the grid (if that matters).  I have tried several things but can't get it to work.


        var tableTools = new $.fn.dataTable.TableTools( table, {
                  sRowSelect: "os",
                  aButtons: [
                  {
                      sExtends: 'select_single',
                      sButtonClass: 'marginLeft',
                      sButtonText: 'View History',
                      editor: editor,
                      fnClick: function () {
                        //window.location.href = 'grid_staffHistory.php';
                        //alert( 'Row index: '+table.row( '.selected' ).index() );
                        alert( 'primary key: '+table.row( '.selected' ).cell('usertable.uuk').data() );
                        //alert( 'Row index: '+ editor.get('usertable.uuk') );
                      }
                  },                  
                  {
                      sExtends: 'select_single',
                      sButtonClass: 'marginLeft',
                      sButtonText: 'Add New Staff',
                      fnClick: function () {
                        window.location.href = 'addUsers.php';
                      }
                  }                      
                  ]
              } );
        $( tableTools.fnContainer() ).insertBefore( '#table_filter' );          
    });
Comment
Watch Question

Scott FellDeveloper & EE Moderator
Fellow 2018
Most Valuable Expert 2013

Commented:
If you need to get data you are not placing in the table, you can put it in an attribute like
 <td data-extra="abc123">a Tiger Nixon</td>

Open in new window

I created a datatable example from the zero config for you http://jsbin.com/pacaguyuzi/1/edit?html,js,output

Note the first column on the first row has the data- attribute.
<tr>
                <td data-extra="abc123">a Tiger Nixon</td>
                <td>System Architect</td>
                <td>Edinburgh</td>
                <td>61</td>
                <td>2011/04/25</td>
                <td>$320,800</td>
            </tr>

Open in new window


I used the code below to get it
$(document).ready(function() {
    $('#example').DataTable();
     $('td').click(function(){
       var t=$(this).text();
       var e =$(this).attr('data-extra');
       alert(t);
       alert(e);
     }); 
      
} );

Open in new window

Give the sample a try and let me know if that is what you are trying to do
Developer & EE Moderator
Fellow 2018
Most Valuable Expert 2013
Commented:
fyi, the link above was fixed and you will need to click on the first column of the first row for "a tiger nixon"

Author

Commented:
your answer let me down the right path.  When I defined the editor I needed to include that field, but as hidden.  Then, one of the lines I had commented out would work.

        editor = new $.fn.dataTable.Editor( {
            serverSide: true,
            ajax: "lib/getdata.php",
            table: "#table",
            idSrc: "usertable.uuk",
            fields: [
              {
                name: "usertable.uuk", 
                visible: "false"
              },
            //rest of visible fields here

Open in new window


then...
        var tableTools = new $.fn.dataTable.TableTools( table, {
                  sRowSelect: "os",
                  aButtons: [
                  {
                      sExtends: 'select_single',
                      sButtonClass: 'marginLeft',
                      sButtonText: 'View History',
                      editor: editor,
                      fnClick: function () {
                        //window.location.href = 'grid_staffHistory.php';
                        alert( 'Row identifier: '+ editor.get('usertable.uuk') );
                      }
                  },  

Open in new window

Author

Commented:
Thank you for getting me on the right path :)