我有这个非常基本的表格显示公司:
<table id="companies" class="table table-striped table-bordered" cellspacing="0" width="100%">
<thead>
<tr>
<th>Company Name</th>
<th class="hidden-xs hidden-sm hidden-md">Business Field</th>
<th class="hidden-xs hidden-sm">Area</th>
<th class="hidden-xs hidden-sm">Related Items</th>
<th class="hidden-xs">Subscription ends</th>
</tr>
</thead>
</table>
我正在使用“datatables 1.10”和以下初始化脚本:
oTable = $('#companies').dataTable({
"processing": true,
"serverSide": true,
"ajax": {
"url": "ajax/companies/get_companies.ajax.php"
"type": "POST"
},
"columns": [
{ "bSearchable": true, "data": "company_name" },
{ "bSearchable": true, "data": "company_field" },
{ "bSearchable": true, "data": "company_area" },
//{ "bSearchable": true, "data": "relative_items" }, //displays id numbers from DB eg: (6, 32, 17, 24) but I dont want to display just ID numbers
{ // Here I convert the item ID's array to Item Names
"bSearchable": true,
"data": "relative_items",
"mRender": function ( data ) {
var trimResponse = "initialValue"; //if I don't set this variable I get datatables error...
$.ajax({
type: "POST",
url: "companies/get_relative_items.php",
data: "datastring="+data,
success: function(resp){
trimResponse = $.trim(resp);
console.log(trimResponse); //works! I can see the item names in console log
}
})
return trimResponse; //HOWEVER, datatables display the initial value of this variable and NOT the new one set by the above function
}
},
{ "bSearchable": false, "data": "subscription_end" }
],
"order": [[1, 'asc']]
});
//alert(trimResponse); //if commented out, it also displays the initial value and not the one acquired by ajax
通过阅读javascript中的注释,您可能已经发现在第四列中我想显示每个公司的相关项目(存储在另一个数据库表中)。
到目前为止,我可以显示ID号,但由于ID号不是“人性化”,我使用ajax返回id号数组(例如:2,5,3,12)作为对应的实际名称每件商品ID(例如:衬衫,裤子,袜子,夹克)。
但是,虽然我可以在控制台中看到名称数组正确显示,但是数据表第四列仅显示变量“trimResponse”的初始值,而不显示ajax给出的新值...
有关如何在datatables列中显示ajax响应的任何建议? 提前谢谢。
答案 0 :(得分:1)
无论如何,在经过多次努力之后,我决定采用不同的方法解决问题:
我将初始化脚本更改为&#34;&#34;默认&#34;状态:
oTable = $('#companies').dataTable({
"processing": true,
"serverSide": true,
"ajax": {
"url": "ajax/companies/get_companies.ajax.php"
"type": "POST"
},
"columns": [
{ "bSearchable": true, "data": "company_name" },
{ "bSearchable": true, "data": "company_field" },
{ "bSearchable": true, "data": "company_area" },
{ "bSearchable": true, "data": "relative_items" },
{ "bSearchable": false, "data": "subscription_end" }
],
"order": [[1, 'asc']] });
我决定不应该在初始化脚本中进行任何更改,而应该在向表提供结果的ajax文件中进行更改(ajax / companies / get_companies.ajax.php)。
这个脚本是我从datatables.net获得的默认服务器端脚本。点击此处:http://www.datatables.net/examples/server_side/simple.html
最初我只想为数据表定义columns数组:
$columns = array(
array( 'db' => 'name', 'dt' => 'company_name' ),
array( 'db' => 'business_field', 'dt' => 'company_field' ),
array( 'db' => 'area', 'dt' => 'company_area' ),
array( 'db' => 'items', 'dt' => 'relative_items' ),
array(
'db' => 'subscription_ends',
'dt' => 'subscription_end',
'formatter' => function( $d, $row ) {
return date( 'd M Y', strtotime($d));
}
)
);
工作解决方案是使用&#39;格式化程序&#39;也是第四栏(正如我已经为第五栏做的那样)。像那样:
$columns = array(
array( 'db' => 'name', 'dt' => 'company_name' ),
array( 'db' => 'business_field', 'dt' => 'company_field' ),
array( 'db' => 'area', 'dt' => 'company_area' ),
array(
'db' => 'items',
'dt' => 'relative_items',
'formatter' => function( $d, $row ) {
$conn = ... //CONNECTION TO THE DATABASE
$items_array = str_replace("|", ", ", substr(substr($d, 1), 0, -1));
//format the items strin from this: |3|12|7| to comma-separated 3,12,7
$arr = explode(", ", $items_array);
//create my array
$first = reset($arr);
$last = end($arr);
foreach ($arr as $item_id) { //For each Id, get the corresponding Item Name:
$get_item_name = mysql_query("SELECT name FROM ".DB_TABLE_PREFIX."items WHERE id = '$item_id' ");
$row = mysql_fetch_array($get_item_name);
//Append to the results comma and space (", ") except to the last one...
if ($item_idx == $last) {
$res .= $row["name"];
}else{
$res .= $row["name"];
$res .= ", ";
}
}
return $res;
//and voila! worked like charm!
//column now shows: Jacket, Shirt, T-shirt (instead of id's: 3, 12, 7)
}
),
array(
'db' => 'subscription_ends',
'dt' => 'subscription_end',
'formatter' => function( $d, $row ) {
return date( 'd M Y', strtotime($d));
}
)
);
所以,解决方案只是从不同的角度来看问题!
希望这可以帮助一些人!