我有一个js数据表,上面有json内容
$('#data').DataTable( {
data: data,
columns : [
{ data: "number" },
{ data: "firstname" },
{ data: "lastname" }
]
});
数据表本身按预期填充。但是,我想在表的末尾添加另一列,这并不是我得到的数据的一部分。假设我要在此处添加按钮或链接。
是否可以快速添加另一列并使用数据(例如数字)?
表所需的重新存储
Number | Firstname | Lastname | Action
001 | John | Doe | link to ...page?nr=001
023 | Jane | Doe | link to ...page?nr=023
答案 0 :(得分:0)
此代码可以完成您的工作,
$('#data').DataTable( {
data: data,
columns : [
{ data: "number" },
{ data: "firstname" },
{ data: "lastname" },
{
"data": null,
"render": function ( data, type, row, meta ) {
return '<a href="'+data['number']+'">View Detail</a>'; }
},
]
});
答案 1 :(得分:0)
这基于找到的示例here
function Person( firstname, lastname, age ) {
this._firstname = firstname;
this._lastname = lastname;
this._age = age;
this.firstname = function () {
return this._firstname;
};
this.lastname = function () {
return this._lastname;
};
this.age = function () {
return this._age;
};
this.link = function () {
return '<a href="linkto...'+this._age+'">linkto...'+this._age+'</a>';
};
}
$(document).ready(function() {
var table = $('#example').DataTable({
columns: [
{ data: null, render: 'firstname' },
{ data: null, render: 'lastname' },
{ data: null, render: 'age' },
{ data: null, render: 'link' }
]
});
table.row.add( new Person( 'Airi', 'Satou', 33 ) );
table.row.add( new Person( 'Angelica', 'Ramos', 47 ) );
table.row.add( new Person( 'Ashton', 'Cox', 66 ) );
table.draw();
} );
<link href="https://cdn.datatables.net/1.10.19/css/jquery.dataTables.min.css" rel="stylesheet"/>
<script src="https://code.jquery.com/jquery-3.3.1.js"></script>
<script src="https://cdn.datatables.net/1.10.19/js/jquery.dataTables.min.js"></script>
<table id="example" class="display" style="width:100%">
</table>