提问人:Alberto De Caro 提问时间:11/5/2012 最后编辑:CommunityAlberto De Caro 更新时间:3/29/2023 访问量:84214
jQuery DataTables:添加额外的列
jquery datatables: adding extra column
问:
场景
我第一次使用数据表(@version 1.9.4)向用户显示数据。 我成功地通过ajax检索了数据,并将它们绑定到数据表。
现在,我想添加额外的列,让用户处理记录。为了便于起见,目的是添加一个带有 onclick 处理程序的按钮,用于检索“点击”记录的数据。
在我的计划中,我会将与“clicked”记录对应的 json 项绑定到 onclick 处理程序。
到目前为止,如果我在 DOM 中为 action 列添加一个附加项,则数据表代码会发生错误:TH
Requested unknown parameter '5' from data source for row 0
问题
如何设置自定义列?如何用HTML填充他们的内容?
这是我的实际配置。
[HTML全
<table id="tableCities">
<thead>
<tr>
<th>country</th>
<th>zip</th>
<th>city</th>
<th>district code</th>
<th>district description</th>
<th>actions</th>
</tr>
</thead>
<tbody></tbody>
</table>
Java脚本
$('#tableCities').dataTable({
"bPaginate": true,
"bLengthChange": false,
"bFilter": true,
"bSort": true,
"bInfo": false,
"bAutoWidth": true
, "bJQueryUI": true
, "bProcessing": true
, "bServerSide": true
, "sAjaxSource": "../biz/GetCitiesByZip.asp?t=" + t
});
示例 Json 结果
{
"aaData":
[
[
"IT",
"10030",
"VILLAREGGIA",
"TO",
"Torino"
],
[
"IT",
"10030",
"VISCHE",
"TO",
"Torino"
]
],
"iTotalRecords": 2,
"iTotalDisplayRecords": 2,
"iDisplayStart": 0,
"iDisplayLength": 2
}
编辑
丹尼尔是对的。解决方案是在 中设置自定义列,指定 和 属性。特别是 mRender
允许定义自定义 html 和 javascript。aoColumnDefs
mData
mRender
/* inside datatable initialization */
, "aoColumnDefs": [
{
"aTargets": [5],
"mData": null,
"mRender": function (data, type, full) {
return '<a href="#" onclick="alert(\''+ full[0] +'\');">Process</a>';
}
}
]
答:
您可以用不同的方式定义列 喜欢这个
"aoColumns": [
null,
null,
null,
null,
null,
{ "mData": null }
]
或者这个
"aoColumnDefs":[
{
"aTargets":[5],
"mData": null
}
]
这里有一些文档列
看看这个 DataTables AJAX 源示例 - 列的 null 数据源
请注意,在 DataTables 1.9.2 之前,mData 称为 mDataProp。名称更改反映了此属性的灵活性,并且与 mRender 的命名一致。如果给出了“mDataProp”,则 DataTable 仍将使用它,因为它会在需要时自动将旧名称映射到新名称。
另一种解决方案/解决方法是添加“5”参数...
例如,为每一行添加额外的内容""
喜欢这个:
[
"IT",
"10030",
"VILLAREGGIA",
"TO",
"Torino",
""
],
[
"IT",
"10030",
"VISCHE",
"TO",
"Torino",
""
]
评论
以防万一使用较新版本的DataTables(1.10+)的任何人正在寻找这个问题的答案,我按照此页面上的说明进行操作:
https://datatables.net/examples/ajax/null_data_source.html
评论
在这里发布这个答案,只是为了表明需要定义的地方。我自己从这个问题中得到了帮助,但我挣扎了一段时间,不知道该把.此外,还添加了 onclick 事件的功能。aoColumnDefs
aoColumnDefs
$(document).ready(function() {
var table = $('#userTable').DataTable( {
"sAjaxSource": "/MyApp/proctoring/user/getAll",
"sAjaxDataProp": "users",
"columns": [
{ "data": "username" },
{ "data": "name" },
{ "data": "surname" },
{ "data": "status" },
{ "data": "emailAddress" },
{ "data" : "userId" }
],
"aoColumnDefs": [
{
"aTargets": [5],
"mData": "userId",
"mRender": function (data, type, full) {
return '<button href="#"' + 'id="'+ data + '">Edit</button>';
}
}
]
} );
$('#userTable tbody').on( 'click', 'button', function () {
var data = table.row( $(this).parents('tr') ).data();
console.log(data);
$('#userEditModal').modal('show');
});
} );
行动
[HttpGet]
public JsonResult Roles()
{
var data = _accountService.GetRoles().Result;
return Json(data);
}
CDN网络
<link rel="stylesheet"
href="https://cdn.datatables.net/1.13.4/css/jquery.dataTables.css" />
<script
src="https://cdn.datatables.net/1.13.4/js/jquery.dataTables.js"></script>
Html 格式
<table class="table table-hover" id="table">
<thead>
<tr>
<th>Code</th>
<th>Name</th>
<th></th>
</tr>
</thead>
</table>
DataTable 代码
<script>
$(document).ready(function(){
debugger;
$("#table").dataTable({
"ajax": {
"url": "/Admistration/Roles",
"type": "GET",
"dataSrc": "",
"datatype": "json"
},
"columns": [
{ "data": "code", "autoWidth": true },
{ "data": 'name', "autoWidth":true},
{ "render": function (data, type, full) { return "<a class='btn btn-danger' onclick=DeleteCustomer('" + full.code + "') ><i class='bi bi-trash3-fill'></i></a> <a href='#' class='btn btn-primary' onclick=DeleteCustomer('" + full.code + "'); ><i class='bi bi-pencil-square'></i></a>" } }
]
});
})
</script>
评论