|
@@ -68,7 +68,7 @@
|
|
|
<script setup lang="ts">
|
|
<script setup lang="ts">
|
|
|
import { ref, computed, type PropType, watch } from "vue";
|
|
import { ref, computed, type PropType, watch } from "vue";
|
|
|
import type { ClSelectOption } from "../../types";
|
|
import type { ClSelectOption } from "../../types";
|
|
|
-import { isEmpty, parsePt } from "@/cool";
|
|
|
|
|
|
|
+import { isEmpty, isNull, parsePt } from "@/cool";
|
|
|
import type { ClSelectTriggerPassThrough } from "../cl-select-trigger/props";
|
|
import type { ClSelectTriggerPassThrough } from "../cl-select-trigger/props";
|
|
|
import type { ClPopupPassThrough } from "../cl-popup/props";
|
|
import type { ClPopupPassThrough } from "../cl-popup/props";
|
|
|
import { t } from "@/locale";
|
|
import { t } from "@/locale";
|
|
@@ -213,37 +213,101 @@ const columns = computed<ClSelectOption[][]>(() => {
|
|
|
});
|
|
});
|
|
|
|
|
|
|
|
// 显示文本
|
|
// 显示文本
|
|
|
-const text = computed(() => {
|
|
|
|
|
- // 获取当前v-model绑定的值
|
|
|
|
|
|
|
+const text = ref("");
|
|
|
|
|
+
|
|
|
|
|
+// 更新文本内容
|
|
|
|
|
+function updateText() {
|
|
|
|
|
+ // 当前绑定的值
|
|
|
const val = props.modelValue;
|
|
const val = props.modelValue;
|
|
|
|
|
|
|
|
// 如果值为null或空,直接返回空字符串
|
|
// 如果值为null或空,直接返回空字符串
|
|
|
if (val == null || isEmpty(val)) {
|
|
if (val == null || isEmpty(val)) {
|
|
|
- return "";
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ text.value = "";
|
|
|
|
|
+ } else {
|
|
|
|
|
+ // 用于存储每列的选中值
|
|
|
|
|
+ let arr: any[];
|
|
|
|
|
|
|
|
- // 用于存储每列的选中值
|
|
|
|
|
- let arr: any[];
|
|
|
|
|
|
|
+ if (props.columnCount == 1) {
|
|
|
|
|
+ // 单列时将值包装为数组
|
|
|
|
|
+ arr = [val];
|
|
|
|
|
+ } else {
|
|
|
|
|
+ // 多列时直接使用数组
|
|
|
|
|
+ arr = val as any[];
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
- if (props.columnCount == 1) {
|
|
|
|
|
- // 单列时将值包装为数组
|
|
|
|
|
- arr = [val];
|
|
|
|
|
- } else {
|
|
|
|
|
- // 多列时直接使用数组
|
|
|
|
|
- arr = val as any[];
|
|
|
|
|
|
|
+ // 遍历每列的选中值,查找对应label,找不到则用空字符串
|
|
|
|
|
+ text.value = arr
|
|
|
|
|
+ .map((e, i) => columns.value[i].find((a) => a.value == e)?.label ?? "")
|
|
|
|
|
+ .join(props.splitor);
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
- // 遍历每列的选中值,查找对应label,找不到则用空字符串
|
|
|
|
|
- return arr
|
|
|
|
|
- .map((e, i) => columns.value[i].find((a) => a.value == e)?.label ?? "")
|
|
|
|
|
- .join(props.splitor);
|
|
|
|
|
-});
|
|
|
|
|
|
|
+}
|
|
|
|
|
|
|
|
// 获取当前选中值
|
|
// 获取当前选中值
|
|
|
function getValue() {
|
|
function getValue() {
|
|
|
return props.columnCount == 1 ? value.value[0] : value.value;
|
|
return props.columnCount == 1 ? value.value[0] : value.value;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+// 解析值
|
|
|
|
|
+function setValue(val: Value) {
|
|
|
|
|
+ // 声明选中值数组
|
|
|
|
|
+ let _value: any[];
|
|
|
|
|
+
|
|
|
|
|
+ // 判断值是否为null
|
|
|
|
|
+ if (val == null) {
|
|
|
|
|
+ // 设置为空数组
|
|
|
|
|
+ _value = [];
|
|
|
|
|
+ }
|
|
|
|
|
+ // 判断是否为数组类型
|
|
|
|
|
+ else if (Array.isArray(val)) {
|
|
|
|
|
+ // 使用该数组
|
|
|
|
|
+ _value = [...(val as any[])];
|
|
|
|
|
+ }
|
|
|
|
|
+ // 其他类型
|
|
|
|
|
+ else {
|
|
|
|
|
+ // 转换为数组格式
|
|
|
|
|
+ _value = [val];
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // 存储每列选中项的索引值
|
|
|
|
|
+ let _indexes = [] as number[];
|
|
|
|
|
+
|
|
|
|
|
+ // 遍历所有列
|
|
|
|
|
+ for (let i = 0; i < props.columnCount; i++) {
|
|
|
|
|
+ // 判断是否超出选中值数组长度
|
|
|
|
|
+ if (i >= _value.length) {
|
|
|
|
|
+ // 添加默认索引0
|
|
|
|
|
+ _indexes.push(0);
|
|
|
|
|
+
|
|
|
|
|
+ // 添加默认值
|
|
|
|
|
+ if (!isNull(columns.value[i][0])) {
|
|
|
|
|
+ _value.push(columns.value[i][0].value);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ // 在范围内
|
|
|
|
|
+ else {
|
|
|
|
|
+ // 查找匹配的选项索引
|
|
|
|
|
+ let index = columns.value[i].findIndex((e) => e.value == _value[i]);
|
|
|
|
|
+
|
|
|
|
|
+ // 索引无效时重置为0
|
|
|
|
|
+ if (index < 0) {
|
|
|
|
|
+ index = 0;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // 添加索引
|
|
|
|
|
+ _indexes.push(index);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // 更新选中值
|
|
|
|
|
+ value.value = _value;
|
|
|
|
|
+
|
|
|
|
|
+ // 更新索引值
|
|
|
|
|
+ indexes.value = _indexes;
|
|
|
|
|
+
|
|
|
|
|
+ // 更新显示文本
|
|
|
|
|
+ updateText();
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
// 选择器值改变事件
|
|
// 选择器值改变事件
|
|
|
function onChange(a: number[]) {
|
|
function onChange(a: number[]) {
|
|
|
// 复制当前组件内部维护的索引数组
|
|
// 复制当前组件内部维护的索引数组
|
|
@@ -271,7 +335,7 @@ function onChange(a: number[]) {
|
|
|
// 更新组件内部维护的索引数组
|
|
// 更新组件内部维护的索引数组
|
|
|
indexes.value = b;
|
|
indexes.value = b;
|
|
|
// 根据最新的索引数组,更新选中的值数组
|
|
// 根据最新的索引数组,更新选中的值数组
|
|
|
- value.value = b.map((e, i) => columns.value[i][e].value);
|
|
|
|
|
|
|
+ value.value = b.map((e, i) => (isNull(columns.value[i][e]) ? 0 : columns.value[i][e].value));
|
|
|
|
|
|
|
|
// 触发changing事件
|
|
// 触发changing事件
|
|
|
emit("changing", getValue());
|
|
emit("changing", getValue());
|
|
@@ -286,6 +350,11 @@ let callback: ((value: Value) => void) | null = null;
|
|
|
// 打开选择器
|
|
// 打开选择器
|
|
|
function open(cb: ((value: Value) => void) | null = null) {
|
|
function open(cb: ((value: Value) => void) | null = null) {
|
|
|
visible.value = true;
|
|
visible.value = true;
|
|
|
|
|
+
|
|
|
|
|
+ // 设置值
|
|
|
|
|
+ setValue(props.modelValue);
|
|
|
|
|
+
|
|
|
|
|
+ // 回调
|
|
|
callback = cb;
|
|
callback = cb;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -296,6 +365,8 @@ function close() {
|
|
|
|
|
|
|
|
// 清空选择器
|
|
// 清空选择器
|
|
|
function clear() {
|
|
function clear() {
|
|
|
|
|
+ text.value = "";
|
|
|
|
|
+
|
|
|
if (props.columnCount == 1) {
|
|
if (props.columnCount == 1) {
|
|
|
emit("update:modelValue", null);
|
|
emit("update:modelValue", null);
|
|
|
emit("change", null);
|
|
emit("change", null);
|
|
@@ -327,58 +398,7 @@ function confirm() {
|
|
|
watch(
|
|
watch(
|
|
|
computed(() => props.modelValue),
|
|
computed(() => props.modelValue),
|
|
|
(val: Value) => {
|
|
(val: Value) => {
|
|
|
- // 声明选中值数组
|
|
|
|
|
- let _value: any[];
|
|
|
|
|
-
|
|
|
|
|
- // 判断值是否为null
|
|
|
|
|
- if (val == null) {
|
|
|
|
|
- // 设置为空数组
|
|
|
|
|
- _value = [];
|
|
|
|
|
- }
|
|
|
|
|
- // 判断是否为数组类型
|
|
|
|
|
- else if (Array.isArray(val)) {
|
|
|
|
|
- // 使用该数组
|
|
|
|
|
- _value = [...(val as any[])];
|
|
|
|
|
- }
|
|
|
|
|
- // 其他类型
|
|
|
|
|
- else {
|
|
|
|
|
- // 转换为数组格式
|
|
|
|
|
- _value = [val];
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- // 存储每列选中项的索引值
|
|
|
|
|
- let _indexes = [] as number[];
|
|
|
|
|
-
|
|
|
|
|
- // 遍历所有列
|
|
|
|
|
- for (let i = 0; i < props.columnCount; i++) {
|
|
|
|
|
- // 判断是否超出选中值数组长度
|
|
|
|
|
- if (i >= _value.length) {
|
|
|
|
|
- // 添加默认索引0
|
|
|
|
|
- _indexes.push(0);
|
|
|
|
|
-
|
|
|
|
|
- // 添加默认值
|
|
|
|
|
- _value.push(columns.value[i][0].value);
|
|
|
|
|
- }
|
|
|
|
|
- // 在范围内
|
|
|
|
|
- else {
|
|
|
|
|
- // 查找匹配的选项索引
|
|
|
|
|
- let index = columns.value[i].findIndex((e) => e.value == _value[i]);
|
|
|
|
|
-
|
|
|
|
|
- // 索引无效时重置为0
|
|
|
|
|
- if (index < 0) {
|
|
|
|
|
- index = 0;
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- // 添加索引
|
|
|
|
|
- _indexes.push(index);
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- // 更新选中值
|
|
|
|
|
- value.value = _value;
|
|
|
|
|
-
|
|
|
|
|
- // 更新索引值
|
|
|
|
|
- indexes.value = _indexes;
|
|
|
|
|
|
|
+ setValue(val);
|
|
|
},
|
|
},
|
|
|
{
|
|
{
|
|
|
immediate: true
|
|
immediate: true
|