網站首頁 編程語言 正文
1. 用法 回顯 :defaultIds="[755684, 1415686, 2142999]" 初始 :defaultIds="[]"
<template>
<view class="content">
<selfPicker
level="3"
:defaultIds="[755684, 1415686, 2142999]"
:getIds="getIds"
idKey="areaId"
name="areaName"
children="children"
:columnData="data"
></selfPicker>
</view>
</template>
<script>
import { data } from "./index";
export default {
data() {
return {
data: [],
};
},
mounted() {
setTimeout(() => {
// 數據格式
// [
// {
// areaId: 752254,
// areaName: "北京市",
// children: [
// {
// areaId: 752252,
// areaName: "北京市",
// children: [
// {
// areaId: 752260,
// areaName: "順義區",
// parentId: 752252,
// },
// {
// areaId: 752262,
// areaName: "昌平區",
// parentId: 752252,
// },
// ],
// },
// ],
// },
// ];
this.data = Object.freeze(data);
}, 300);
},
methods: {
getIds(value) {
console.log(value, "111111111111");
},
},
};
</script>
組件封裝
<template>
<picker
:mode="mode"
@columnchange="columnchange"
@change="change"
:range="pickerRange"
:value="selectItem.value"
:range-key="name"
>
{{ selectItem.name || "請選擇" }}
</picker>
</template>
<script>
export default {
props: {
level: {
type: [String, Number],
default: 1,
},
defaultIds: {
type: Array,
default: [],
},
getIds: {
type: Function,
default: () => {},
},
columnData: {
type: Array,
required: true,
},
name: {
type: String,
required: true,
},
idKey: {
type: String,
required: true,
},
children: {
type: String,
required: true,
},
},
data() {
return {
mode: "multiSelector", // 默認多列
pickerRange: undefined,
selectItem: { value: "", name: "" },
areaData: [],
};
},
watch: {
columnData(newData) {
this.$nextTick(this.dealData(newData));
},
},
methods: {
// 1:一列選擇器 2:兩列選擇器 3:三列選擇器
dealData(newData) {
this.areaData = Object.freeze(newData);
let result = "";
switch (+this.level) {
case 1:
this.mode = "selector"; // 普通單列選擇器
result = this.areaData.map((item, index) => {
if (+item[this.idKey] === +this.defaultIds[0]) {
this.$set(this.selectItem, "value", index);
this.$set(this.selectItem, "name", item[this.name]);
}
return {
[this.idKey]: item[this.idKey],
[this.name]: item[this.name],
};
});
this.pickerRange = Object.freeze(result);
break;
case 2:
case 3:
this.mode = "multiSelector"; // 默認多列
this.pickerRange = [];
this.$set(this.selectItem, "value", []);
for (let i = 0; i < +this.level; i++) {
this.pickerRange.push([]);
}
result = this.areaData.map((item, index) => {
if (+item[this.idKey] === +this.defaultIds[0]) {
this.$set(
this.pickerRange,
1,
Object.freeze(item?.[this.children] ?? [])
);
this.$set(this.selectItem, "value", [
...this.selectItem.value,
index,
]);
this.$set(
this.selectItem,
"name",
this.selectItem.name + `${item[this.name]}`
);
this.pickerRange[1].forEach((item, index) => {
if (+item[this.idKey] === +this.defaultIds[1]) {
if (+this.level === 3) {
this.$set(
this.pickerRange,
2,
Object.freeze(item?.[this.children] ?? [])
);
this.pickerRange[2].forEach((item, index) => {
if (+item[this.idKey] === +this.defaultIds[2]) {
this.$set(this.selectItem, "value", [
...this.selectItem.value,
index,
]);
this.$set(
this.selectItem,
"name",
this.selectItem.name + `--${item[this.name]}`
);
}
});
}
this.$set(this.selectItem, "value", [
...this.selectItem.value,
index,
]);
this.$set(
this.selectItem,
"name",
this.selectItem.name + `--${item[this.name]}`
);
}
});
}
return {
[this.idKey]: item[this.idKey],
[this.name]: item[this.name],
[this.children]: item[this.children],
};
});
this.pickerRange[0] = Object.freeze(result);
if (this.pickerRange[1].length <= 0) {
this.$set(
this.pickerRange,
1,
Object.freeze(this.pickerRange[0][0][this.children])
);
}
if (+this.level === 3 && this.pickerRange[2].length <= 0) {
this.$set(
this.pickerRange,
2,
Object.freeze(this.pickerRange[1][0][this.children])
);
}
break;
}
},
columnchange(event) {
const { column, value } = event.detail;
switch (+column) {
case 0:
if (+this.level === 2) {
this.$set(
this.pickerRange,
1,
Object.freeze(
this.pickerRange[column][value]?.[this.children] ?? []
)
);
this.selectItem.value = [value, 0];
}
if (+this.level === 3) {
this.$set(
this.pickerRange,
1,
Object.freeze(
this.pickerRange[column][value]?.[this.children] ?? []
)
);
this.$set(
this.pickerRange,
2,
Object.freeze(this.pickerRange[1][0]?.[this.children] ?? [])
);
this.selectItem.value = [value, 0, 0];
}
break;
case 1:
if (+this.level === 3) {
this.$set(
this.pickerRange,
2,
Object.freeze(
this.pickerRange[column][value]?.[this.children] ?? []
)
);
this.selectItem.value[2] = 0;
}
break;
}
},
change(e) {
const { value } = e.detail;
if (typeof value === "number") {
this.getIds([this.pickerRange[value]?.[this.idKey]]);
this.$set(
this.selectItem,
"name",
this.pickerRange[value]?.[this.name]
);
return;
}
let arr = [],
name = "";
value.forEach((item, index) => {
arr.push(this.pickerRange[index][item]?.[this.idKey]);
if (name) {
name += `--${this.pickerRange[index][item]?.[this.name]}`;
} else {
name += `${this.pickerRange[index][item]?.[this.name]}`;
}
});
this.$set(this.selectItem, "name", name);
this.getIds(arr);
},
},
};
</script>
<style></style>
原文鏈接:https://blog.csdn.net/weixin_44147791/article/details/122986481
- 上一篇:沒有了
- 下一篇:沒有了
相關推薦
- 2022-05-13 Github pages 同步到Gitee pages 并自動更新Gitee pages
- 2023-01-07 python導入其他目錄下模塊的四種情況_python
- 2022-11-02 Python?IDLE設置清屏快捷鍵的方法詳解_python
- 2022-07-06 C語言舉例講解i++與++i之間的區別_C 語言
- 2023-08-15 antdv Input組件maxLength屬性設置默認值
- 2022-09-19 Tomcat配置https?SSL證書的項目實踐_Tomcat
- 2022-07-03 C#入門之定義類成員與接口實現_C#教程
- 2023-10-15 DPC_WATCHDOG_VIOLATION藍屏分析
- 欄目分類
-
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支