[英]jQuery Datatable reload data - json to columns
I have table that first time is server - side rendered. 我有表第一次是服务器-端呈现。 I want on some click event refresh data in table.
我想要一些单击事件刷新表中的数据。
Problem is how to "bind" specific json object propertie to specific column. 问题是如何将特定的json对象属性“绑定”到特定的列。 I receive json object with array of objects where only some of properties are interesting me.
我收到带有对象数组的json对象,其中只有一些属性很有趣。
I want 我想要
Table column -- json object propertie 表列-JSON对象属性
ID -- id ID-ID
Name -- name + surname 名字-名字+姓
Cooperation -- scCooperationCollection 合作-scCooperationCollection
Skills -- scSkillsCollection 技能-scSkillsCollection
Experience -- workExperience 经验-工作经验
HTML table: http://pastebin.com/b5yRWsGe HTML表格: http : //pastebin.com/b5yRWsGe
JS reload table: http://pastebin.com/GzS8tpV6 JS重新加载表: http : //pastebin.com/GzS8tpV6
JSON example : http://pastebin.com/AyBSrSui JSON范例: http : //pastebin.com/AyBSrSui
See columns.data
or columns.render
options on how to bind source data to table columns or produce custom content for a cell. 有关如何将源数据绑定到表列或如何为单元格生成自定义内容,请参见
columns.data
或columns.render
选项。
You can access source data properties using dotted notation in columns.data
or even join arrays with []
notation. 您可以在
columns.data
使用点分符号来访问源数据属性,甚至可以使用[]
符号连接数组。 For more complex data rendering like joining two fields, use columns.render
instead. 对于更复杂的数据渲染(如连接两个字段),请改用
columns.render
。
For example: 例如:
var table = $('#example').DataTable({
ajax: {
url: 'https://api.myjson.com/bins/3x4ql',
dataSrc: 'aaData'
},
columns: [
{ data: "id" },
{
data: null,
render: function(data, type, full, meta){
return full['name'] + ' ' + full['surname'];
}
},
{ data: "scCooperationCollection[,].scFields.name" },
{ data: "scSkillsCollection[,].scFields.name" },
{ data: "workExperience" }
]
});
See this jsFiddle for code and demonstration. 有关代码和演示,请参见此jsFiddle 。
You can navigate through the desired properties in returned json object and tie up same to your table something like below: 您可以在返回的json对象中浏览所需的属性,并将其与表绑定起来,如下所示:
table.on( 'xhr', function () {
var json = table.ajax.json();
var obj = JSON.parse(json)
var tableJson = {"ID":obj.aaData[0].id};
console.log(json);
} );
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.