如何重命名某些JSON Object属性以在HTML表

时间:2018-03-13 21:14:36

标签: javascript html css json dynatable

如果我有从数据库中获取的对象列表

[{ "id":0 ,"name":"John", "lastname":"Shell" },{ "id":1,...];

导致(dynatable插件):

 data : JSON.stringify(data)
 success: function(data,status){ 
          $('#table').dynatable({
            dataset:{
              records:data
            }
          })
        }

但我不想在第39页上显示 id 。在html中生成表时,我想将其重命名为 securityNumber 而不更改实际的JSON

      <table id="tabela">
        <thead>
           <th>id</th>
           <th>name<th>
           <th>last_name<th>
        </thead>
      </table>

我无法替换id,因为插件通过JSON对象的属性标识列名称

我尝试过不同的插件,我使用了dynatable(已在文档中搜索过),但我对其他解决方案持开放态度。

我该如何处理?

1 个答案:

答案 0 :(得分:1)

您可以在textTransform上添加自定义格式设置方法,并在那里写下列名称的映射。查看文档here

这种方法的一个例子可能是

dynatable.utility.textTransform.customColumnName = function(text) {
  if (text) === 'id' return 'security_number'
  return text
}

或者,您可以使用Array.map转换数组而不更改源JSON,例如:

...
records: data.map((item) => {
  return {
    security_number: item.id,
    name: item.name,
    last_name: item.last_name
  }
}),
...