I need to hide a column on the table, but after that I cannot read selected row's hidden column value.
dtTable = $('#lookupTable').DataTable({
"columnDefs": [
{
"targets": [ 0 ],
"visible": false,
"searchable": false
}
],
aaData: data,
aoColumns: cols,
paging: false,
scrollCollapse: true,
destroy: true
});
as you see the first column is hidden now. And I am trying to read the column value with that code
selectedIndex = $(this).find('td:eq(0)').text();
if i delete <"visible": false> from the code i can read the first column's value, but if it is hidden it gives me second column value.
I tired to change "witdh" property but it didnt work..
The CSS selector wont work, because
"visible": false
in yourcolumnDefs
does not mean that the column gets the equivalentdisplay: none;
style property in the markup.Instead, you'll have to use the DataTables API to get the data in the hidden column.
The function
fnGetData
will do the trick. It returns the text data in the cell that is passed as an argument to the function.Heres the example from the documentation
In your case, the column is hidden, thus you'll have to combine it with a second API call. Say that you click the row with the hidden first column, you can combine the
fnGetData
with thefnGetPosition
function.Check the documentation, it has some great examples.
fnGetData()
fnGetPosition()
This is the working code
Another Soultion Is Add Css Class ID Fiekd Visbilety hideen And $(this).find('td:eq(0)').text(); Retrive The value..
The correct answer is pretty old. So, if the correct answer does not work out for you. Please try this method :
This code will return the raw json object which was fetched for this row. something like :
To get the "fileID" you just need to :
`
Go through the dataTables API and you have multiple ways to retrieve data from hidden columns the correct way. For example you can use
cells
. As you see in the link you can use all kind of selectors withcells
, like a jQuery selector.Here a very simple example that logs out the values of the first column that has been hidden :
http://jsfiddle.net/oumtdd6k/
It cannot be emphasized enough : Always go through the API, do not try to use traditional jQuery on an initialised dataTable!!
In this case the reason is obvious : jQuery can only access elements that actually is in the DOM. When you hide columns in dataTables they are not hidden as in
display: none
, they are simply not rendered!