Reputation: 1199
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.
Upvotes: 8
Views: 10065
Reputation: 4014
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
Upvotes: 11