首页 > 解决方案 > jQuery数据表:排序后用行索引重写每一行的标题

问题描述

感谢 Andrew James,我能够将行的索引写入行的 title 属性。现在我有这段代码在每次排序后都会执行,但我正在寻找一种美化代码的方法

如果没有人可以,那么这可以作为其他人的解决方案,因为它有效

干杯

    var table = $(".table").DataTable(...);
    //jquery code to rewrite the row index number to each row title after sorting
    function writeRowIndex() {
        var rowIdx = 1;
        table.rows().every(function (rowIdx) {
            $(this.node()).attr('title', rowIdx);
            rowIdx++;
        });
    }
    //call the function on loading the page;
    writeRowIndex();
    //calling function after each sorting event
    var headers = table.columns().header().toArray();
    $(headers).on('click', function () {
        writeRowIndex();//call existing function
    });

标签: jquerydatatables

解决方案


推荐阅读