简体   繁体   English

jquery 数据表在初始化时设置宽度

[英]jquery datatable set width upon initialization

I'm trying to set the width of a cell in a jQuery datatable upon initialization and it never seems to work.我正在尝试在初始化时在 jQuery 数据表中设置单元格的宽度,但它似乎从未起作用。 I've have tried what the official site recommends and other examples .我已经尝试过官方网站推荐的内容和其他示例 Below are some of the options I have tried.以下是我尝试过的一些选项。 Please help on what I'm doing wrong?请帮助我做错了什么?

Ex: 1例如:1

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

Ex: 2例如:2

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

Ex: 3例如:3

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

As with any other usage of a CSS percentage value, the value must be a percentage of something .与 CSS 百分比值的任何其他用法一样,该值必须something的百分比。 If the table itself not have a defined width , then 10% is untranslatable.如果表格本身没有定义的width ,则10%是不可翻译的。 So give your table a width :所以给你的桌子一个width

#dataTables-comments {
  width: 800px;
}

and be sure to turn offautoWidth so dataTables not begin to overrule the predefined column widths :并确保关闭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
});

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

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