Closed ghost closed 9 years ago
I tomfumb, I'm sorry to only answer you now but I've been away for a while. I am sorry but I will not be able to fix the issue you are facing, simply because it would require a lot of modification for something quite unusual. You should not use HTML elements to store metadata about your rows: You could use the data
attribute of your th
elements to store such information, or even better use the datatable
plugin to internally store these informations and only display what you want.
I agree that the data attribute can be useful but in my scenario this was not possible. A server-side script was generating the HTML through a repeating row template and hidden table columns was the only way I had to communicate metadata.
Regardless of the reason I think it's sensible for javascript libraries to respect DOM display properties, or at least provide that as a non-default option, to adhere to the 'principle of least astonishment'. Thanks for your hard work providing Open Source tools to strangers!
Even if I think this is not useful anymore for you, I will put some useful stuff here (and close this issue).
After some tries, I didn't manage to easily solve this issue. Because of the ways filtering and sorting work, it would require a lot of works to « respect » hidden columns.
However, there is a very simple way to deal with hidden columns, but which required a few steps from you (or anyone using the plugins). Assuming you have 3 columns, and the second one is hidden, you could do the following (the important options are sort
, filters
and lineFormat
):
$('#mydatatable').datatable({
sort: {
0: true,
2: true
},
filters: {
0: true,
2: 'select'
},
lineFormat: function (id, data) {
var tr = $('<tr></tr>') ;
tr.append('<td>' + data[0] + '</td>') ;
tr.append('<td style="display: none">' +
tr.append('<td>' + data[2] + '</td>') ;
return tr ;
}
}) ;
The key here is to:
sort
and filters
options, so you can specify only the columns you want (0 and 2 here).lineFormat
to display hidden columns.
I am calling .datatable on an existing HTML table that has hidden columns. These columns provide metadata about each row that I use in JavaScript processing. Calling .datatable removes all classes and element-level styling so that apparently I have no way of keeping these columns hidden. The thead element does not have a <th> for the hidden columns and I have set nbColumns to the number of visible columns.