【问题标题】:jquery datatable set width upon initializationjquery数据表在初始化时设置宽度
【发布时间】:2016-05-11 20:03:27
【问题描述】:

我试图在初始化时在 jQuery 数据表中设置单元格的宽度,但它似乎永远无法工作。我已经尝试过official site 和其他examples 的建议。以下是我尝试过的一些选项。请帮忙看看我做错了什么?

例如:1

$('#dataTables-comments').DataTable({
    "bLengthChange": false,
    "bFilter": false,
    "iDisplayLength": 5,
    "aoColumns": [{
            "sWidth": "50%"
        }, 
        {
            "sWidth": null
        },  
        {
            "sWidth": null
        },   
        {
            "sWidth": null
        },   
        {
            "sWidth": null
        }   
    ],
    "responsive": true
});

例如:2

$('#dataTables-tbl').DataTable({
    "bLengthChange": false,
    "bFilter": false,
    "iDisplayLength": 5,
    "columnDefs": [{
        "width": "50%",
        "targets": 0
    }],
    "responsive": true
});

例如:3

$('#dataTables-tbl').DataTable({
    "bLengthChange": false,
    "bFilter": false,
    "iDisplayLength": 5,
    "columns": [{
        "width": "50%"
    }, {
        "width": "10%"
    }, {
        "width": "10%"
    }, {
        "width": "10%"
    }, {
        "width": "10%"
    }],
    "responsive": true
});

【问题讨论】:

    标签: javascript jquery css datatables


    【解决方案1】:

    与 CSS 百分比值的任何其他用法一样,该值必须something 的百分比。如果表本身没有定义width,则10% 是不可翻译的。所以给你的桌子一个width

    #dataTables-comments {
      width: 800px;
    }
    

    并确保关闭autoWidth,这样dataTables 就不会开始推翻预定义的列宽:

    $('#dataTables-tbl').DataTable({
        autoWidth: false, //<---
        "bLengthChange": false,
        "bFilter": false,
        "iDisplayLength": 5,
        "columns": [{
            "width": "50%"
        }, {
            "width": "10%"
        }, {
            "width": "10%"
        }, {
            "width": "10%"
        }, {
            "width": "10%"
        }],
        "responsive": true
    });
    

    【讨论】:

    • 由于它似乎无法识别数据表中的宽度,但将样式添加到 css 文件使其工作。谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-13
    • 2016-05-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-05
    相关资源
    最近更新 更多