简体   繁体   English

高级JavaScript来源数据-DataTables

[英]Advanced javascript sourced data - DataTables

The default way of initializing dataTables with javascript sourced data is with the data option and it accepts either an array of arrays or an array of objects as far as I know. 用javascript源数据初始化dataTables的默认方法是使用data选项 ,据我所知,它可以接受数组数组或对象数组。

var arrayDataSet = [
    ['Trident', 'Internet Explorer 11', '11'],
    ['Blink', 'Chrome 35', '35'],
    ...
];

var objectDataSet = [
    {
        engine: 'Trident',
        browser: 'Internet Explorer 11',
        version: '11'
    },
    {
        engine: 'Blink',
        browser: 'Chrome 35',
        version: '35'
    }
    ...
];

I want to use the createdRow option though and add for example an id and url on each row. 我想使用createdRow选项,并在每行上添加例如id和url。 I think I want to initialize with data like this instead: 我想我想用这样的数据初始化:

var otherDataSet = [
    {
        id: 'ie11',
        url: 'http://windows.microsoft.com/en-us/internet-explorer/download-ie',
        data: {
            engine: 'Trident',
            browser: 'Internet Explorer 11',
            version: '11'
        }
    },
    {
        id: 'chr35',
        url: 'https://www.google.com/chrome/browser/',
        data: {
            engine: 'Blink',
            browser: 'Chrome 35',
            version: '35'
        }
    }
];

Is it possible? 可能吗?

Yes this can be done. 是的,可以这样做。 You need to set the data property in the columns config object to set where each column should use for it's data. 您需要在columns配置对象中设置data属性,以设置每列的数据使用位置。 Then you can reference any other fields in the createdRow callback. 然后,您可以在createdRow回调中引用任何其他字段。 Here is a working fiddle: http://jsfiddle.net/V7bBg/2/ 这是一个有效的小提琴: http : //jsfiddle.net/V7bBg/2/

var otherDataSet = [
    {
        id: 'ie11',
        url: 'http://windows.microsoft.com/en-us/internet-explorer/download-ie',
        data: {
            engine: 'Trident',
            browser: 'Internet Explorer 11',
            version: '11'
        }
    },
    {
        id: 'chr35',
        url: 'https://www.google.com/chrome/browser/',
        data: {
            engine: 'Blink',
            browser: 'Chrome 35',
            version: '35'
        }
    }
];


$(document).ready(function() {
    $('#example').dataTable( {
        "data": otherDataSet,

        //define the columns and set the data property for each column
        "columns": [
            { "title": "Engine",  "data": "data.engine" },
            { "title": "Browser", "data": "data.browser"},
            { "title": "Version", "data": "data.version"}
        ],
        "createdRow": function ( row, data, index ) {
            //adding id and url as attributes to the row
            $(row).attr('id',data.id).attr('data-url',data.url);
        }
    } );            
});

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

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