WXL (wul)
2 天以前 e0909a604449641ebcc6ed15495e6f28515f437c
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
<template>
  <div class="ordered-checkbox-container">
    <!-- 横向排列的多选框组 -->
    <el-checkbox-group
      v-model="checkedValues"
      class="horizontal-checkbox-group"
    >
      <el-checkbox
        v-for="option in options"
        :key="getValue(option)"
        :label="getValue(option)"
      >
        {{ getLabel(option) }}
      </el-checkbox>
    </el-checkbox-group>
 
    <!-- 选中顺序展示区域 -->
    <div v-if="selectedOrder.length > 0" class="selection-order-display">
      <span class="order-label">服务执行顺序:</span>
      <span
        v-for="(item, index) in selectedOrder"
        :key="item.value"
        class="order-item"
      >
        {{ getSelectedIndex(index) }}.{{ getLabelByValue(item.value) }}
        <el-tooltip content="设置补偿时间" placement="top">
          <el-input-number
            v-model="item.compensateTime"
            :min="0"
            :max="60"
            size="mini"
            controls-position="right"
            class="compensate-time-input"
            @change="handleCompensateTimeChange(item.value, $event)"
          />
        </el-tooltip>
        <span v-if="index < selectedOrder.length - 1">、</span>
      </span>
    </div>
    <div v-else class="selection-order-display">
      <span class="order-label">暂无选中项</span>
    </div>
  </div>
</template>
 
<script>
export default {
  name: "OrderedCheckboxGroup",
  props: {
    options: {
        type: Array,
        default: () => [],
        validator: (value) => {
            return Array.isArray(value);
        }
    },
    value: {
        type: Array,
        default: () => [],
        validator: (value) => {
            // 允许空数组,但如果是非数组值则发出警告
            if (!Array.isArray(value) && value !== null && value !== undefined) {
                console.warn('value prop should be an array, received:', typeof value);
                return false;
            }
            return true;
        }
    },
    initialselectedOrder: {
      type: Array,
      default: () => [],
    },
    valueKey: {
      type: String,
      default: "value",
    },
    labelKey: {
      type: String,
      default: "label",
    },
    // 新增:默认补偿时间
    defaultCompensateTime: {
      type: Number,
      default: 0,
    },
  },
  data() {
    return {
      checkedValues: [],
      selectedOrder: [], // 现在格式为 [{value, compensateTime}]
    };
  },
  watch: {
    value: {
      immediate: true,
      handler(newVal) {
        // 首先确保newVal是数组,如果不是则转换为空数组
        if (!Array.isArray(newVal)) {
          console.warn(
            "Expected array for value prop, received:",
            typeof newVal
          );
          this.checkedValues = [];
          this.selectedOrder = [];
          return;
        }
 
        if (newVal.length > 0 && typeof newVal[0] === "object") {
          console.log(this.selectedOrder, "111");
          // 1. 传入的是对象数组 [{ sort, preachform, compensateTime }]
          this.checkedValues = newVal.map((item) => item.preachform);
          this.selectedOrder = newVal.map((item) => ({
            value: item.preachform,
            compensateTime: item.hasOwnProperty("compensateTime")
              ? item.compensateTime
              : this.defaultCompensateTime,
          }));
        } else {
          // 2. 传入的是字符串数组
          if (JSON.stringify(newVal) !== JSON.stringify(this.checkedValues)) {
            this.checkedValues = [...newVal];
            console.log(this.selectedOrder, "222");
 
            const newOrder = [];
            newVal.forEach((value) => {
              const existingItem = this.selectedOrder.find(
                (item) => item.value === value
              );
              if (existingItem) {
                newOrder.push(existingItem);
              } else {
                // 修复hasOwnProperty方法调用
                const existingCompensateTime = this.hasOwnProperty(value);
                newOrder.push({
                  value,
                  compensateTime:
                    existingCompensateTime !== false
                      ? existingCompensateTime
                      : this.defaultCompensateTime,
                });
              }
            });
            this.selectedOrder = newOrder;
          }
        }
      },
      deep: true,
    },
    checkedValues(newVal, oldVal) {
      console.log(this.selectedOrder, "333");
      // 处理选中项的变化
      const added = newVal.filter((item) => !oldVal.includes(item));
      const removed = oldVal.filter((item) => !newVal.includes(item));
 
      added.forEach((value) => {
        if (!this.selectedOrder.find((item) => item.value === value)) {
          this.selectedOrder.push({
            value,
            compensateTime: this.defaultCompensateTime,
          });
        }
      });
 
      removed.forEach((value) => {
        const index = this.selectedOrder.findIndex(
          (item) => item.value === value
        );
        if (index > -1) {
          this.selectedOrder.splice(index, 1);
        }
      });
 
      // 更新父组件的 v-model 绑定值(选中值数组)
      this.$emit("input", [...newVal]);
      // 触发 change 事件,传递完整的业务数据
      this.emitChangeEvent();
    },
  },
  methods: {
    getValue(option) {
      return typeof option === "object" ? option[this.valueKey] : option;
    },
    getLabel(option) {
      return typeof option === "object" ? option[this.labelKey] : option;
    },
    getLabelByValue(value) {
      const option = this.options.find((opt) => this.getValue(opt) === value);
      return option ? this.getLabel(option) : value;
    },
    getSelectedIndex(index) {
      if (index < 20) {
        return String.fromCharCode(0x2460 + index);
      } else {
        return `(${index + 1})`;
      }
    },
    // 处理补偿时间变化
    handleCompensateTimeChange(value, newTime) {
      const item = this.selectedOrder.find((item) => item.value === value);
      if (item) {
        item.compensateTime = newTime;
        // 补偿时间变化时,只触发 change 事件,不触动 v-model
        this.emitChangeEvent();
      }
    },
    hasOwnProperty(patfrom) {
      if (!this.initialselectedOrder || !Array.isArray(this.initialselectedOrder)) {
        return false;
    }
 
    // 使用find方法查找匹配的对象
    const foundObject = this.initialselectedOrder.find(
        (item) => item.preachform === patfrom
    );
 
    // 如果找到对象,返回其compensateTime;否则返回false
    return foundObject ? foundObject.compensateTime : false;
    },
    // 发射变化事件
    emitChangeEvent() {
      // 转换数据格式为父组件需要的格式
      const outputData = this.selectedOrder.map((item, index) => ({
        sort: index + 1,
        preachform: item.value,
        compensateTime: item.compensateTime,
      }));
      this.$emit("change", outputData); // 发射 change 事件,传递完整数据
    },
    // 获取当前选择顺序和补偿时间
    getSelectionOrder() {
      return this.selectedOrder.map((item, index) => ({
        sort: index + 1,
        preachform: item.value,
        compensateTime: item.compensateTime,
      }));
    },
    // 设置选择顺序和补偿时间
    setSelectionOrder(orderedValues) {
      this.selectedOrder = orderedValues.map((item) => ({
        value: item.preachform,
        compensateTime: item.compensateTime || this.defaultCompensateTime,
      }));
 
      this.checkedValues = orderedValues.map((item) => item.preachform);
      this.emitChangeEvent();
    },
  },
};
</script>
 
<style scoped>
.ordered-checkbox-container {
  display: flex;
  flex-direction: column;
  gap: 16px;
}
 
.horizontal-checkbox-group {
  display: flex;
  flex-wrap: wrap;
  gap: 20px;
}
 
.selection-order-display {
  padding: 12px;
  background-color: #f5f7fa;
  border-radius: 4px;
  border: 1px solid #ebeef5;
  display: flex;
  flex-wrap: wrap;
  align-items: center;
}
 
.order-label {
  font-weight: bold;
  color: #606266;
  margin-right: 8px;
}
 
.order-item {
  color: #409eff;
  font-weight: 500;
  display: inline-flex;
  align-items: center;
  margin-right: 8px;
}
 
.compensate-time-input {
  width: 90px;
  margin-left: 8px;
}
 
/* 响应式设计:小屏幕时换行 */
@media (max-width: 768px) {
  .horizontal-checkbox-group {
    gap: 12px;
  }
 
  .selection-order-display {
    padding: 8px;
    flex-direction: column;
    align-items: flex-start;
  }
 
  .order-item {
    margin-bottom: 8px;
  }
}
</style>