Checkbox disappears when trying to add in additional formatted columns
Checkbox disappears when trying to add in additional formatted columns
I'm trying to create a table with a checkbox in the first column using this code:
<script>
$.fn.dataTable.ext.buttons.view = {
text: 'View Data',
action: function ( e, dt, node, config ) {
alert( 'Button activated' );
}
};
$.fn.dataTable.ext.buttons.design = {
text: 'Design',
action: function ( e, dt, node, config ) {
alert( 'Button activated' );
}
};
$(document).ready(function() {
$('#results').DataTable( {
columnDefs: [ {
orderable: false,
className: 'select-checkbox'
targets: 0,
},
],
select: {
style: 'multi',
selector: 'td:first-child'
},
dom: 'Bfrtip',
buttons: [
'copy', 'csv', 'excel', 'view', 'design'
]
} );
} );
</script>
I would like to add a column rendering to the 14th and 16th column of my table and i thought I could do this in the columnDefs part:
columnDefs: [ {
orderable: false,
className: 'select-checkbox'
targets: 0,
},
{
targets: [14,16],
"render": function (data, type, row, meta) {
var pm_list = data.split(";")
var pmid;
var value = "";
for pmid in pm_list {
value += '<a href="https://pubmed.ncbi.nlm.nih.gov/'+ pmid +'">' + pmid + '</a>;'
}
return value;
}
}
],
But this not only didn't work, but my select checkboxes disappeared. The other I wanted to do was to be able to freeze the first six columns when scrolling to the right as my table has 16 columns.
Any help with this would be greatly appreciated!
Raj
This question has an accepted answers - jump to answer
Answers
Nothing obvious stands out in you code snippet. Do you get errors in your browser's console?
Take a look at the FixColumns Extension.
Kevin
Thanks for your quick response Kevin. I don't get any errors in the console. What happens is that the checkboxes disappear. It seems as though when adding another column def, it just overwrites it.
I copied the above code into a test case and found two syntax errors. Please see the test case with comments of the syntax error fixes:
http://live.datatables.net/lipiriya/1/edit
Kevin
Thanks Kevin! Much appreciated with your help. I notice that it just hyperlinks 0s and 1s for some reason and not the data itself. I've managed to incorporate what you did but it's not hyperlinking with the data that's there
If you are talking about in my example thats probably true since I don't have data with semicolons for
data.split(";")
to work. However if you are having problems in our environment please update the test case to reflect your data.Or you can do a bit of debugging to make sure pm_list, etc is what you expect it to be and right click on the link and inspect it to see the HTML being built. Datatables isn't going to influence the link.
Kevin
I figured out what happened. The for loop was outputting array indices instead of the elements. I fixed it and it works! Thank you so much!