ITPub博客

首页 > 应用开发 > IT综合 > 使用postgresql作为 bootstrap tree view 后端

使用postgresql作为 bootstrap tree view 后端

IT综合 作者:suzhouclark 时间:2017-07-28 14:30:12 0 删除 编辑

Building a nested hierarchy of JSON in a relational DB



https://lakshminp.com/building-nested-hierarchy-json-relational-db


其中,不要coalesce

点击(此处)折叠或打开

  1. SELECT id, name, COALESCE(get_children(id), '[]') as children from genres

  2. 改成

  3. SELECT id as text, get_children(id) as nodes from genres






另外,去除JSON中null的代码:

点击(此处)折叠或打开

  1. function removeNulls(obj){
  2.   var isArray = obj instanceof Array;
  3.   for (var k in obj){
  4.     if (obj[k]===null) isArray ? obj.splice(k,1) : delete obj[k];
  5.     else if (typeof obj[k]=="object") removeNulls(obj[k]);
  6.   }
  7. }

  8. removeNulls(root);

  9.     return JSON.stringify(root,null,2);






来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/9697/viewspace-2142727/ ,如需转载,请注明出处,否则将追究法律责任。

上一篇: 修改openstack dhcp
请登录后发表评论 登录
全部评论

注册时间:2010-03-26

  • 博文量
    124
  • 访问量
    335895