GroupSettings.vue 2.6 KB

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