我正在尝试向数据表的每一行添加data-foo
属性。我需要此属性来对数据进行排序,但是我尝试了以下操作,但未添加定义的属性
cx.common.data.cxAdminDataTables.EbFaqCategory = $CxRecordsTable.cxAdminDataTable({
ajaxUrl: '<?php echo $this->CxHelper->Route('eb-admin-get-general-faq-categories')?>',
// Per-row function to iterate cells
"createdRow": function (row, data, rowIndex) {
// Per-cell function to do whatever needed with cells
$.each($('tr', row), function (colIndex) {
// For example, adding data-* attributes to the cell
$(this).attr('data-foo', "bar");
});
},
columns: [
cx.common.admin.tableEditColumn('id', { delete: true }),
{ data: 'category_name' },
{ data: 'faq_order' },
cx.common.admin.tableDateColumn('date_created')
]
});
我的查看代码:
<table id="cx-records-table" class="table table-striped table-bordered faq-categories-table" width="100%">
<thead>
<tr>
<th></th>
<th class="hasinput">
<input type="text" class="form-control filter" placeholder="Name">
</th>
<th class="hasinput">
<input type="text" class="form-control filter" placeholder="Order">
</th>
<th class="hasinput">
<input type="text" class="form-control filter" placeholder="Date Created">
</th>
</tr>
<tr>
<th></th>
<th class="all">Name</th>
<th class="all">Order</th>
<th class="all">Date Created</th>
</tr>
</thead>
<tbody></tbody>
</table>
JS:
var $sortable = $( ".faq-categories-table > tbody" );
$sortable.sortable({
stop: function ( event, ui ) {
var parameters = $sortable.sortable( "toArray");
console.log(parameters);
$.ajax({
url: '<?php echo $this->CxHelper->Route('eb-admin-change-general-faq-category-order')?>',
type: 'POST',
data: { value: parameters },
success: function (data) {
cx.common.data.cxAdminDataTables.EbFaqCategory.cxAdminDataTable("reloadAjax");
}
});
}
});
答案 0 :(得分:2)
$.each($('tr', row)
将不起作用,因为row
已经是tr
标签,并且将$
与第二个参数一起使用将限制在内部将上下文作为第二个参数。 tr
中没有tr
,因此您的选择为空。
只需尝试:
"createdRow": function (row, data, rowIndex) {
// Per-cell function to do whatever needed with cells
$(row).attr('data-foo', "bar");
},