GroupSettings.vue 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. <template>
  2. <div id="main">
  3. <el-tree :expand-on-click-node="false" :data="data" :props="defaultProps" :defaultExpandAll="true" :class="node">
  4. <template #default="{ node, data }">
  5. <div>
  6. <span v-if="data.id != -1"> {{ data.label }}</span>
  7. <el-input v-if="data.id == -1" v-model="data.label" @blur="onInputBlur(data)"></el-input>
  8. <el-button v-if="data.id != 0" @click="del(node, data)" size="small" type="danger" circle> -
  9. </el-button>
  10. <el-button v-if="data.id != 0" @click="add(data)" size="small" type="primary" circle> + </el-button>
  11. </div>
  12. </template>
  13. </el-tree>
  14. <el-button @click="addRoot">{{ lang.add_group }}</el-button>
  15. </div>
  16. </template>
  17. <script setup>
  18. import { reactive, ref, getCurrentInstance } from 'vue'
  19. import lang from '../i18n/i18n';
  20. const data = reactive([])
  21. const app = getCurrentInstance()
  22. const $http = app.appContext.config.globalProperties.$http
  23. $http.get('/api/group').then(res => {
  24. data.push(...res.data)
  25. })
  26. const del = function (node, data) {
  27. if (data.id != -1) {
  28. this.$axios.post("/api/group/del", { "id": data.id }).then(res => {
  29. if (res.errorNo != 0) {
  30. ElMessage({
  31. message: res.errorMsg,
  32. type: 'error',
  33. })
  34. } else {
  35. const pc = node.parent.childNodes
  36. for (let i = 0; i < pc.length; i++) {
  37. if (pc[i].id == node.id) {
  38. pc.splice(i, 1)
  39. return
  40. }
  41. }
  42. }
  43. })
  44. } else {
  45. const pc = node.parent.childNodes
  46. for (let i = 0; i < pc.length; i++) {
  47. if (pc[i].id == node.id) {
  48. pc.splice(i, 1)
  49. return
  50. }
  51. }
  52. }
  53. }
  54. const add = function (item) {
  55. if (item.children == null) {
  56. item.children = []
  57. }
  58. item.children.push({
  59. "children": [],
  60. "label": "",
  61. "id": "-1",
  62. "parent_id": item.id
  63. })
  64. }
  65. const addRoot = function () {
  66. data.push({
  67. "children": [],
  68. "label": "",
  69. "id": "-1",
  70. "parent_id": 0
  71. })
  72. }
  73. const onInputBlur = function (item) {
  74. if (item.label != "") {
  75. this.$axios.post("/api/group/add", { "name": item.label, "parent_id": item.parent_id }).then(res => {
  76. if (res.errorNo != 0) {
  77. ElMessage({
  78. message: res.errorMsg,
  79. type: 'error',
  80. })
  81. } else {
  82. this.$axios.get('/api/group').then(res => {
  83. data.splice(0, data.length)
  84. data.push(...res.data)
  85. })
  86. }
  87. })
  88. }
  89. }
  90. </script>