Jquery基于行索引的数据化操作

时间:2022-08-08 14:25:12

I'm trying to add some texts and links on rows on a datatable based on row index.

我尝试在基于行索引的datatable上添加一些文本和链接。

this is my datatable initialization part

这是我的datatable初始化部分。

 <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.

我尝试在第一行添加文本“A”,在其他行添加“B”。

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.

但是每一行都是a,我做错了什么,但我不明白是什么。

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.

我需要为下一列确定相同的行索引来包含链接(或不包含链接),但是我想如果我可以在这里解决它,那么rest就很容易了。

1 个解决方案

#1


8  

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.

您在呈现函数中丢失了另一个名为meta的参数,它存储行和列索引。有关呈现函数参数的更多信息,请参见JQuery Datatables文档。

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

如文档中所述,元参数仅在1.10.1版本中添加

#1


8  

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.

您在呈现函数中丢失了另一个名为meta的参数,它存储行和列索引。有关呈现函数参数的更多信息,请参见JQuery Datatables文档。

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

如文档中所述,元参数仅在1.10.1版本中添加