feat:默认选中
This commit is contained in:
1 parent
f243e0330a
commit
f3cf7a5054
15 files changed
+730
-248
No files matched your search
@@ -35,11 +35,13 @@ const props = withDefaults(
|
||||
defineProps<{
|
||||
placeholder?: string
|
||||
apiParams?: any
|
||||
autoFirst?: boolean
|
||||
disabled?: boolean
|
||||
}>(),
|
||||
{
|
||||
placeholder: "请选择",
|
||||
apiParams: {},
|
||||
autoFirst: false,
|
||||
disabled: false,
|
||||
},
|
||||
)
|
||||
@@ -75,6 +77,9 @@ async function getOptions() {
|
||||
}
|
||||
const { data } = await mchApi.select(_pa)
|
||||
state.options = data.records ?? []
|
||||
if (props.autoFirst && state.options.length != 0) {
|
||||
state.selectId = state.options[0].id
|
||||
}
|
||||
} finally {
|
||||
setTimeout(() => (state.loading = false), 500)
|
||||
}
|
||||
@@ -91,4 +96,8 @@ function clear() {
|
||||
state.selectId = null
|
||||
getOptions()
|
||||
}
|
||||
|
||||
onMounted(() => {
|
||||
props.autoFirst && getOptions()
|
||||
})
|
||||
</script>
|
||||
@@ -3,7 +3,7 @@
|
||||
style="width: 100%"
|
||||
v-model="state.selectId"
|
||||
:placeholder="props.placeholder"
|
||||
clearable
|
||||
:clearable="props.clearable"
|
||||
remote-show-suffix
|
||||
filterable
|
||||
remote
|
||||
@@ -11,6 +11,7 @@
|
||||
:remote-method="remoteSearch"
|
||||
:loading="state.loading"
|
||||
@visible-change="(e: boolean) => e && getOptions()"
|
||||
@change="change"
|
||||
>
|
||||
<el-option v-for="v in state.options" :key="v.id" :label="`${v.name}`" :value="v.id">
|
||||
<div class="flex items-center">
|
||||
@@ -30,16 +31,24 @@ import { reactive } from "vue"
|
||||
import { useDict } from "/@/hooks/dict"
|
||||
import { getWalletTypeSimple } from "/@/api/wallet/wallet-type"
|
||||
|
||||
const emit = defineEmits(["change"])
|
||||
|
||||
const selectId = defineModel()
|
||||
|
||||
const props = withDefaults(
|
||||
defineProps<{
|
||||
placeholder?: string
|
||||
disabled?: boolean
|
||||
clearable?: boolean
|
||||
autoFirst?: boolean
|
||||
apiParams?: any
|
||||
}>(),
|
||||
{
|
||||
placeholder: "请选择",
|
||||
disabled: false,
|
||||
clearable: true,
|
||||
autoFirst: false,
|
||||
apiParams: {},
|
||||
},
|
||||
)
|
||||
|
||||
@@ -59,6 +68,8 @@ const state = reactive<any>({
|
||||
watch(
|
||||
selectId,
|
||||
(newVal, oldValue) => {
|
||||
console.log(newVal, "newVal")
|
||||
|
||||
if (newVal == oldValue) return
|
||||
state.selectId = newVal
|
||||
getOptions()
|
||||
@@ -71,10 +82,14 @@ async function getOptions() {
|
||||
state.loading = true
|
||||
const _pa = {
|
||||
...queryForm,
|
||||
...props.apiParams,
|
||||
}
|
||||
const { data } = await getWalletTypeSimple(_pa)
|
||||
state.options = data.records ?? []
|
||||
|
||||
if (props.autoFirst && state.options.length != 0) {
|
||||
state.selectId = state.options[0].id
|
||||
emit("change", state.selectId)
|
||||
}
|
||||
const { wallet_type } = useDict("wallet_type")
|
||||
state.walletType = wallet_type
|
||||
} finally {
|
||||
@@ -93,4 +108,12 @@ function clear() {
|
||||
state.selectId = null
|
||||
getOptions()
|
||||
}
|
||||
|
||||
function change() {
|
||||
emit("change", selectId)
|
||||
}
|
||||
|
||||
onMounted(() => {
|
||||
props.autoFirst && getOptions()
|
||||
})
|
||||
</script>
|
||||
@@ -0,0 +1,159 @@
|
||||
<template>
|
||||
<div class="el-tree-select">
|
||||
<el-tree-select
|
||||
v-model="state.selectIdList"
|
||||
id="tree-option"
|
||||
ref="selectTree"
|
||||
:accordion="true"
|
||||
:data="state.options"
|
||||
:props="state.objMap"
|
||||
:node-key="state.objMap.value"
|
||||
:expand-on-click-node="true"
|
||||
:default-expanded-keys="defaultExpandedKey"
|
||||
:filter-node-method="filterNode"
|
||||
@node-click="handleNodeClick"
|
||||
@check="handleCheck"
|
||||
@visible-change="(e) => e && getOptions()"
|
||||
clearable
|
||||
highlight-current
|
||||
default-expand-all
|
||||
show-checkbox
|
||||
collapse-tags
|
||||
multiple
|
||||
/>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<script setup name="TreeMultipleSelect">
|
||||
import { deptTree } from "/@/api/admin/dept"
|
||||
const { proxy } = getCurrentInstance()
|
||||
|
||||
const selectIdList = defineModel()
|
||||
|
||||
const state = reactive({
|
||||
objMap: {
|
||||
value: "id",
|
||||
label: "name",
|
||||
children: "children",
|
||||
},
|
||||
selectIdList: selectIdList,
|
||||
loading: false,
|
||||
options: [],
|
||||
checkedTreeNodes: [],
|
||||
})
|
||||
|
||||
watch(
|
||||
selectIdList,
|
||||
(newVal, oldValue) => {
|
||||
if (newVal == oldValue) return
|
||||
state.selectIdList = newVal
|
||||
getOptions()
|
||||
},
|
||||
{ immediate: true },
|
||||
)
|
||||
|
||||
const emit = defineEmits(["update:value"])
|
||||
|
||||
const valueTitle = ref("")
|
||||
const defaultExpandedKey = ref([])
|
||||
|
||||
/**
|
||||
* 初始化下拉菜单选择器的默认值,并设置默认选中和默认展开。
|
||||
*/
|
||||
function initHandle() {
|
||||
nextTick(() => {
|
||||
const selectedValue = selectIdList.value
|
||||
if (selectedValue !== null && typeof selectedValue !== "undefined") {
|
||||
const node = proxy.$refs.selectTree.getNode(selectedValue)
|
||||
if (node) {
|
||||
valueTitle.value = node.data[state.objMap.label]
|
||||
proxy.$refs.selectTree.setCurrentKey(selectedValue) // 设置默认选中
|
||||
defaultExpandedKey.value = [selectedValue] // 设置默认展开
|
||||
}
|
||||
} else {
|
||||
clearHandle()
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* 点击某一节点时触发的事件,更新当前的选中值和展开状态。
|
||||
* @param {Object} node - 被点击的节点对象
|
||||
*/
|
||||
function handleNodeClick(node) {
|
||||
valueTitle.value = node[state.objMap.label]
|
||||
selectIdList.value = node[state.objMap.value]
|
||||
defaultExpandedKey.value = []
|
||||
proxy.$refs.treeSelect.blur()
|
||||
selectFilterData("")
|
||||
}
|
||||
|
||||
/**
|
||||
* 勾选树节点时
|
||||
*/
|
||||
const handleCheck = (data, checkStatus) => {
|
||||
const { checkedNodes } = checkStatus
|
||||
state.checkedTreeNodes = checkedNodes
|
||||
if (state.checkedTreeNodes.length > 0) {
|
||||
state.switch = false
|
||||
}
|
||||
let deptIds = []
|
||||
if (state.checkedTreeNodes && state.checkedTreeNodes.length) {
|
||||
deptIds = state.checkedTreeNodes.map((v) => v[state.objMap.value])
|
||||
}
|
||||
state.selectIdList = deptIds
|
||||
}
|
||||
|
||||
/**
|
||||
* 搜索过滤函数,根据输入的值来过滤显示的节点。
|
||||
* @param {String} val - 输入框内的搜索关键字
|
||||
*/
|
||||
function selectFilterData(val) {
|
||||
proxy.$refs.selectTree.filter(val)
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据输入的值来判断节点是否需要显示。
|
||||
* @param {String} value - 输入框内的搜索关键字
|
||||
* @param {Object} data - 当前处理的节点数据
|
||||
* @returns {Boolean} - 是否需要显示此节点
|
||||
*/
|
||||
function filterNode(value, data) {
|
||||
if (!value) return true
|
||||
return data[state.objMap["label"]].indexOf(value) !== -1
|
||||
}
|
||||
/**
|
||||
* 清空当前的选中状态,并重置展开状态。
|
||||
*/
|
||||
function clearHandle() {
|
||||
valueTitle.value = ""
|
||||
valueId.value = ""
|
||||
defaultExpandedKey.value = []
|
||||
clearSelected()
|
||||
}
|
||||
/**
|
||||
* 删除所有选中状态的节点。
|
||||
*/
|
||||
function clearSelected() {
|
||||
const allNode = document.querySelectorAll("#tree-option .el-tree-node")
|
||||
allNode.forEach((element) => element.classList.remove("is-current"))
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取数据
|
||||
*/
|
||||
async function getOptions() {
|
||||
try {
|
||||
state.loading = true
|
||||
const _pa = {}
|
||||
const { data } = await deptTree(_pa)
|
||||
state.options = data
|
||||
} finally {
|
||||
setTimeout(() => (state.loading = false), 500)
|
||||
}
|
||||
}
|
||||
</script>
|
||||
|
||||
<style lang="scss" scoped>
|
||||
@import "/@/assets/styles/variables.module.scss";
|
||||
</style>
|
||||
+181
-181
@@ -1,181 +1,181 @@
|
||||
<template>
|
||||
<div class="el-tree-select">
|
||||
<el-select
|
||||
style="width: 100%"
|
||||
v-model="valueId"
|
||||
ref="treeSelect"
|
||||
:filterable="true"
|
||||
:clearable="true"
|
||||
@clear="clearHandle"
|
||||
:filter-method="selectFilterData"
|
||||
:placeholder="placeholder"
|
||||
>
|
||||
<el-option :value="valueId" :label="valueTitle">
|
||||
<el-tree
|
||||
id="tree-option"
|
||||
ref="selectTree"
|
||||
:accordion="accordion"
|
||||
:data="options"
|
||||
:props="objMap"
|
||||
:node-key="objMap.value"
|
||||
:expand-on-click-node="false"
|
||||
:default-expanded-keys="defaultExpandedKey"
|
||||
:filter-node-method="filterNode"
|
||||
@node-click="handleNodeClick"
|
||||
></el-tree>
|
||||
</el-option>
|
||||
</el-select>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<script setup>
|
||||
const { proxy } = getCurrentInstance();
|
||||
|
||||
const props = defineProps({
|
||||
/* 配置项 */
|
||||
objMap: {
|
||||
type: Object,
|
||||
default: () => {
|
||||
return {
|
||||
value: 'id', // ID字段名
|
||||
label: 'label', // 显示名称
|
||||
children: 'children', // 子级字段名
|
||||
};
|
||||
},
|
||||
},
|
||||
/* 自动收起 */
|
||||
accordion: {
|
||||
type: Boolean,
|
||||
default: () => {
|
||||
return false;
|
||||
},
|
||||
},
|
||||
/**当前双向数据绑定的值 */
|
||||
value: {
|
||||
type: [String, Number],
|
||||
default: '',
|
||||
},
|
||||
/**当前的数据 */
|
||||
options: {
|
||||
type: Array,
|
||||
default: () => [],
|
||||
},
|
||||
/**输入框内部的文字 */
|
||||
placeholder: {
|
||||
type: String,
|
||||
default: '',
|
||||
},
|
||||
});
|
||||
|
||||
const emit = defineEmits(['update:value']);
|
||||
|
||||
const valueId = computed({
|
||||
get: () => props.value,
|
||||
set: (val) => {
|
||||
emit('update:value', val);
|
||||
},
|
||||
});
|
||||
const valueTitle = ref('');
|
||||
const defaultExpandedKey = ref([]);
|
||||
|
||||
/**
|
||||
* 初始化下拉菜单选择器的默认值,并设置默认选中和默认展开。
|
||||
*/
|
||||
function initHandle() {
|
||||
nextTick(() => {
|
||||
const selectedValue = valueId.value;
|
||||
if (selectedValue !== null && typeof selectedValue !== 'undefined') {
|
||||
const node = proxy.$refs.selectTree.getNode(selectedValue);
|
||||
if (node) {
|
||||
valueTitle.value = node.data[props.objMap.label];
|
||||
proxy.$refs.selectTree.setCurrentKey(selectedValue); // 设置默认选中
|
||||
defaultExpandedKey.value = [selectedValue]; // 设置默认展开
|
||||
}
|
||||
} else {
|
||||
clearHandle();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 点击某一节点时触发的事件,更新当前的选中值和展开状态。
|
||||
* @param {Object} node - 被点击的节点对象
|
||||
*/
|
||||
function handleNodeClick(node) {
|
||||
valueTitle.value = node[props.objMap.label];
|
||||
valueId.value = node[props.objMap.value];
|
||||
defaultExpandedKey.value = [];
|
||||
proxy.$refs.treeSelect.blur();
|
||||
selectFilterData('');
|
||||
}
|
||||
|
||||
/**
|
||||
* 搜索过滤函数,根据输入的值来过滤显示的节点。
|
||||
* @param {String} val - 输入框内的搜索关键字
|
||||
*/
|
||||
function selectFilterData(val) {
|
||||
proxy.$refs.selectTree.filter(val);
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据输入的值来判断节点是否需要显示。
|
||||
* @param {String} value - 输入框内的搜索关键字
|
||||
* @param {Object} data - 当前处理的节点数据
|
||||
* @returns {Boolean} - 是否需要显示此节点
|
||||
*/
|
||||
function filterNode(value, data) {
|
||||
if (!value) return true;
|
||||
return data[props.objMap['label']].indexOf(value) !== -1;
|
||||
}
|
||||
/**
|
||||
* 清空当前的选中状态,并重置展开状态。
|
||||
*/
|
||||
function clearHandle() {
|
||||
valueTitle.value = '';
|
||||
valueId.value = '';
|
||||
defaultExpandedKey.value = [];
|
||||
clearSelected();
|
||||
}
|
||||
/**
|
||||
* 删除所有选中状态的节点。
|
||||
*/
|
||||
function clearSelected() {
|
||||
const allNode = document.querySelectorAll('#tree-option .el-tree-node');
|
||||
allNode.forEach((element) => element.classList.remove('is-current'));
|
||||
}
|
||||
|
||||
onMounted(() => {
|
||||
initHandle();
|
||||
});
|
||||
|
||||
watch(valueId, () => {
|
||||
initHandle();
|
||||
});
|
||||
</script>
|
||||
|
||||
<style lang="scss" scoped>
|
||||
@import '/@/assets/styles/variables.module.scss';
|
||||
.el-scrollbar .el-scrollbar__view .el-select-dropdown__item {
|
||||
padding: 0;
|
||||
background-color: #fff;
|
||||
height: auto;
|
||||
}
|
||||
|
||||
.el-select-dropdown__item.selected {
|
||||
font-weight: normal;
|
||||
}
|
||||
|
||||
ul li .el-tree .el-tree-node__content {
|
||||
height: auto;
|
||||
padding: 0 20px;
|
||||
box-sizing: border-box;
|
||||
}
|
||||
|
||||
:deep(.el-tree-node__content:hover),
|
||||
:deep(.el-tree-node__content:active),
|
||||
:deep(.is-current > div:first-child),
|
||||
:deep(.el-tree-node__content:focus) {
|
||||
background-color: mix(#fff, $--color-primary, 90%);
|
||||
color: $--color-primary;
|
||||
}
|
||||
</style>
|
||||
<template>
|
||||
<div class="el-tree-select">
|
||||
<el-select
|
||||
style="width: 100%"
|
||||
v-model="valueId"
|
||||
ref="treeSelect"
|
||||
:filterable="true"
|
||||
:clearable="true"
|
||||
@clear="clearHandle"
|
||||
:filter-method="selectFilterData"
|
||||
:placeholder="placeholder"
|
||||
>
|
||||
<el-option :value="valueId" :label="valueTitle">
|
||||
<el-tree
|
||||
id="tree-option"
|
||||
ref="selectTree"
|
||||
:accordion="accordion"
|
||||
:data="options"
|
||||
:props="objMap"
|
||||
:node-key="objMap.value"
|
||||
:expand-on-click-node="false"
|
||||
:default-expanded-keys="defaultExpandedKey"
|
||||
:filter-node-method="filterNode"
|
||||
@node-click="handleNodeClick"
|
||||
></el-tree>
|
||||
</el-option>
|
||||
</el-select>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<script setup>
|
||||
const { proxy } = getCurrentInstance();
|
||||
|
||||
const props = defineProps({
|
||||
/* 配置项 */
|
||||
objMap: {
|
||||
type: Object,
|
||||
default: () => {
|
||||
return {
|
||||
value: 'id', // ID字段名
|
||||
label: 'label', // 显示名称
|
||||
children: 'children', // 子级字段名
|
||||
};
|
||||
},
|
||||
},
|
||||
/* 自动收起 */
|
||||
accordion: {
|
||||
type: Boolean,
|
||||
default: () => {
|
||||
return false;
|
||||
},
|
||||
},
|
||||
/**当前双向数据绑定的值 */
|
||||
value: {
|
||||
type: [String, Number],
|
||||
default: '',
|
||||
},
|
||||
/**当前的数据 */
|
||||
options: {
|
||||
type: Array,
|
||||
default: () => [],
|
||||
},
|
||||
/**输入框内部的文字 */
|
||||
placeholder: {
|
||||
type: String,
|
||||
default: '',
|
||||
},
|
||||
});
|
||||
|
||||
const emit = defineEmits(['update:value']);
|
||||
|
||||
const valueId = computed({
|
||||
get: () => props.value,
|
||||
set: (val) => {
|
||||
emit('update:value', val);
|
||||
},
|
||||
});
|
||||
const valueTitle = ref('');
|
||||
const defaultExpandedKey = ref([]);
|
||||
|
||||
/**
|
||||
* 初始化下拉菜单选择器的默认值,并设置默认选中和默认展开。
|
||||
*/
|
||||
function initHandle() {
|
||||
nextTick(() => {
|
||||
const selectedValue = valueId.value;
|
||||
if (selectedValue !== null && typeof selectedValue !== 'undefined') {
|
||||
const node = proxy.$refs.selectTree.getNode(selectedValue);
|
||||
if (node) {
|
||||
valueTitle.value = node.data[props.objMap.label];
|
||||
proxy.$refs.selectTree.setCurrentKey(selectedValue); // 设置默认选中
|
||||
defaultExpandedKey.value = [selectedValue]; // 设置默认展开
|
||||
}
|
||||
} else {
|
||||
clearHandle();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 点击某一节点时触发的事件,更新当前的选中值和展开状态。
|
||||
* @param {Object} node - 被点击的节点对象
|
||||
*/
|
||||
function handleNodeClick(node) {
|
||||
valueTitle.value = node[props.objMap.label];
|
||||
valueId.value = node[props.objMap.value];
|
||||
defaultExpandedKey.value = [];
|
||||
proxy.$refs.treeSelect.blur();
|
||||
selectFilterData('');
|
||||
}
|
||||
|
||||
/**
|
||||
* 搜索过滤函数,根据输入的值来过滤显示的节点。
|
||||
* @param {String} val - 输入框内的搜索关键字
|
||||
*/
|
||||
function selectFilterData(val) {
|
||||
proxy.$refs.selectTree.filter(val);
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据输入的值来判断节点是否需要显示。
|
||||
* @param {String} value - 输入框内的搜索关键字
|
||||
* @param {Object} data - 当前处理的节点数据
|
||||
* @returns {Boolean} - 是否需要显示此节点
|
||||
*/
|
||||
function filterNode(value, data) {
|
||||
if (!value) return true;
|
||||
return data[props.objMap['label']].indexOf(value) !== -1;
|
||||
}
|
||||
/**
|
||||
* 清空当前的选中状态,并重置展开状态。
|
||||
*/
|
||||
function clearHandle() {
|
||||
valueTitle.value = '';
|
||||
valueId.value = '';
|
||||
defaultExpandedKey.value = [];
|
||||
clearSelected();
|
||||
}
|
||||
/**
|
||||
* 删除所有选中状态的节点。
|
||||
*/
|
||||
function clearSelected() {
|
||||
const allNode = document.querySelectorAll('#tree-option .el-tree-node');
|
||||
allNode.forEach((element) => element.classList.remove('is-current'));
|
||||
}
|
||||
|
||||
onMounted(() => {
|
||||
initHandle();
|
||||
});
|
||||
|
||||
watch(valueId, () => {
|
||||
initHandle();
|
||||
});
|
||||
</script>
|
||||
|
||||
<style lang="scss" scoped>
|
||||
@import '/@/assets/styles/variables.module.scss';
|
||||
.el-scrollbar .el-scrollbar__view .el-select-dropdown__item {
|
||||
padding: 0;
|
||||
background-color: #fff;
|
||||
height: auto;
|
||||
}
|
||||
|
||||
.el-select-dropdown__item.selected {
|
||||
font-weight: normal;
|
||||
}
|
||||
|
||||
ul li .el-tree .el-tree-node__content {
|
||||
height: auto;
|
||||
padding: 0 20px;
|
||||
box-sizing: border-box;
|
||||
}
|
||||
|
||||
:deep(.el-tree-node__content:hover),
|
||||
:deep(.el-tree-node__content:active),
|
||||
:deep(.is-current > div:first-child),
|
||||
:deep(.el-tree-node__content:focus) {
|
||||
background-color: mix(#fff, $--color-primary, 90%);
|
||||
color: $--color-primary;
|
||||
}
|
||||
</style>
|
||||
@@ -25,6 +25,7 @@ import SelectHardGroup from "/@/components/Select/selectHardGroup.vue"
|
||||
import SelectHandlerUser from "/@/components/Select/SelectHandlerUser.vue"
|
||||
import SelectSupplier from "/@/components/Select/SelectSupplier.vue"
|
||||
import SelectMchRule from "/@/components/Select/SelectMchRule.vue"
|
||||
import TreeMultipleSelect from "/@/components/TreeMultipleSelect/index.vue"
|
||||
|
||||
// 第三方组件
|
||||
import ElementPlus from "element-plus"
|
||||
@@ -61,6 +62,7 @@ export default {
|
||||
app.component("SvgIcon", SvgIcon)
|
||||
app.component("PreviewImage", PreviewImage)
|
||||
app.component("DraggableDepTree", DraggableDepTree)
|
||||
app.component("TreeMultipleSelect", TreeMultipleSelect)
|
||||
|
||||
app.component("SelectUser", SelectUser)
|
||||
app.component("SelectWalletType", SelectWalletType)
|
||||
|
||||
Reference in new issue
Block a user