bootstrap-table-treegrid.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /**
  2. * @author: YL
  3. * @update: zhixin wen <wenzhixin2010@gmail.com>
  4. */
  5. const Utils = $.fn.bootstrapTable.utils
  6. Utils.extend($.fn.bootstrapTable.defaults, {
  7. treeEnable: false,
  8. treeShowField: null,
  9. idField: 'id',
  10. parentIdField: 'pid',
  11. rootParentId: null
  12. })
  13. $.BootstrapTable = class extends $.BootstrapTable {
  14. init (...args) {
  15. this._rowStyle = this.options.rowStyle
  16. super.init(...args)
  17. }
  18. initHeader (...args) {
  19. super.initHeader(...args)
  20. const treeShowField = this.options.treeShowField
  21. if (treeShowField) {
  22. for (const field of this.header.fields) {
  23. if (treeShowField === field) {
  24. this.treeEnable = true
  25. break
  26. }
  27. }
  28. }
  29. }
  30. initBody (...args) {
  31. if (this.treeEnable) {
  32. this.options.virtualScroll = false
  33. }
  34. super.initBody(...args)
  35. }
  36. initTr (item, idx, data, parentDom) {
  37. const nodes = data.filter(it => item[this.options.idField] === it[this.options.parentIdField])
  38. parentDom.append(super.initRow(item, idx, data, parentDom))
  39. // init sub node
  40. const len = nodes.length - 1
  41. for (let i = 0; i <= len; i++) {
  42. const node = nodes[i]
  43. const defaultItem = Utils.extend(true, {}, item)
  44. node._level = defaultItem._level + 1
  45. node._parent = defaultItem
  46. if (i === len) {
  47. node._last = 1
  48. }
  49. // jquery.treegrid.js
  50. this.options.rowStyle = (item, idx) => {
  51. const res = this._rowStyle(item, idx)
  52. const id = item[this.options.idField] ? item[this.options.idField] : 0
  53. const pid = item[this.options.parentIdField] ? item[this.options.parentIdField] : 0
  54. res.classes = [
  55. res.classes || '',
  56. `treegrid-${id}`,
  57. `treegrid-parent-${pid}`
  58. ].join(' ')
  59. return res
  60. }
  61. this.initTr(node, $.inArray(node, data), data, parentDom)
  62. }
  63. }
  64. initRow (item, idx, data, parentDom) {
  65. if (this.treeEnable) {
  66. if (
  67. this.options.rootParentId === item[this.options.parentIdField] ||
  68. !item[this.options.parentIdField]
  69. ) {
  70. if (item._level === undefined) {
  71. item._level = 0
  72. }
  73. // jquery.treegrid.js
  74. this.options.rowStyle = (item, idx) => {
  75. const res = this._rowStyle(item, idx)
  76. const x = item[this.options.idField] ? item[this.options.idField] : 0
  77. res.classes = [
  78. res.classes || '',
  79. `treegrid-${x}`
  80. ].join(' ')
  81. return res
  82. }
  83. this.initTr(item, idx, data, parentDom)
  84. return true
  85. }
  86. return false
  87. }
  88. return super.initRow(item, idx, data, parentDom)
  89. }
  90. destroy (...args) {
  91. super.destroy(...args)
  92. this.options.rowStyle = this._rowStyle
  93. }
  94. }