简体   繁体   English

排序更改列呈现选项上的数据表

[英]Datatable on sort change column rendering option

I am using jQuery DataTable for product categories table For that I have to show nested categories according to depth of the category node. 我在产品类别表中使用jQuery DataTable为此我必须根据类别节点的深度显示嵌套类别。

eg 例如

  • Parent1 Parent1
    • Child1 Child1
    • Child2 CHILD2
  • Parent2 Parent2
    • Child3 Child3
      • Child4 Child4
    • Child5 Child5

here I have two main categories Parent1 and Parent2 where these two also have child categories under them and also their childs under them. 在这里,我有两个主要类别Parent1和Parent2,其中这两个类别下面还有子类别以及它们下面的子节点。 to show category table like wordpress+woocommerce 显示类似wordpress + woocommerce的类别表

I have done something like this 我做过这样的事情

$("#category-listing-table").DataTable({
    processing: true,
    serverSide: true,
    "ajax": "/categories",
    "columns": [
        { "data": "id", orderable: false, searchable: false }, {
            "data": "name",
            "render": function(data, type, row) {
                var html = "";
                for (i = 1; i <= row.depth; i++) { html = html + "<strong>-</strong>&nbsp;"; }
                html = html + row.name;
                return [html].join('');
            }
        },
        { "data": "action", "orderable": false, "searchable": false }
    ],
})

now this is perfect, Category with depth 2 will show two '-' in column of name but when I sort category with name column I don't want to show category name with '-'. 现在这是完美的,深度为2的类别将在名称列中显示两个“ - ”,但是当我使用名称列对类别进行排序时,我不想使用“ - ”显示类别名称。

for that I have tried this code 为此,我尝试了这段代码

$("#category-listing-table").DataTable({
    processing: true,
    serverSide: true,
    "ajax": "/categories",
    "columns": [
        { "data": "id", orderable: false, searchable: false }, {
            "data": "name",
            "render": function(data, type, row) {
                var html = "";
                for (i = 1; i <= row.depth; i++) { html = html + "<strong>-</strong>&nbsp;"; }
                html = html + row.name;
                return [html].join('');
            }
        },
        { "data": "action", "orderable": false, "searchable": false }
    ],
}).on('order.dt', function() {
    var order = table.order();
    if (order[0][0] === 1) {
        console.log('I tried so much here but don\'t know what to do now');
    }
});

On sorting simply show category name with no '-' 在排序时只显示没有' - '的类别名称

So I came up with this patch solution 所以我提出了这个补丁解决方案

I have created two columns for name in table 我在表中为name创建了两列

  • One with tree format 一个用树格式
  • Another is simple category name (which is hide initially) 另一种是简单的类别名称(最初是隐藏)

When I sort name column I hide my formatted column and show column with basic name 当我对名称列进行排序时,我隐藏了我的格式化列并显示了基本名称列

var table = $("#category-listing-table").DataTable({
    processing: true,
    serverSide: true,
    "ajax": "/categories",
    "columns": [
        { "data": "id", orderable: false, searchable: false }, {
            "data": "name",
            "render": function(data, type, row) {
                var html = "";
                for (i = 1; i <= row.depth; i++) { html = html + "<strong>-</strong>&nbsp;"; }
                html = html + row.name;
                return [html].join('');
            }
        }, {
            "data": "name", "visible": false
        },
        { "data": "action", "orderable": false, "searchable": false }
    ],
}).on('order.dt', function() {
    /**
     * Column order information
     *
     * order[0][0] column id
     * order[0][1] column order type
     * @object
     */
    var order = table.order();

    // if the column is name
    if (order[0][0] === 1) {
        table.column(1).visible(false);
        table.column(2).visible(true).order(order[0][1]).draw();
    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM