初始提交
This commit is contained in:
289
components/Jnpf/TreeSelect/Tree.vue
Normal file
289
components/Jnpf/TreeSelect/Tree.vue
Normal file
@@ -0,0 +1,289 @@
|
||||
<template>
|
||||
<view class="u-select">
|
||||
<u-popup :maskCloseAble="maskCloseAble" mode="bottom" :popup="false" v-model="showPopup" length="auto"
|
||||
:safeAreaInsetBottom="safeAreaInsetBottom" @close="close" :z-index="uZIndex">
|
||||
<view class="u-select">
|
||||
<view class="u-select__header" @touchmove.stop.prevent="">
|
||||
<view class="u-select__header__cancel u-select__header__btn" :style="{ color: cancelColor }"
|
||||
hover-class="u-hover-class" :hover-stay-time="150" @tap="close()">
|
||||
{{cancelText}}
|
||||
</view>
|
||||
<view class="u-select__header__title">{{title}}</view>
|
||||
<view class="u-select__header__confirm u-select__header__btn"
|
||||
:style="{ color: moving ? cancelColor : confirmColor }" hover-class="u-hover-class"
|
||||
:hover-stay-time="150" @touchmove.stop="" @tap.stop="handleConfirm()">
|
||||
{{confirmText}}
|
||||
</view>
|
||||
</view>
|
||||
<view class="search-box_sticky" v-if="filterable">
|
||||
<view class="search-box">
|
||||
<u-search :placeholder="$t('app.apply.pleaseKeyword')" height="72" :show-action="false"
|
||||
bg-color="#f0f2f6" shape="square" v-model="filterText">
|
||||
</u-search>
|
||||
</view>
|
||||
</view>
|
||||
<view class="u-select__body u-select__body__treeSelect">
|
||||
<view class="tree-box">
|
||||
<scroll-view :scroll-y="true" style="height: 100%">
|
||||
<ly-tree ref="tree" :node-key="realProps.value" :tree-data="options" :props="realProps"
|
||||
:show-node-icon="true" :filter-node-method="filterNode" child-visible-for-filter-node
|
||||
check-on-click-node :expand-on-click-node="false" default-expand-all
|
||||
:show-radio="!multiple" :show-checkbox="multiple" />
|
||||
</scroll-view>
|
||||
</view>
|
||||
</view>
|
||||
</view>
|
||||
</u-popup>
|
||||
</view>
|
||||
</template>
|
||||
|
||||
<script>
|
||||
const defaultProps = {
|
||||
label: 'fullName',
|
||||
value: 'id',
|
||||
icon: 'icon',
|
||||
children: 'children'
|
||||
}
|
||||
import LyTree from '@/components/ly-tree/ly-tree.vue'
|
||||
export default {
|
||||
name: "tree-select",
|
||||
components: {
|
||||
LyTree
|
||||
},
|
||||
props: {
|
||||
options: {
|
||||
type: Array,
|
||||
default () {
|
||||
return [];
|
||||
}
|
||||
},
|
||||
filterable: {
|
||||
type: Boolean,
|
||||
default: false
|
||||
},
|
||||
// 是否显示边框
|
||||
border: {
|
||||
type: Boolean,
|
||||
default: true
|
||||
},
|
||||
// 通过双向绑定控制组件的弹出与收起
|
||||
modelValue: {
|
||||
type: Boolean,
|
||||
default: false
|
||||
},
|
||||
// "取消"按钮的颜色
|
||||
cancelColor: {
|
||||
type: String,
|
||||
default: '#606266'
|
||||
},
|
||||
// "确定"按钮的颜色
|
||||
confirmColor: {
|
||||
type: String,
|
||||
default: '#2979ff'
|
||||
},
|
||||
// 弹出的z-index值
|
||||
zIndex: {
|
||||
type: [String, Number],
|
||||
default: 0
|
||||
},
|
||||
safeAreaInsetBottom: {
|
||||
type: Boolean,
|
||||
default: false
|
||||
},
|
||||
// 是否允许通过点击遮罩关闭Picker
|
||||
maskCloseAble: {
|
||||
type: Boolean,
|
||||
default: true
|
||||
},
|
||||
defaultValue: {
|
||||
type: Array,
|
||||
default: () => []
|
||||
},
|
||||
props: {
|
||||
type: Object,
|
||||
default: () => ({
|
||||
label: 'fullName',
|
||||
value: 'id',
|
||||
icon: 'icon',
|
||||
children: 'children'
|
||||
})
|
||||
},
|
||||
// 只能选择最后一层的数值
|
||||
lastLevel: {
|
||||
type: Boolean,
|
||||
default: false
|
||||
},
|
||||
// 只能选择最后一层的数值时,需要根据lastLevelKey来判断是否最后一层
|
||||
lastLevelKey: {
|
||||
type: String,
|
||||
default: "hasChildren"
|
||||
},
|
||||
lastLevelValue: {
|
||||
default: false
|
||||
},
|
||||
multiple: {
|
||||
type: Boolean,
|
||||
default: false
|
||||
},
|
||||
// 顶部标题
|
||||
title: {
|
||||
type: String,
|
||||
default: ''
|
||||
},
|
||||
// 取消按钮的文字
|
||||
cancelText: {
|
||||
type: String,
|
||||
default: '取消'
|
||||
},
|
||||
// 确认按钮的文字
|
||||
confirmText: {
|
||||
type: String,
|
||||
default: '确认'
|
||||
}
|
||||
},
|
||||
data() {
|
||||
return {
|
||||
filterText: '',
|
||||
moving: false,
|
||||
showPopup: false
|
||||
};
|
||||
},
|
||||
watch: {
|
||||
// 在select弹起的时候,重新初始化所有数据
|
||||
modelValue: {
|
||||
immediate: true,
|
||||
handler(val) {
|
||||
this.showPopup = val
|
||||
if (val) setTimeout(() => this.init(), 10);
|
||||
}
|
||||
},
|
||||
filterText(val) {
|
||||
this.$refs.tree.filter(val);
|
||||
}
|
||||
},
|
||||
computed: {
|
||||
uZIndex() {
|
||||
// 如果用户有传递z-index值,优先使用
|
||||
return this.zIndex ? this.zIndex : this.$u.zIndex.popup;
|
||||
},
|
||||
realProps() {
|
||||
return {
|
||||
...defaultProps,
|
||||
...this.props
|
||||
}
|
||||
}
|
||||
},
|
||||
methods: {
|
||||
// 标识滑动开始,只有微信小程序才有这样的事件
|
||||
pickstart() {
|
||||
// #ifdef MP-WEIXIN
|
||||
this.moving = true;
|
||||
// #endif
|
||||
},
|
||||
// 标识滑动结束
|
||||
pickend() {
|
||||
// #ifdef MP-WEIXIN
|
||||
this.moving = false;
|
||||
// #endif
|
||||
},
|
||||
filterNode(value, data) {
|
||||
if (!value) return true;
|
||||
return data[this.realProps.label].indexOf(value) !== -1;
|
||||
},
|
||||
init() {
|
||||
this.filterText = ''
|
||||
this.setSelectValue();
|
||||
},
|
||||
// 获取默认选中的值
|
||||
setSelectValue() {
|
||||
this.$nextTick(() => {
|
||||
this.$refs.tree.setCheckedKeys(this.defaultValue)
|
||||
})
|
||||
},
|
||||
close() {
|
||||
this.$emit('close');
|
||||
},
|
||||
handleConfirm() {
|
||||
// #ifdef MP-WEIXIN
|
||||
if (this.moving) return;
|
||||
// #endif
|
||||
let selectValue = this.$refs.tree.getCheckedNodes()
|
||||
if (this.lastLevel) {
|
||||
selectValue = selectValue.filter(o => o[this.lastLevelKey] == this.lastLevelValue)
|
||||
}
|
||||
if (!selectValue.length) return
|
||||
this.$emit('confirm', selectValue);
|
||||
this.close();
|
||||
}
|
||||
}
|
||||
};
|
||||
</script>
|
||||
|
||||
<style scoped lang="scss">
|
||||
.u-select {
|
||||
|
||||
&__action {
|
||||
position: relative;
|
||||
line-height: $u-form-item-height;
|
||||
height: $u-form-item-height;
|
||||
|
||||
&__icon {
|
||||
position: absolute;
|
||||
right: 20rpx;
|
||||
top: 50%;
|
||||
transition: transform .4s;
|
||||
transform: translateY(-50%);
|
||||
z-index: 1;
|
||||
|
||||
&--reverse {
|
||||
transform: rotate(-180deg) translateY(50%);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
&__hader {
|
||||
&__title {
|
||||
color: $u-content-color;
|
||||
}
|
||||
}
|
||||
|
||||
&--border {
|
||||
border-radius: 6rpx;
|
||||
border-radius: 4px;
|
||||
border: 1px solid $u-form-item-border-color;
|
||||
}
|
||||
|
||||
&__header {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: space-between;
|
||||
height: 80rpx;
|
||||
padding: 0 40rpx;
|
||||
}
|
||||
|
||||
&__body {
|
||||
width: 100%;
|
||||
height: 500rpx;
|
||||
overflow: hidden;
|
||||
background-color: #fff;
|
||||
|
||||
.tree-box {
|
||||
height: 100%;
|
||||
}
|
||||
|
||||
&__picker-view {
|
||||
height: 100%;
|
||||
box-sizing: border-box;
|
||||
|
||||
&__item {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
font-size: 32rpx;
|
||||
color: $u-main-color;
|
||||
padding: 0 8rpx;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
</style>
|
||||
143
components/Jnpf/TreeSelect/index.vue
Normal file
143
components/Jnpf/TreeSelect/index.vue
Normal file
@@ -0,0 +1,143 @@
|
||||
<template>
|
||||
<view class="jnpf-tree-select">
|
||||
<selectBox v-model="innerValue" :disabled='disabled' :placeholder="placeholder" @openSelect="openSelect"
|
||||
:select-open="selectShow">
|
||||
</selectBox>
|
||||
<Tree v-if="selectShow" v-model="selectShow" :default-value="defaultValue" :options="options"
|
||||
:multiple="multiple" :lastLevel="lastLevel" :lastLevelKey="lastLevelKey" :lastLevelValue="lastLevelValue"
|
||||
:props="props" :filterable="filterable" @close="handleClose" @confirm="handleConfirm" />
|
||||
</view>
|
||||
</template>
|
||||
|
||||
<script>
|
||||
import Tree from './Tree';
|
||||
import selectBox from '@/components/selectBox'
|
||||
export default {
|
||||
name: 'jnpf-tree-select',
|
||||
components: {
|
||||
Tree,
|
||||
selectBox
|
||||
},
|
||||
props: {
|
||||
modelValue: {
|
||||
default: ''
|
||||
},
|
||||
options: {
|
||||
type: Array,
|
||||
default: () => []
|
||||
},
|
||||
placeholder: {
|
||||
type: String,
|
||||
default: '请选择'
|
||||
},
|
||||
props: {
|
||||
type: Object,
|
||||
default: () => ({
|
||||
label: 'fullName',
|
||||
value: 'id',
|
||||
children: 'children'
|
||||
})
|
||||
},
|
||||
disabled: {
|
||||
type: Boolean,
|
||||
default: false
|
||||
},
|
||||
filterable: {
|
||||
type: Boolean,
|
||||
default: false
|
||||
},
|
||||
multiple: {
|
||||
type: Boolean,
|
||||
default: false
|
||||
},
|
||||
// 只能选择最后一层的数值
|
||||
lastLevel: {
|
||||
type: Boolean,
|
||||
default: false
|
||||
},
|
||||
// 只能选择最后一层的数值时,需要根据lastLevelKey来判断是否最后一层
|
||||
lastLevelKey: {
|
||||
type: String,
|
||||
default: "hasChildren"
|
||||
},
|
||||
lastLevelValue: {
|
||||
default: false
|
||||
},
|
||||
},
|
||||
data() {
|
||||
return {
|
||||
selectShow: false,
|
||||
innerValue: '',
|
||||
defaultValue: []
|
||||
}
|
||||
},
|
||||
watch: {
|
||||
options() {
|
||||
this.setDefault()
|
||||
},
|
||||
modelValue: {
|
||||
handler(val) {
|
||||
this.setDefault()
|
||||
},
|
||||
immediate: true
|
||||
}
|
||||
},
|
||||
methods: {
|
||||
setDefault() {
|
||||
if (!this.modelValue || !this.options) {
|
||||
this.defaultValue = []
|
||||
this.innerValue = ''
|
||||
return
|
||||
}
|
||||
const value = this.multiple ? this.modelValue : [this.modelValue]
|
||||
let label = ''
|
||||
const loop = (value, list) => {
|
||||
for (let i = 0; i < list.length; i++) {
|
||||
let item = list[i]
|
||||
if (value === item[this.props.value]) {
|
||||
label += (!label ? '' : ',') + item[this.props.label]
|
||||
break
|
||||
}
|
||||
if (item.children && Array.isArray(item.children) && item.children.length) {
|
||||
loop(value, item.children)
|
||||
}
|
||||
}
|
||||
}
|
||||
for (let i = 0; i < value.length; i++) {
|
||||
loop(value[i], this.options)
|
||||
}
|
||||
this.innerValue = label
|
||||
this.defaultValue = value
|
||||
},
|
||||
openSelect() {
|
||||
if (this.disabled) return
|
||||
this.selectShow = true
|
||||
},
|
||||
handleClose() {
|
||||
this.selectShow = false
|
||||
},
|
||||
handleConfirm(e) {
|
||||
let label = ''
|
||||
let value = []
|
||||
for (let i = 0; i < e.length; i++) {
|
||||
label += (!label ? '' : ',') + e[i][this.props.label]
|
||||
value.push(e[i][this.props.value])
|
||||
}
|
||||
this.defaultValue = value
|
||||
this.innerValue = label
|
||||
if (!this.multiple) {
|
||||
this.$emit('update:modelValue', value[0])
|
||||
this.$emit('change', value[0], e[0])
|
||||
} else {
|
||||
this.$emit('update:modelValue', value)
|
||||
this.$emit('change', value, e)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
</script>
|
||||
<style lang="scss" scoped>
|
||||
.jnpf-tree-select {
|
||||
width: 100%;
|
||||
}
|
||||
</style>
|
||||
Reference in New Issue
Block a user