12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697 |
- <template>
- <el-cascader
- :options="options"
- :props="{ multiple: true }"
- separator=":"
- clearable
- filterable
- v-model="newValue"
- @change="onChange"
- ></el-cascader>
- </template>
- <script>
- export default {
- name: "cl-menu-perms",
- props: {
- value: [String, Number, Array]
- },
- data() {
- return {
- options: [],
- newValue: []
- };
- },
- watch: {
- value() {
- this.parse();
- }
- },
- created() {
- let options = [];
- let list = [];
- const flat = (obj) => {
- for (let i in obj) {
- let { permission } = obj[i];
- if (permission) {
- list = [...list, Object.values(permission)].flat();
- } else {
- flat(obj[i]);
- }
- }
- };
- flat(this.$service);
- list.filter((e) => e.includes(":"))
- .map((e) => e.split(":"))
- .forEach((arr) => {
- const col = (i, d) => {
- let key = arr[i];
- let index = d.findIndex((e) => e.label == key);
- if (index >= 0) {
- col(i + 1, d[index].children);
- } else {
- let isLast = i == arr.length - 1;
- d.push({
- label: key,
- value: key,
- children: isLast ? null : []
- });
- if (!isLast) {
- col(i + 1, d[d.length - 1].children || []);
- }
- }
- };
- col(0, options);
- });
- this.options = options;
- },
- mounted() {
- this.parse();
- },
- methods: {
- parse() {
- this.newValue = this.value ? this.value.split(",").map((e) => e.split(":")) : [];
- },
- onChange(row) {
- this.$emit("input", row.map((e) => e.join(":")).join(","));
- }
- }
- };
- </script>
|