nodeTree.vue 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. <template>
  2. <div class="nodeTree">
  3. <div class="nodeTree_search">
  4. <el-input size="small" placeholder="输入关键字进行过滤" clearable v-model="filterText"></el-input>
  5. </div>
  6. <div class="nodeTree_tree">
  7. <el-scrollbar style="height: 100%" :horizontal="false">
  8. <el-tree class="filter-tree" :expand-on-click-node="false" :data="treedata" :props="defaultProps" node-key="id" :default-expanded-keys="defaultKeys" :filter-node-method="filterNode" ref="tree" @node-click="treeNodeClick">
  9. </el-tree>
  10. </el-scrollbar>
  11. </div>
  12. </div>
  13. </template>
  14. <script>
  15. import { listToTree } from '@/utils/validate'
  16. import { getQueryData } from '../utils/index'
  17. export default {
  18. name: 'NodeTree',
  19. data () {
  20. return {
  21. filterText: '',
  22. treedata: [],
  23. treedataCopy: [],
  24. nodeParents: [],
  25. defaultKeys: [],
  26. defaultProps: {
  27. children: 'children',
  28. label: 'name'
  29. },
  30. }
  31. },
  32. watch: {
  33. filterText (val) {
  34. this.$refs.tree.filter(val);
  35. },
  36. },
  37. mounted () {
  38. this.getTreeData()
  39. },
  40. methods: {
  41. filterNode (value, data) {
  42. if (!value) return true;
  43. return data.name.indexOf(value) !== -1;
  44. },
  45. treeNodeClick (item) {
  46. const { id } = item
  47. const ncids = [0, ...this.defaultKeys]
  48. this.nodeParents = []
  49. if (!ncids.includes(id)) {
  50. this.formatUpdata(item)
  51. const results = [item, ...this.nodeParents].reverse()
  52. let nodeTxt = ''
  53. results.map((item, index) => nodeTxt = nodeTxt + (index != 0 ? '-' : '') + item.name)
  54. sessionStorage.setItem('nodeTreeTxt', nodeTxt)
  55. this.$emit('treeNodeClick', item)
  56. } else {
  57. this.$message.error('请选择算法模板后再操作')
  58. }
  59. },
  60. formatUpdata (item) {
  61. const nitem = { ...item }
  62. this.treedataCopy.forEach(citem => {
  63. if (nitem.superiorid == citem.id) {
  64. this.nodeParents.push(citem)
  65. this.formatUpdata(citem)
  66. }
  67. })
  68. },
  69. async getTreeData () {
  70. const { code, returnData } = await getQueryData(20, { 1: 1 })
  71. if (code == 0 && returnData?.length) {
  72. const result = listToTree(_.cloneDeep(returnData), 'superiorid', 'id')
  73. const resall = [{
  74. name: '全部',
  75. id: 0,
  76. superiorid: -1,
  77. children: result
  78. }]
  79. const check = [...returnData].filter(item => item.code == 'jisuan' || item.code == 'ss' || item.code == 'sc')
  80. const checksid = []
  81. check.map(({ id }) => checksid.push(id))
  82. this.defaultKeys = checksid
  83. this.treedataCopy = _.cloneDeep(returnData)
  84. this.treedata = resall
  85. }
  86. }
  87. }
  88. }
  89. </script>
  90. <style lang="scss" scoped>
  91. ::v-deep .nodeTree {
  92. height: 100vh;
  93. &_tree {
  94. margin-top: 20px;
  95. }
  96. }
  97. </style>