I'm trying to add some texts and links on rows on a datatable based on row index.
this is my datatable initialization part
<script>
var datatablecompanyuser = null;
$(document).ready(function () {
$.extend(true, $.fn.dataTable.defaults, {
"searching": false,
"ordering": false,
"paging": false
});
var dataSourceUrl = "@Url.Action("CompanyUsersList", "Settings")";
datatablecompanyuser = $('#datatablecompanyuser').dataTable({
info: false,
ajax: {
"url": dataSourceUrl,
"type": "GET",
},
columns: [
{
"data": "Id",
"render": function (data, type, row) {
return "<label><input type='checkbox' value='" + data + "' name='chkGrid'><span class='text'></span></label>";
}
},
{ "data": "Fullname" },
{ "data": "Email" },
{ "data": "CitizenIdNo" },
{
"data": "CreateDate",
"render": function (data, type, row) {
return moment(parseInt(data.substr(6))).format('DD.MM.YYYY hh:mm');
}
},
{
"data": "Id",
"render": function (data, type, row) {
var table = $('#datatablecompanyuser').DataTable();
if (table.row().index() == 0){
return "<label>A</label>"}
else {
return "<label>B</label>";
}
}
},
{ "data": "Id" },
],
language: {
"url": "//cdn.datatables.net/plug-ins/1.10.10/i18n/Turkish.json"
}
});
});
</script>
I'm trying to add text "A" on first row and "B" on other rows.
As seen on code piece above, I tried doing something like this on the corresponding column.
{
"data": "Id",
"render": function (data, type, row) {
var table = $('#datatablecompanyuser').DataTable();
if (table.row().index() == 0){
return "<label>A</label>";}
else {
return "<label>B</label>";
}
}
},
But every row gets A. I'm doing something wrong but can't understand what.
I will need the same row index determining for the next column to include a link (or not) but I guess if I can solve it here, rest will be easy.
Approach 2: Use $('table tr:last') jQuery Selector to find the last element of the table. The 'table' in query looks for the table element then 'tr' is looking for all the rows in the table element and ':last' is looking for the last table row of the table.
The maximum number of rows that a DataTable can store is 16,777,216.
Description. The row selector is a component that acts like a visual filter for datasets.
You are missing another parameter in the render function called meta
and this stores the row and column index. See the JQuery Datatables documentation for more info on the render function parameters.
function (data, type, row, meta) {
if (meta.row == 0){
return "<label>A</label>";}
else {
return "<label>B</label>";
}
}
As stated in the documentation the meta parameter was only added in version 1.10.1
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With