简体   繁体   English

columDefs 和 columns 是互斥的吗?

[英]Are columDefs and columns mutually exclusive?

I want to make a table from JSON data formatted as arrays.我想从格式为 arrays 的 JSON 数据制作一个表格。 Within that table, the column titles are dynamic (they're week numbers) but the rendering function for a specific column isn't.在该表中,列标题是动态的(它们是周数),但特定列的呈现 function 不是。 So I wanted to use the columns options for the title and the columnDefs for the rendering function.所以我想使用columns选项作为标题, columnDefs用于渲染 function。

Here's what I have so far:这是我到目前为止所拥有的:

var messagesDataRaw = {
    data: [
        ["03", "129", "129", "77.36%"],
        ["40", "172", "396", "10.32%"],
        ["41", "614", "180", "10.29%"],
        ["43", "155", "221", "9.30%"]
], 
    columns: [
        { title: 'team' },  
        { title: '32' }, 
        { title: '33' }, 
        { title: 'rate' }
    ]};
var messagesData = messagesDataRaw.data;
var messagesCol = messagesDataRaw.columns;

var renderExceptionFct = function(data, type, row, meta) {
    return '<a target=\"_blank\"  href=\"http://URL_TO_TEAM#EXCEPTION_' + data.trim() + '\">' + data + '</a>';
};

$('#messages_datatable').DataTable( {
    data: messagesData,
    columns: messagesCol,
    paging: false,
    ordering: false,
    searching: false,
    info: false,
    columnDefs: [{
        target: 0,
        type: "display",
        render: renderExceptionFct
    }
]});

However, this doesn't work.但是,这不起作用。 I might resort to using only the column option but this would imply sending the render function every time and i'd rather avoid that.我可能只使用column选项,但这意味着每次都发送渲染 function,我宁愿避免这种情况。

No, both options columns and columnDefs can be used at the same time.不可以,options columnscolumnDefs可以同时使用。 See official documentation for more details.有关详细信息,请参阅官方文档

Problem with your code not working is that you've a typo in your initialization code, it should be columnDefs.targets and not columnDefs.target , see below:您的代码不起作用的问题是您的初始化代码中有错字,它应该是columnDefs.targets而不是columnDefs.target ,见下文:

columnDefs: [{
    targets: 0,
    type: "display",
    render: renderExceptionFct
}

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

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