Jquery数据表呈现多个函数

时间:2017-12-01 11:14:52

标签: javascript jquery datatable datatables

我正在使用jQuery的datatables插件,但是我遇到了渲染函数的问题:

我有一个字符串,其中包含列中的一些html元素,我不希望html元素在表中呈现,因此我使用函数$.fn.dataTable.render.text()

我也只希望这些数据是预览,因为字符串可能非常长,因此我使用的是省略号函数$.fn.dataTable.render.ellipsis(40)

省略号函数定义为here

// https://datatables.net/manual/data/renderers#Text-helper
$.fn.dataTable.render.ellipsis = function (cutoff) {
    return function (data, type, row) {
        if (type === 'display') {
            var str = data.toString(); // cast numbers

            return str.length < cutoff ?
                str :
                str.substr(0, cutoff - 1) + '&#8230;';
        }

        // Search, order and type can use the original data
        return data;
    };
};

是否可以以任何方式组合这些功能?

我的问题的最接近的解决方案是在{datatables论坛上找到this。 这正是我想要做的,但它不起作用,因为d = renderArray[r](d, type, row, meta);不是一个函数。

提前致谢。

编辑: Table of available renders

Error of function not defined

1 个答案:

答案 0 :(得分:0)

mRender函数传递整行的数据。因此,您可以使用数据表

访问要访问的任何属性渲染多个函数
{
    mData: "bar",
    mRender: function(data, type, val) {
        switch (type) {
            case 'display':
                return '<a href="/data/' + data.foo + '">' + data.bar + '</a>';
                break;
            // optionally, add case statements for 'sort', 'filter', and 'type'
            default:
                return data.bar;
                break;
        }
    }
}