Jquery Datatable:仅选定列的固定宽度

Jquery Datatable:仅选定列的固定宽度,jquery,datatable,datatables,Jquery,Datatable,Datatables,我有一个表,它的列数不是固定的。但是,前3列始终存在 因此,以下属性在我的情况下不起作用(因为在这种方法中需要固定列数) 我试过这样的方法: "aoColumns": [[1, { "sWidth": "50px" }] ] 这也不起作用,因为它会产生一些错误 请建议一个好方法。为什么不让函数动态生成aoColumns-array // function that generates the aoColumns-array based on the tables <thead> /

我有一个表,它的列数不是固定的。但是,前3列始终存在

因此,以下属性在我的情况下不起作用(因为在这种方法中需要固定列数)

我试过这样的方法:

"aoColumns": [[1, { "sWidth": "50px" }] ]
这也不起作用,因为它会产生一些错误


请建议一个好方法。

为什么不让函数动态生成
aoColumns
-array

// function that generates the aoColumns-array based on the tables <thead>
// columns beyond #3 get a fixed 25px width (just to be illustrative)
// expand the switch if certain columns need certain fixed widths
function aoColumns() {
    var ao = [];
    $("#table th").each(function(i) {
        switch (i) {
            case 0 : 
                ao.push({"sWidth": "50px"});
                break;
            case 1 : 
                ao.push({"sWidth": "100px"});
                break;
            case 2 : 
                ao.push({"sWidth": "100px"});
                break;
            default :
                ao.push({"sWidth": "25px"});
                break;
        }
    });
    return ao;
}

$(document).ready(function () {
    var table = $('#table').dataTable({
        aoColumns: aoColumns()
    });
});
// function that generates the aoColumns-array based on the tables <thead>
// columns beyond #3 get a fixed 25px width (just to be illustrative)
// expand the switch if certain columns need certain fixed widths
function aoColumns() {
    var ao = [];
    $("#table th").each(function(i) {
        switch (i) {
            case 0 : 
                ao.push({"sWidth": "50px"});
                break;
            case 1 : 
                ao.push({"sWidth": "100px"});
                break;
            case 2 : 
                ao.push({"sWidth": "100px"});
                break;
            default :
                ao.push({"sWidth": "25px"});
                break;
        }
    });
    return ao;
}

$(document).ready(function () {
    var table = $('#table').dataTable({
        aoColumns: aoColumns()
    });
});
function aoColumns() {
    var ao = [];
    $("#table th").each(function(i, th) {
        var caption=$(th).text();
        switch (caption) {
            case 'A' : 
                ao.push({"sWidth": "50px"});
                break;
            case 'B' : 
                ao.push({"sWidth": "100px"});
                break;

            /*...
            and so on
            ...*/

            default :
                ao.push({"sWidth": "25px"});
                break;
        }
    });
    return ao;
}