WXL (wul)
23 小时以前 38d3a2b23df5d7fea47d3260cfc19e803dc40863
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
<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="(value, index) in selectedOrder"
        :key="value"
        class="order-item"
      >
        {{ getSelectedIndex(index) }}.{{ getLabelByValue(value) }}
        <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: () => [],
    },
    value: {
      type: Array,
      default: () => [],
    },
    valueKey: {
      type: String,
      default: "value",
    },
    labelKey: {
      type: String,
      default: "label",
    },
  },
  data() {
    return {
      checkedValues: [],
      selectedOrder: [],
    };
  },
  watch: {
    value: {
      immediate: true,
      handler(newVal) {
        if (JSON.stringify(newVal) !== JSON.stringify(this.checkedValues)) {
          this.checkedValues = [...newVal];
          this.selectedOrder = [...newVal];
        }
      },
    },
    checkedValues(newVal, oldVal) {
      const added = newVal.filter((item) => !oldVal.includes(item));
      const removed = oldVal.filter((item) => !newVal.includes(item));
 
      added.forEach((value) => {
        if (!this.selectedOrder.includes(value)) {
          this.selectedOrder.push(value);
        }
      });
 
      removed.forEach((value) => {
        const index = this.selectedOrder.indexOf(value);
        if (index > -1) {
          this.selectedOrder.splice(index, 1);
        }
      });
 
      this.$emit("input", [...newVal]);
      this.$emit("change", [...newVal], [...this.selectedOrder]);
    },
  },
  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})`;
      }
    },
    getSelectionOrder() {
      return [...this.selectedOrder];
    },
    setSelectionOrder(orderedValues) {
      this.selectedOrder = [...orderedValues];
      this.checkedValues = [...orderedValues];
    },
  },
};
</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;
}
 
.order-label {
  font-weight: bold;
  color: #606266;
  margin-right: 8px;
}
 
.order-item {
  color: #409eff;
  font-weight: 500;
}
 
/* 响应式设计:小屏幕时换行 */
@media (max-width: 768px) {
  .horizontal-checkbox-group {
    gap: 12px;
  }
 
  .selection-order-display {
    padding: 8px;
  }
}
</style>