|
@@ -1,36 +1,160 @@
|
|
|
<template>
|
|
|
- <up-picker :show="show" ref="uPickerRef" :columns="columns" @confirm="confirm" @change="changeHandler"></up-picker>
|
|
|
+ <up-picker :show="show" :defaultIndex="pickValue.indexs" ref="uPickerRef" valueKey="id" :columns="columns" @confirm="confirm" @change="changeHandler"></up-picker>
|
|
|
</template>
|
|
|
|
|
|
<script setup>
|
|
|
-import { ref, reactive } from 'vue';
|
|
|
-
|
|
|
-const show = ref(true);
|
|
|
-const columns = reactive([
|
|
|
- ['中国', '美国'],
|
|
|
- ['深圳', '厦门', '上海', '拉萨']
|
|
|
-]);
|
|
|
-const columnData = reactive([
|
|
|
- ['深圳', '厦门', '上海', '拉萨'],
|
|
|
- ['得州', '华盛顿', '纽约', '阿拉斯加']
|
|
|
-]);
|
|
|
+import { ref, reactive, computed } from 'vue';
|
|
|
+
|
|
|
+
|
|
|
+const props = defineProps({
|
|
|
+ columnData: {
|
|
|
+ type: Array,
|
|
|
+ default: []
|
|
|
+ }
|
|
|
+});
|
|
|
+
|
|
|
+const emit = defineEmits(['submit']);
|
|
|
+
|
|
|
+
|
|
|
+const show = ref(false);
|
|
|
+
|
|
|
+const pickValue = ref({
|
|
|
+ indexs: [0, 0, 1],
|
|
|
+});
|
|
|
+
|
|
|
+const cate = ref(5);
|
|
|
+
|
|
|
+/**
|
|
|
+ *
|
|
|
+ * 根据下标获取显示数据
|
|
|
+ */
|
|
|
+function getDataByKeys(array, keys) {
|
|
|
+ const result = [];
|
|
|
+ let currentLevel = array;
|
|
|
+
|
|
|
+ for (let i = 0; i < keys.length; i++) {
|
|
|
+ const key = keys[i];
|
|
|
+ const currentItems = currentLevel.map(item => item.businessName);
|
|
|
+ result.push(currentItems);
|
|
|
+
|
|
|
+ if (currentLevel[key] && currentLevel[key].children) {
|
|
|
+ currentLevel = currentLevel[key].children;
|
|
|
+ } else {
|
|
|
+ // 如果没有 children,则在结果中添加一个空数组
|
|
|
+ result.push([]);
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ if (result.length > keys.length) {
|
|
|
+ result.pop();
|
|
|
+ }
|
|
|
+ return result;
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+const columns = computed(() => {
|
|
|
+ const data = props.columnData;
|
|
|
+ const { indexs } = pickValue.value
|
|
|
+ const result = getDataByKeys(data, indexs);
|
|
|
+ console.log('=====>', result);
|
|
|
+ console.log('data', data);
|
|
|
+ return result;
|
|
|
+});
|
|
|
+
|
|
|
|
|
|
const uPickerRef = ref(null)
|
|
|
const changeHandler = (e) => {
|
|
|
- const {
|
|
|
- columnIndex,
|
|
|
- value,
|
|
|
- values,
|
|
|
- index,
|
|
|
- } = e;
|
|
|
-
|
|
|
- if (columnIndex === 0) {
|
|
|
- uPickerRef.value.setColumnValues(1, columnData[index]);
|
|
|
- }
|
|
|
+ console.log('e', e);
|
|
|
+ pickValue.value = e;
|
|
|
};
|
|
|
|
|
|
+/**
|
|
|
+ *
|
|
|
+ * 根据下标获取id
|
|
|
+ */
|
|
|
+function getid(keys) {
|
|
|
+ let currentLevel = props.columnData;
|
|
|
+
|
|
|
+ for (let i = 0; i < keys.length; i++) {
|
|
|
+ const key = keys[i];
|
|
|
+ if (currentLevel[key] && currentLevel[key].children) {
|
|
|
+ currentLevel = currentLevel[key].children;
|
|
|
+ } else {
|
|
|
+ if (currentLevel[key]) {
|
|
|
+ return currentLevel[key].id;
|
|
|
+ }
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return null; // 如果没有找到对应的项,返回 null
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
const confirm = (e) => {
|
|
|
- console.log('confirm', e);
|
|
|
+ console.log(e);
|
|
|
+
|
|
|
+ const id = getid(e.indexs);
|
|
|
+ emit('submit', {key: e.value.join('/'),value: id });
|
|
|
show.value = false;
|
|
|
};
|
|
|
+
|
|
|
+//根据id获取下标
|
|
|
+function idToIndexs(array, targetId, path = []) {
|
|
|
+ for (let i = 0; i < array.length; i++) {
|
|
|
+ const item = array[i];
|
|
|
+ const currentPath = path.concat(i);
|
|
|
+
|
|
|
+ if (item.id === targetId) {
|
|
|
+ return currentPath
|
|
|
+ }
|
|
|
+
|
|
|
+ if (item.children) {
|
|
|
+ const result = idToIndexs(item.children, targetId, currentPath);
|
|
|
+ if (result) {
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return null; // 如果没有找到对应的项,返回 null
|
|
|
+}
|
|
|
+
|
|
|
+function indexsToNames(indexs){
|
|
|
+ let currentLevel = props.columnData;
|
|
|
+ const names = [];
|
|
|
+
|
|
|
+ for (let i = 0; i < indexs.length; i++) {
|
|
|
+ const key = indexs[i];
|
|
|
+
|
|
|
+ if (currentLevel[key]) {
|
|
|
+ names.push(currentLevel[key].businessName);
|
|
|
+ if (currentLevel[key].children) {
|
|
|
+ currentLevel = currentLevel[key].children;
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return names;
|
|
|
+ }
|
|
|
+const piceInit = (id) => {
|
|
|
+ console.log('piceInit-id',id);
|
|
|
+ const indexs = idToIndexs(props.columnData,id)
|
|
|
+ const names = indexsToNames(indexs)
|
|
|
+ console.log('piceInit-indexs', indexs,names,names.join('/'));
|
|
|
+ pickValue.value.indexs = indexs;
|
|
|
+
|
|
|
+ return names.join('/')
|
|
|
+}
|
|
|
+
|
|
|
+defineExpose({
|
|
|
+ show() {
|
|
|
+ show.value = true;
|
|
|
+ },
|
|
|
+ piceInit
|
|
|
+});
|
|
|
</script>
|