I cannot get defaultContent to work
I cannot get defaultContent to work
I have pasted code from the last example on this page
https://datatables.net/reference/option/columns.defaultContent
into my page in the columns array to add a button to each row.
Loading my page with firebug enabled shows "typeError: i[n] is undefined from jquery. And the page does not render completely.
Removing the object from the columns array allows the page to render, but of course there are no buttons as desired.
JS snippet
$(document).ready(function () {
$('#collections').DataTable({
"columns": [
null,
null,
null,
{
"data": null,
"defaultContent":"<button>del</button>"
} ]
});
HTML (laravel blade template)
<table id='collections' class='display' cellspacing='0' width='100%' >
<thead>
<tr>
<th width='15%'>Collection ID</th>
<th>Value</th>
<th>Description</th>
</tr>
</thead>
<tbody>
@foreach($items as $item)
<tr>
<td>
{{$item['collectionsid']}}
</td>
<td>
{{$item['value']}}
</td>
<td>
{{$item['description']}}
</td>
<td></td>
</tr>
@endforeach
</tbody>
</table>
Any help is appreciated.
Bart
Answers
The issue is that the data for the column is already defined in your HTML. Could you just put
<button>del</button>
into the final cell in yourforeach
template loop?The other option would be to use
columns.render
, but the former option seems to be easier based on the above.Allan
Thanks for the reply Allan
I tried what you suggested, but still had some problems.
I reversed the logic and it dawned on me that the header columns did not match the columns array. Once I fixed that the Jquery errors stopped popping up and things began to work. Perhaps a console message could be added to inform dumb users like me that the columns count must match.?
Good point - I will look into adding something like that. Thanks for the suggestion.
Allan