jQuery datatables doesn't show titles

问题: I'm trying to get dinamically tittles on jquery datatables from object, but it doesn't draw them. For example, with this object: var tableData = [ { id: 1, user: '...

问题:

I'm trying to get dinamically tittles on jquery datatables from object, but it doesn't draw them.

For example, with this object:

var tableData = [ 
    { id: 1, user: 'admin', pass: 'pass', role: 'admin' },
    { id: 2, user: 'user', pass: 'pass', role: 'user' } 
]

this is my method:

function generateDatatable(tableId, tableData) {

    var columns = [];
    if (tableData.length == 0) {
        columns[0] = { data: '-' };
    } else {
        Object.keys(tableData[0]).forEach(element => {
            columns[columns.length] = { data: element };
        });
    }

    jQuery('#' + tableId).DataTable({
        data: tableData,
        language: dataTablesTexts,
        pagingType: 'full',
        lengthMenu: [
            [10, 25, 50, -1], 
            [10, 25, 50, 'All']
        ],
        searching: true,
        ordering:  true,
        paging: true,
        select: false,
        info: true,
        responsive: true,
        columns: columns
    });

}

I can see the values but not the tittles.

Resultant columns will look like this:

var columns = [
    {data: "id"},
    {data: "user"},
    {data: "pass"},
    {data: "role"}
]

if I change data: "..." to title: "..." I can see the titles but not the values.

What could be wrong with the code.


回答1:

You should use both: data to point to specific property within data source that corresponds to current column for each entry and title to specify column title.

  • 发表于 2019-03-11 19:17
  • 阅读 ( 151 )
  • 分类:sof

条评论

请先 登录 后评论
不写代码的码农
小编

篇文章

作家榜 »

  1. 小编 文章
返回顶部
部分文章转自于网络,若有侵权请联系我们删除