这个问题与此处的插件有关:http://www.datatables.net/usage/columns
var hidecols = '{"sClass": "Hide", "aTargets": [0]},{"sClass": "asdf", "aTargets": [1]},{"sClass": "qwer", "aTargets": [2]}';
var hidecolsobj = eval('(' + hidecols + ')');
var oTable = $('#MainContent_OverviewTable').dataTable({
"sPaginationType": "full_numbers",
"bProcessing": true,
"bServerSide": true,
"aoColumnDefs":
[
hidecolsobj, // <--------- *** HERE ***
],
"sAjaxSource": "Services/Service.svc/GetDataTableOutputOverview",
"fnServerData": function (sSource, aoData, fnCallback) {
var jsonAOData = JSON.stringify(aoData);
$.ajax({
type: "POST",
async: true,
url: sSource,
contentType: "application/json; charset=utf-8",
data: '{"Input":' + jsonAOData + '}',
dataType: "json",
success: function (msg) {
fnCallback(msg);
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
alert(XMLHttpRequest.status);
alert(XMLHttpRequest.responseText);
}
});
}
});
因此代码工作正常,没有错误,但类“qwer”是唯一一个应用于我的表。我测试了它,它只应用我的对象列表中最后出现的类。我希望每个列都有hidecols变量中定义的类。我怎么能这样做?
这是它在Datatables网站的文档中的显示方式:
$('#example').dataTable( {
"aoColumnDefs": [
{ "sClass": "my_class", "aTargets": [ 0 ] }
]
} );
编辑:
"aoColumnDefs":
[
{ "sClass": "Hide", "aTargets": [0] },
{ "sClass": "asdf", "aTargets": [1] },
{ "sClass": "qwer", "aTargets": [2] }
],
以上编辑工作正常,但这不适合我。我需要能够动态构建字符串hidecols。
答案 0 :(得分:1)
您正在为DataTables传递aoColumnDefs参数的字符串。只需删除hidecols“string”周围的引号,使其成为一个对象数组。这将消除对邪恶(eval)的需要。
var hidecols = [{"sClass": "Hide", "aTargets": [0]},{"sClass": "asdf", "aTargets": [1]},{"sClass": "qwer", "aTargets": [2]}];
阿伦
答案 1 :(得分:0)
啊!我想出了怎么做!
而不是这样做:
"aoColumnDefs":
[
hidecolsobj, // <--------- *** HERE ***
],
我应该这样做:
var hidecols = '[{"sClass": "Hide", "aTargets": [0]},{"sClass": "asdf", "aTargets": [1]},{"sClass": "qwer", "aTargets": [2]}]';
var hidecolsobj = eval('(' + hidecols + ')');
"aoColumnDefs": hidecolsobj, // <--------- *** HERE ***