tree list 互相转换

爱被打了一巴掌 2021-08-13 20:05 528阅读 0赞

本人没有亲测过,有问题的话会重新编辑文档,这篇文章里的代码是积分下载换来的,如果涉及侵权联系我。。。。。看到我这边博客的人可以免积分了。。。。。。

  1. import _ from 'lodash'
  2. export function list2JsonTree(data, options) {
  3. var mainKey = options && options.mainKey || 'id'
  4. var parentKey = options && options.parentKey || 'parentId'
  5. var sortKey = options && options.sortKey || 'seq'
  6. var childListKey = options && options.childListKey || 'children'
  7. var deleteParentKey = options && options.deleteParentKey || true
  8. var deleteSortKey = options && options.deleteSortKey || true
  9. var idItemMap = {}
  10. _.each(data, one => {
  11. // 初始化一下children字段
  12. one[childListKey] = []
  13. idItemMap[one[mainKey]] = one
  14. })
  15. var res = []
  16. _.each(data, one => {
  17. if (one[parentKey] && idItemMap[one[parentKey]]) {
  18. idItemMap[one[parentKey]][childListKey].push(one)
  19. if(deleteParentKey) {
  20. delete one[parentKey]
  21. }
  22. } else {
  23. res.push(one)
  24. if(deleteParentKey) {
  25. delete one[parentKey]
  26. }
  27. }
  28. })
  29. // 排序
  30. return sortJsonTree(res, sortKey, deleteSortKey)
  31. }
  32. export function sortJsonTree(data, key, deleteKey) {
  33. let res = _.sortBy(data, key)
  34. if(deleteKey){
  35. _.each(res, function(one){
  36. delete one[key]
  37. })
  38. }
  39. _.each(data, function (one) {
  40. if (one.children) {
  41. one.children = sortJsonTree(one.children, key, deleteKey)
  42. }
  43. })
  44. return res
  45. }
  46. export function jsonTree2list(data, options, parentId) {
  47. var mainKey = options && options.mainKey || 'id'
  48. var parentKey = options && options.parentKey || 'parentId'
  49. var sortKey = options && options.sortKey || 'seq'
  50. var childListKey = options && options.childListKey || 'children'
  51. var deleteChildListKey = options && options.deleteChildListKey || true
  52. let res = []
  53. _.each(data, function (one, index) {
  54. if (parentId) {
  55. one[parentKey] = parentId
  56. }
  57. res.push(one)
  58. one[sortKey] = index + 1
  59. if (one[childListKey]) {
  60. res = res.concat(jsonTree2list(one[childListKey], options, one[mainKey]))
  61. }
  62. if(deleteChildListKey) {
  63. delete one[childListKey]
  64. }
  65. })
  66. return res
  67. }
  68. // let testData = [
  69. // {id: 1, parentId: 0, name: '1', seq: '2'},
  70. // {id: 2, parentId: 0, name: '2', seq: '4'},
  71. // {id: 3, parentId: 0, name: '3', seq: '3'},
  72. // {id: 4, parentId: 0, name: '4', seq: '1'},
  73. // {id: 5, parentId: 1, name: '11', seq: '2'},
  74. // {id: 6, parentId: 1, name: '12', seq: '3'},
  75. // {id: 7, parentId: 1, name: '13', seq: '1'},
  76. // {id: 8, parentId: 2, name: '21', seq: '2'},
  77. // {id: 9, parentId: 2, name: '22', seq: '1'},
  78. // ]
  79. // let a = JSON.stringify(list2JsonTree(testData), 0, 2)
  80. // console.log(a)
  81. // let b = JSON.stringify(jsonTree2list(JSON.parse(a)),0,2)
  82. // console.log(b)-+

发表评论

表情:
评论列表 (有 0 条评论,528人围观)

还没有评论,来说两句吧...

相关阅读

    相关 tree list 互相转换

    本人没有亲测过,有问题的话会重新编辑文档,这篇文章里的代码是积分下载换来的,如果涉及侵权联系我。。。。。看到我这边博客的人可以免积分了。。。。。。 import _