在动态jquery datatable上将sType设置为所有aoColumns [英] Set sType to all aoColumns on dynamic jquery datatable

查看:1007
本文介绍了在动态jquery datatable上将sType设置为所有aoColumns的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究一个巨大的自定义datatable函数,我有一个简单的问题,我需要将'sType'设置为所有'aoColumns',但是这些表是动态的,因此它们可能有6或10列,我找不到办法,我已经尝试了以下选项:

  //不工作
aoColumns:[{sType:string,aTargets:[_all]}],

//这样做,但它不动态:/
aoColumns:{sType:string},{sType:string},{sType:string},{sType:string},{sType :string},{sType:string}],

请使用服务器端执行此操作



obs .:我有一列var数列

  6列
aoColumns:[{sType:string},{sType:string},{sType string},{sType:string},{sType:string},{sType:string}],
10列
aoColumns [{sType:string},{sType:string},{sType:string},{sType:str sType:string},{sType:string},{sType:string},{sType string},{sType:string}],

无论如何

解决方案

服务器端PHP:嗯,如果你在php中创建表,那么你应该能够构建一个所有需要的字符串您可以插入到javacript中的数组部分。

 <?php 
$ colnum = 6;
$ i = 1;

$ aocolumns = array();

while($ i< = $ colnum){
$ aocolumns [] ='{sType:string}'
$ i ++;
}

$ aocolumns = join(,,$ aocolumns);
$ aocolumns ='['。 $ aocolumns。 ']'

?>

然后将其放在像

 aoColumns:<?= $ aocolumns?> 

Javascript:假设您将ajax的列作为一个名为cols的数组,尝试这个循环。 :

  var aocolumns = []; 
for(i in cols){
var ao = {sType:string};
aocolumns.push(ao);
}
alert(aocolumns);


I'm working on a huge custom datatable function and I've a simple issue, I need to set 'sType' to all 'aoColumns', but these tables are dynamic, so they may have 6 or 10 columns, and I can't find a way to do, I have already tried the following options:

//doesn't work
"aoColumns": [{"sType":"string", "aTargets": ["_all"]}],

//this works, but its not dynamic :/
"aoColumns": [{"sType":"string"}, {"sType":"string"}, {"sType":"string"}, {"sType":"string"}, {"sType":"string"}, {"sType":"string"}],

and I can't do this using server-side

obs.: I have a var with number of columns

6 columns
"aoColumns": [{"sType":"string"}, {"sType":"string"}, {"sType":"string"}, {"sType":"string"}, {"sType":"string"}, {"sType":"string"}],
10 columns
"aoColumns": [{"sType":"string"}, {"sType":"string"}, {"sType":"string"}, {"sType":"string"}, {"sType":"string"}, {"sType":"string"}, {"sType":"string"}, {"sType":"string"}, {"sType":"string"}, {"sType":"string"}],

Thanks anyway

解决方案

Server-side PHP: Well, If you are creating the table in php, then you should be able to build a string with all the needed array parts that you can plug into the javacript.

<?php
$colnum = 6;
$i = 1;

$aocolumns = array();

while($i <= $colnum){
  $aocolumns[] = '{"sType":"string"}';
  $i++;
}

$aocolumns = join(",",$aocolumns);
$aocolumns = '[' . $aocolumns . ']';

?>

then drop it in like

"aoColumns": <?=$aocolumns?>

Javascript: Assuming you get the columns back from the ajax as an array called "cols", you can try this for loop. :

var aocolumns = [];
for(i in cols){
  var ao = {"sType":"string"}; 
  aocolumns.push(ao);
}
alert(aocolumns);

这篇关于在动态jquery datatable上将sType设置为所有aoColumns的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆