ny
昨天 282fbc6488f4e8ceb5fda759f963ee88fbf7b999
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
<script lang="ts" setup>
import { reactive, toRefs } from 'vue';
 
import { BasicModal, useModalInner } from '@jnpf/ui/modal';
 
interface State {
  innerValue: string;
  separator: string;
}
 
const emit = defineEmits(['register', 'confirm']);
const state = reactive<State>({
  innerValue: '',
  separator: '|',
});
const { innerValue } = toRefs(state);
const [registerModal, { closeModal }] = useModalInner(init);
 
function init(data) {
  state.innerValue = '';
  if (!data.options.length) return;
  const loop = (list, parentId?) => {
    for (const e of list) {
      let parent = '';
      if (parentId) parent = state.separator + parentId;
      state.innerValue += `${e.fullName + state.separator + e.id + parent}\n`;
      if (e?.children && Array.isArray(e.children)) {
        loop(e.children, e.id);
      }
    }
  };
  loop(data.options);
}
async function handleSubmit() {
  const options: any[] = [];
  if (!state.innerValue) return emit('confirm', options);
  const list = state.innerValue.split('\n');
  for (let i = 0; i < list.length; i++) {
    const e = list[i]?.trim();
    if (!e || !e.includes(state.separator)) continue;
    const row = e.split(state.separator) || [];
    if (row.length < 2) continue;
    if (isRepeat(options, row[1])) continue;
    if (row.length === 2) {
      options.push({ fullName: row[0], id: row[1], children: [] });
      continue;
    }
    if (row.length === 3) {
      const getParentId = (list, row) => {
        const parentId = row[row.length - 1];
        for (const e of list) {
          if (e.children && e.children.length) getParentId(e.children, row);
          if (e.id == parentId) {
            e.children.push({ fullName: row[0], id: row[1], children: [] });
            break;
          }
        }
      };
      getParentId(options, row);
    }
  }
  emit('confirm', options);
  closeModal();
}
function isRepeat(options, id) {
  let boo = false;
  const loop = (list) => {
    for (const e of list) {
      if (e.id == id) {
        boo = true;
        break;
      }
      if (e.children && Array.isArray(e.children)) loop(e.children);
    }
  };
  loop(options);
  return boo;
}
</script>
<template>
  <BasicModal v-bind="$attrs" @register="registerModal" title="批量编辑" show-ok-btn @ok="handleSubmit" :width="700">
    <a-alert message="注意:每行对应一个选项;选项名、选项值和父级选项值之间用英文 | 隔开。格式如下:" type="warning" show-icon />
    <div class="demo-box">
      <div class="demo-box-cell">
        <p class="w-[260px]">根节点格式:选项名|选项值</p>
        <p>输入示例:<span>选项一|选项一的值</span></p>
      </div>
      <div class="demo-box-cell">
        <p class="w-[260px]">子节点格式:选项名|选项值|父级选项值</p>
        <p>输入示例:<span>选项一|选项一的值|选项一父级的值</span></p>
      </div>
    </div>
    <jnpf-textarea v-model:value="innerValue" :rows="12" class="!mb-[20px] !leading-[30px]" />
  </BasicModal>
</template>
<style lang="scss" scoped>
.demo-box {
  margin: 10px 0;
  border: 1px solid var(--border-color-base);
 
  .demo-box-cell {
    padding: 0 11px;
    line-height: 36px;
    border-bottom: 1px solid var(--border-color-base);
 
    &:last-child {
      border-bottom: 0;
    }
 
    p,
    span {
      display: inline-block;
    }
 
    span {
      color: var(--primary-color);
    }
  }
}
</style>