I would like to load datatable data with AJAX and add some parameters to the rows and cells (classes or 'data' attributes). The most tricky part is that I have a PHP helper that creates javascript. For example:
我想用AJAX加载数据表数据,并向行和单元格添加一些参数(类或'数据'属性)。最棘手的部分是我有一个创建javascript的PHP助手。例如:
$datatables = array(
'type' => array(
'bAutoWidth' => false,
'ajax' => site_url('admin/parameters/area_add_type_table_data_ajax/' . $t_id),
'paging' => false,
'searching' => true,
'columns' => array(
array('width' => '20%', 'class' => 'text-center')
),
'info' => true,
'scrollX' => true,
),
);
This PHP array is JSON-encoded and creates the javascript that displays the table.
这个PHP数组是JSON编码的,并创建显示表的javascript。
The ajax source also exits JSON-encoded array, for example:
ajax源也退出JSON编码的数组,例如:
$data = array('data' => array(array('SOME TEXT HERE...'), array('SOME TEXT IN ANOTHER ROW...')); exit(json_encode($data);
One row, produced by this code is:
这段代码生成的一行是:
<tr role="row" class="odd"><td class="text-center">SOME TEXT HERE...</td></tr>
But then I need to add some "parameters" to final rows (tr) and some cells (tds) (attributes in double asterisks).
但后来我需要在最终行(tr)和一些单元格(tds)(双星号属性)中添加一些“参数”。
<tr role="row" class="odd" **data-id="##"**><td class="text-center" **data-content="unit_short" data-id="##"**>SOME TEXT HERE...</td></tr>
That IDs are depending on the current data in the row, so they are variable.
这些ID取决于行中的当前数据,因此它们是可变的。
(I apologise for the one row code examples)
(我为一行代码示例道歉)
Thank you for any help. If it is not possible to do it in my "PHP-way", write at least a solution in pure JS.
感谢您的任何帮助。如果不能用我的“PHP方式”来做,那么至少在纯JS中编写一个解决方案。
1 个解决方案
#1
1
I dont know how your PHP helper works, but you can use the createdRow
callback to do that :
我不知道你的PHP助手是如何工作的,但你可以使用createdRow回调来做到这一点:
createdRow: function(tr, data, index) {
$(tr).attr('data-id', '##');
$('td', tr).eq(1)
.attr('data-content', 'unit_short')
.attr('data-id', '##')
}
It is not clear from the question what ##
is and where it come from, but this is at least the way to go. You can use index
if there is no ##
available.
从问题中不清楚##是什么以及它来自哪里,但这至少是要走的路。如果没有##可用,您可以使用索引。
#1
1
I dont know how your PHP helper works, but you can use the createdRow
callback to do that :
我不知道你的PHP助手是如何工作的,但你可以使用createdRow回调来做到这一点:
createdRow: function(tr, data, index) {
$(tr).attr('data-id', '##');
$('td', tr).eq(1)
.attr('data-content', 'unit_short')
.attr('data-id', '##')
}
It is not clear from the question what ##
is and where it come from, but this is at least the way to go. You can use index
if there is no ##
available.
从问题中不清楚##是什么以及它来自哪里,但这至少是要走的路。如果没有##可用,您可以使用索引。