【问题标题】:Inline if statement inside datatable数据表内的内联 if 语句
【发布时间】:2015-11-21 17:47:21
【问题描述】:

我不确定之前是否有人问过这个问题。但我找不到它。这就是我现在问的原因。

我必须使用更多数据表。所以我正在编写一个通用函数来通过传递参数一次又一次地调用它。对于那个通用代码,我必须使用内联 if 语句来添加列宽,这里我附上了我的代码。有人帮我在其中插入内联条件。

function common_datatable(file_ajax, module_name, btns_list, widths, view_btn) {            
  return $('#'+module_name+'_table').DataTable({ 
    "processing": true,
    "serverSide": true,
    "ajax": file_ajax, 
    "bLengthChange": false, "bAutoWidth": false , "sScrollX": "100%", 
    aoColumns : [
        (widths[0] !=0) ? '{ "sWidth": "1%" }' : '',            
        { "sWidth": "30%" },
        { "sWidth": "30%"},
        { "sWidth": "9%"}           

    ],  orderCellsTop: true,
    "scrollX": true,
    "order": [   [1, "asc"]  ], 
    "columns": [{"orderable": false}, null, null,  null, null, null ]
  });       
}

上面的代码我通过宽度参数传递了宽度数组,这是我正在使用的代码,

(width[0] !=0) ? '{ "sWidth": "1%" }' : '',

但它在里面不起作用。

【问题讨论】:

  • its not working inside.是什么意思?
  • inline if 语句不起作用。它显示错误“意外(”

标签: jquery datatable inline-code


【解决方案1】:

在您尝试时,无法使用条件元素初始化数组。尝试如下

    function common_datatable(file_ajax, module_name, btns_list, widths, view_btn) {            
        var aoColumns = [                
            { "sWidth": "30%" },
            { "sWidth": "30%"},
            { "sWidth": "9%"}
       ];
        if(widths[0] != 0) {
            aoColumns.unshift({"sWidth": "1%"});
        }

        return $('#'+module_name+'_table').DataTable({ 
            "processing": true,
            "serverSide": true,
            "ajax": file_ajax, 
            "bLengthChange": false, "bAutoWidth": false , "sScrollX": "100%", 
            "aoColumns": aoColumns,  
            "orderCellsTop": true,
            "scrollX": true,
            "order": [   [1, "asc"]  ], 
            "columns": [{"orderable": false}, null, null,  null, null, null ]
        });       
    }

【讨论】:

  • 那么结果如何?
  • 感谢您的帮助,是的,它的工作。我没有声望投票给你。对不起。
  • 你是个了不起的人。谢谢
【解决方案2】:

函数中的变量名是widths,你在if语句中使用了width

试试这个:(widths[0] !=0) ? '{ "sWidth": "1%" }' : '',
而不是: (width[0] !=0) ? '{ "sWidth": "1%" }' : '',

【讨论】:

    猜你喜欢
    • 2017-10-28
    • 1970-01-01
    • 1970-01-01
    • 2022-07-07
    • 2020-05-15
    • 2013-02-15
    • 1970-01-01
    • 2017-03-24
    • 2012-03-25
    相关资源
    最近更新 更多