style(TreeSelect.vue): 代码格式化
This commit is contained in:
@@ -29,7 +29,14 @@
|
|||||||
</template>
|
</template>
|
||||||
|
|
||||||
<script setup>
|
<script setup>
|
||||||
import {ref, getCurrentInstance, nextTick, onMounted, computed, watch} from "vue";
|
import {
|
||||||
|
ref,
|
||||||
|
getCurrentInstance,
|
||||||
|
nextTick,
|
||||||
|
onMounted,
|
||||||
|
computed,
|
||||||
|
watch,
|
||||||
|
} from "vue";
|
||||||
|
|
||||||
const { proxy } = getCurrentInstance();
|
const { proxy } = getCurrentInstance();
|
||||||
|
|
||||||
@@ -39,101 +46,101 @@ const state = defineProps({
|
|||||||
type: Object,
|
type: Object,
|
||||||
default: () => {
|
default: () => {
|
||||||
return {
|
return {
|
||||||
value: 'id', // ID字段名
|
value: "value", // 值
|
||||||
label: 'label', // 显示名称
|
label: "label", // 名
|
||||||
children: 'children' // 子级字段名
|
children: "children", // 子级
|
||||||
}
|
};
|
||||||
}
|
},
|
||||||
},
|
},
|
||||||
// 自动收起
|
// 自动收起
|
||||||
accordion: {
|
accordion: {
|
||||||
type: Boolean,
|
type: Boolean,
|
||||||
default: () => {
|
default: () => {
|
||||||
return false
|
return false;
|
||||||
}
|
},
|
||||||
},
|
},
|
||||||
// v-model 绑定的值
|
// v-model 绑定的值
|
||||||
modelValue: {
|
modelValue: {
|
||||||
type: [String, Number],
|
type: [String, Number],
|
||||||
default: ''
|
default: "",
|
||||||
},
|
},
|
||||||
// 数据源
|
// 数据源
|
||||||
options: {
|
options: {
|
||||||
type: Array,
|
type: Array,
|
||||||
default: []
|
default: [],
|
||||||
},
|
},
|
||||||
// 提示文字
|
// 提示文字
|
||||||
placeholder: {
|
placeholder: {
|
||||||
type: String,
|
type: String,
|
||||||
default: ''
|
default: "",
|
||||||
}
|
},
|
||||||
})
|
});
|
||||||
|
|
||||||
const emit = defineEmits(['update:modelValue']);
|
const emit = defineEmits(["update:modelValue"]);
|
||||||
|
|
||||||
const modelValue = computed({
|
const modelValue = computed({
|
||||||
get: () => {
|
get: () => {
|
||||||
return state.modelValue
|
return state.modelValue;
|
||||||
},
|
},
|
||||||
set: (val) => {
|
set: (val) => {
|
||||||
emit('update:modelValue', val)
|
emit("update:modelValue", val);
|
||||||
}
|
},
|
||||||
});
|
});
|
||||||
const valueTitle = ref('');
|
const valueTitle = ref("");
|
||||||
const defaultExpandedKey = ref([]);
|
const defaultExpandedKey = ref([]);
|
||||||
|
|
||||||
function handleNodeClick(node) {
|
function handleNodeClick(node) {
|
||||||
valueTitle.value = node[state.props.label]
|
valueTitle.value = node[state.props.label];
|
||||||
modelValue.value = node[state.props.value];
|
modelValue.value = node[state.props.value];
|
||||||
defaultExpandedKey.value = [];
|
defaultExpandedKey.value = [];
|
||||||
proxy.$refs.treeSelect.blur()
|
proxy.$refs.treeSelect.blur();
|
||||||
selectFilterData('')
|
selectFilterData("");
|
||||||
}
|
}
|
||||||
|
|
||||||
function selectFilterData(val) {
|
function selectFilterData(val) {
|
||||||
proxy.$refs.selectTree.filter(val)
|
proxy.$refs.selectTree.filter(val);
|
||||||
}
|
}
|
||||||
|
|
||||||
function filterNode(value, data) {
|
function filterNode(value, data) {
|
||||||
if (!value) return true
|
if (!value) return true;
|
||||||
return data[state.props['label']].indexOf(value) !== -1
|
return data[state.props["label"]].indexOf(value) !== -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
function clearHandle() {
|
function clearHandle() {
|
||||||
valueTitle.value = ''
|
valueTitle.value = "";
|
||||||
modelValue.value = ''
|
modelValue.value = "";
|
||||||
defaultExpandedKey.value = [];
|
defaultExpandedKey.value = [];
|
||||||
clearSelected()
|
clearSelected();
|
||||||
}
|
}
|
||||||
|
|
||||||
function clearSelected() {
|
function clearSelected() {
|
||||||
const allNode = document.querySelectorAll('#tree-option .el-tree-node')
|
const allNode = document.querySelectorAll("#tree-option .el-tree-node");
|
||||||
allNode.forEach((element) => element.classList.remove('is-current'))
|
allNode.forEach((element) => element.classList.remove("is-current"));
|
||||||
}
|
}
|
||||||
|
|
||||||
function initHandle() {
|
function initHandle() {
|
||||||
nextTick(() => {
|
nextTick(() => {
|
||||||
const selectedValue = modelValue.value;
|
const selectedValue = modelValue.value;
|
||||||
if (selectedValue !== null && typeof (selectedValue) !== "undefined") {
|
if (selectedValue !== null && typeof selectedValue !== "undefined") {
|
||||||
const node = proxy.$refs.selectTree.getNode(selectedValue)
|
const node = proxy.$refs.selectTree.getNode(selectedValue);
|
||||||
if (node) {
|
if (node) {
|
||||||
valueTitle.value = node.data[state.props.label]
|
valueTitle.value = node.data[state.props.label];
|
||||||
proxy.$refs.selectTree.setCurrentKey(selectedValue) // 设置默认选中
|
proxy.$refs.selectTree.setCurrentKey(selectedValue); // 设置默认选中
|
||||||
defaultExpandedKey.value = [selectedValue] // 设置默认展开
|
defaultExpandedKey.value = [selectedValue]; // 设置默认展开
|
||||||
} else {
|
} else {
|
||||||
clearHandle()
|
clearHandle();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
})
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
onMounted(() => {
|
onMounted(() => {
|
||||||
initHandle();
|
initHandle();
|
||||||
})
|
});
|
||||||
|
|
||||||
watch(modelValue, () => {
|
watch(modelValue, () => {
|
||||||
initHandle();
|
initHandle();
|
||||||
})
|
});
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<style lang='scss' scoped>
|
<style lang='scss' scoped>
|
||||||
@@ -157,7 +164,7 @@ ul li .el-tree .el-tree-node__content {
|
|||||||
:deep(.el-tree-node__content:active),
|
:deep(.el-tree-node__content:active),
|
||||||
:deep(.is-current > div:first-child),
|
:deep(.is-current > div:first-child),
|
||||||
:deep(.el-tree-node__content:focus) {
|
:deep(.el-tree-node__content:focus) {
|
||||||
background-color: mix(#fff, #409EFF, 90%);
|
background-color: mix(#fff, #409eff, 90%);
|
||||||
color: #409EFF;
|
color: #409eff;
|
||||||
}
|
}
|
||||||
</style>
|
</style>
|
||||||
|
|||||||
Reference in New Issue
Block a user