gpt4 book ai didi

php - DataTables - 来自 Ajax 数据源的动态列?

转载 作者:可可西里 更新时间:2023-11-01 12:36:07 25 4
gpt4 key购买 nike

我正在尝试让 DataTables 从 AJAX 数据源中读取列名,但似乎这里一定缺少某些东西。

我做了一个 fiddle fiddle我可以在其中手动定义表正在使用的数据和列。

表格在 HTML 中声明,无需定义列名 ( <thead>..</thead> ):

<table id="example" class="display table table-striped table-bordered" 
cellspacing="0" width="100%"></table>

在JS中我们手动定义数据:

var data = [
[ "Row 1 - Field 1", "Row 1 - Field 2", "Row 1 - Field 3" ],
[ "Row 2 - Field 1", "Row 2 - Field 2", "Row 2 - Field 3" ],
];

然后手动定义列名或标题:

var columns = [
{ "title":"One" },
{ "title":"Two" },
{ "title":"Three" }
];

然后当我们初始化表时,我们只需将先前声明的信息传递给 DataTables 以供使用:

$(document).ready(function() {
$('#example').DataTable( {
dom: "Bfrtip",
data: data,
columns: columns
});
});

结果是:

Resulting Table

现在我的问题是,如果数据包含在 AJAX server side response 中,我将如何让它工作? ?

我已经尝试过各种方式和形式,但似乎没有任何效果,我正在努力寻找这方面的相关文档。

例如,如果服务器端处理发回一个 JSON 响应,其中包含末尾的列名:

{
"data": [
{
"id": "1",
"One": "Row 1 - Field 1",
"Two": "Row 1 - Field 2",
"Three": "Row 1 - Field 3"
},
{
"id": "2",
"One": "Row 2 - Field 1",
"Two": "Row 2 - Field 2",
"Three": "Row 2 - Field 3"
}
],
"options": [],
"files": [],
"columns": [
{
"title": "One",
"data": "One"
},
{

"title": "Two",
"data": "Two"
},
{
"title": "Three",
"data": "Three"
}
]
}

鉴于这是响应,我尝试将 DataTables 配置为使用 AJAX 数据源获取行信息,如下所示:

$(document).ready(function() {
$('#example').DataTable( {
dom: "Bfrtip",
"ajax": '/test.php',
columns: columns
});
});

但显然columns此处未定义。

所以我事先得到了列数据:

function getPromise() {
var deferred = $.Deferred();
var dataUrl = document.location.origin+'/text.php';
$.getJSON(dataUrl, function(jsondata) {
setTimeout(function() {
deferred.resolve(jsondata);
}, 0);
}).fail(function( jqxhr, textStatus, error ) {
// ********* FAILED
var err = textStatus + ", " + error;
console.log( "Request Failed: " + err );
});
return deferred.promise();
}
// Get the columns
getPromise().done(function(jsondata) {
columns = jsondata.columns;
console.log(columns);
});

并将其传递给上面的 DataTables。但是这一次我在运行示例时得到的只是控制台中的错误提示 TypeError: p is undefined .

那么我该如何使用服务器端响应中返回的动态生成的列呢?有没有更简单的方法来实现这一点?

编辑:

用于服务器端处理/生成上述 JSON 响应的 DataTables 编辑器代码:

<?php
// DataTables PHP library
require_once '/path/to/DataTables.php';

// Alias Editor classes so they are easy to use
use
DataTables\Editor,
DataTables\Editor\Field,
DataTables\Editor\Format,
DataTables\Editor\Mjoin,
DataTables\Editor\Upload,
DataTables\Editor\Validate;

// Build our Editor instance and process the data coming from _POST
$out = Editor::inst( $db, 'example' )
->fields(
Field::inst( 'id' )->set(false),
Field::inst( '`One`' )->validator( 'Validate::notEmpty' ),
Field::inst( '`Two`' )->validator( 'Validate::notEmpty' ),
Field::inst( '`Three`' )->validator( 'Validate::notEmpty' )
)
->process( $_POST )
->data();

// On 'read' remove the DT_RowId property so we can see fully how the `idSrc`
// option works on the client-side.
if ( Editor::action( $_POST ) === Editor::ACTION_READ ) {
for ( $i=0, $ien=count($out['data']) ; $i<$ien ; $i++ ) {
unset( $out['data'][$i]['DT_RowId'] );
}
}

// Create the thead data
if (count ($out) > 0) {
$columns = array();
foreach ($out['data'][0] as $column=>$relativeValue) {
// Add all but the id value
if ($column !== 'id') {
// Add this column name
$columns[] = array(
"title"=>$column,
"data"=>$column
);
}
}
}
// Add the the thead data to the ajax response
$out['columns'] = $columns;
// Send the data back to the client
echo json_encode( $out );

最佳答案

如果您不使用内置的 DataTables ajax,考虑到您的数据结构应该很容易:

$(document).ready(function() {
$.ajax({
type: 'POST',
dataType: 'json',
url: '/echo/json/',
data: {
json: JSON.stringify(jsonData)
},
success: function(d) {
$('#example').DataTable({
dom: "Bfrtip",
data: d.data,
columns: d.columns
});
}
});
});

this JSFiddle ,那么您只能一次加载所有数据,但这应该不是一个大问题......除非您改变它从初始 ajax 调用中获取列并且一旦 DataTable 启动然后添加内置的 ajax - 虽然我没有尝试过......

关于php - DataTables - 来自 Ajax 数据源的动态列?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36046139/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com