简体   繁体   English

从数据表中的变量设置bSortable的问题

[英]Issue with setting bSortable from a variable in datatables

I am trying to implement the hide sorting option for a particular columns by making use of datatables plugin in php. 我正在尝试通过使用PHP中的datatables插件来实现特定列的隐藏排序选项。 If I write the following code then its working: "aoColumns" : [{'bSortable': false},null,null,null,null,null,null,null,null,null,null,null,null,null,{'bSortable': false}], 如果我编写以下代码,则其工作方式为:“ aoColumns”:[{'bSortable':false},null,null,null,null,null,null,null,null,null,null,null,null,null,{ 'bSortable':false}],

But I need dynamic and that is why I use a variable and the value of that(for my case {'bSortable': false},null,null,null,null,null,null,null,null,null,null,null,null,null,{'bSortable': false} I store it in a js variable called excluded_clmns_sorting ). 但是我需要动态的,这就是为什么我使用一个变量及其值的原因(对于我的情况{'bSortable': false},null,null,null,null,null,null,null,null,null,null,null,null,null,{'bSortable': false}我把它保存在一个js变量,名为excluded_clmns_sorting )。 Even if I am trying to print the value of excluded_clmns_sorting , it's showing {'bSortable': false},null,null,null,null,null,null,null,null,null,null,null,null,null,{'bSortable': false} . 即使我试图打印的价值excluded_clmns_sorting ,它显示{'bSortable': false},null,null,null,null,null,null,null,null,null,null,null,null,null,{'bSortable': false} Then I coded like "aoColumns" : [excluded_clmns_sorting], but it is showing error in console.The error is " TypeError:oCol is undefined ". 然后,我编码为"aoColumns" : [excluded_clmns_sorting],但它在控制台中显示错误。错误为“ TypeError:oCol is undefined ”。 So please let me know where is the problem. 所以,请让我知道问题出在哪里。

Thanks in advance. 提前致谢。

I suspect your problem is that you are assigning a string to excluded_clmns_sorting. 我怀疑您的问题是您正在将一个字符串分配给exclude_clmns_sorting。 But, it's hard to be sure with so little info. 但是,很难保证很少的信息。 If that is the case, you need to JSON.parse the string before passing it to datatables. 如果是这种情况,则需要先将字符串JSON.parse,然后再将其传递到数据表。 Datatables is expecting an array for aoColumns. 数据表期望aoColumns的数组。

Either of these work: 这些工作之一:

$(document).ready(function() {
    var excluded_clmns_sorting = JSON.parse('[{"bSortable": false},  null, null,  null, {"bSortable" : false}]');
    //var excluded_clmns_sorting = [{"bSortable": false},  null, null,  null, {"bSortable" : false}];
    $('#example').dataTable({
        "sPaginationType": "full_numbers",
        "aoColumns": excluded_clmns_sorting
    });
});

http://jsfiddle.net/ave8q/ http://jsfiddle.net/ave8q/

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

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