perms.vue 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. <template>
  2. <div class="cl-role-perms" v-loading="loading">
  3. <p v-if="title">{{ title }}</p>
  4. <el-input placeholder="输入关键字进行过滤" v-model="keyword" size="small"> </el-input>
  5. <div class="scroller">
  6. <el-tree
  7. :data="list"
  8. :props="props"
  9. :default-checked-keys="checked"
  10. :filter-node-method="filterNode"
  11. highlight-current
  12. node-key="id"
  13. show-checkbox
  14. ref="tree"
  15. @check-change="save"
  16. >
  17. </el-tree>
  18. </div>
  19. </div>
  20. </template>
  21. <script>
  22. import { deepTree } from "cl-admin/utils";
  23. export default {
  24. name: "cl-role-perms",
  25. props: {
  26. value: Array,
  27. title: String
  28. },
  29. data() {
  30. return {
  31. list: [],
  32. checked: [],
  33. keyword: "",
  34. props: {
  35. label: "name",
  36. children: "children"
  37. },
  38. loading: false
  39. };
  40. },
  41. watch: {
  42. keyword(val) {
  43. this.$refs["tree"].filter(val);
  44. },
  45. value(val) {
  46. this.refreshTree(val);
  47. }
  48. },
  49. mounted() {
  50. this.refresh();
  51. },
  52. methods: {
  53. refreshTree(val) {
  54. if (!val) {
  55. this.checked = [];
  56. }
  57. let ids = [];
  58. // 处理半选状态
  59. let fn = (list) => {
  60. list.forEach((e) => {
  61. if (e.children) {
  62. fn(e.children);
  63. } else {
  64. ids.push(Number(e.id));
  65. }
  66. });
  67. };
  68. fn(this.list);
  69. this.checked = ids.filter((id) => (val || []).includes(id));
  70. },
  71. refresh() {
  72. this.$service.system.menu
  73. .list()
  74. .then((res) => {
  75. this.list = deepTree(res);
  76. this.refreshTree(this.value);
  77. })
  78. .catch((err) => {
  79. this.$message.error(err);
  80. });
  81. },
  82. filterNode(val, data) {
  83. if (!val) return true;
  84. return data.name.includes(val);
  85. },
  86. save() {
  87. const tree = this.$refs["tree"];
  88. // 选中的节点
  89. const checked = tree.getCheckedKeys();
  90. // 半选中的节点
  91. const halfChecked = tree.getHalfCheckedKeys();
  92. this.$emit("input", [...checked, ...halfChecked]);
  93. }
  94. }
  95. };
  96. </script>
  97. <style lang="scss" scoped>
  98. .scroller {
  99. border: 1px solid #dcdfe6;
  100. margin-top: 5px;
  101. border-radius: 3px;
  102. max-height: 200px;
  103. box-sizing: border-box;
  104. overflow-x: hidden;
  105. padding: 5px 0;
  106. }
  107. </style>