JQuery datatables operations based on row index

I am trying to add some texts and string links to a datatable based on the row index.

this is my current part of initialization

<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 am trying to add the text β€œA” in the first line and β€œB” in other lines.

As you can see from the code above, I tried to do something similar in 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 each line gets A. I am doing something wrong, but I can’t understand that.

I will need the same row index that the next column defines to include the link (or not), but I think if I can solve it, the rest will be easy.

+5
source share
1 answer

There is no parameter in the meta rendering function that stores the row and column index. See the JQuery Datatables documentation for more information on rendering function options .

 function (data, type, row, meta) { if (meta.row == 0){ return "<label>A</label>";} else { return "<label>B</label>"; } } 

As indicated in the documentation, the meta parameter was added only in version 1.10.1

+8
source

Source: https://habr.com/ru/post/1246691/


All Articles