admin管理员组文章数量:1313972
I'm have a js datatable with json content in front of me
$('#data').DataTable( {
data: data,
columns : [
{ data: "number" },
{ data: "firstname" },
{ data: "lastname" }
]
});
the datatable itself fills itself as intended. However, I'd like to add another column at the end of the table which is not quite part of the data I get. Let's say I want to add a button or a link there.
is there a quick way to add another column and use the data (eg. number)?
Desired resault for the table:
Number | Firstname | Lastname | Action
001 | John | Doe | link to ...page?nr=001
023 | Jane | Doe | link to ...page?nr=023
I'm have a js datatable with json content in front of me
$('#data').DataTable( {
data: data,
columns : [
{ data: "number" },
{ data: "firstname" },
{ data: "lastname" }
]
});
the datatable itself fills itself as intended. However, I'd like to add another column at the end of the table which is not quite part of the data I get. Let's say I want to add a button or a link there.
is there a quick way to add another column and use the data (eg. number)?
Desired resault for the table:
Number | Firstname | Lastname | Action
001 | John | Doe | link to ...page?nr=001
023 | Jane | Doe | link to ...page?nr=023
Share
Improve this question
asked Jan 3, 2019 at 16:43
lechneriolechnerio
9803 gold badges21 silver badges55 bronze badges
1
- stackoverflow./questions/38111245/… – hawkstrider Commented Jan 3, 2019 at 16:56
2 Answers
Reset to default 4This code might do your work,
$('#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>'; }
},
]
});
This is based on example found 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/1.10.19/css/jquery.dataTables.min.css" rel="stylesheet"/>
<script src="https://code.jquery./jquery-3.3.1.js"></script>
<script src="https://cdn.datatables/1.10.19/js/jquery.dataTables.min.js"></script>
<table id="example" class="display" style="width:100%">
</table>
本文标签: javascriptdynamically add a column to DatatablesStack Overflow
版权声明:本文标题:javascript - dynamically add a column to Datatables - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741935004a2405801.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论