uni-data-pickerview.vue 7.3 KB
Newer Older
DCloud_JSON's avatar
DCloud_JSON 已提交
1
<template>
2 3 4 5 6 7
	<view class="uni-data-pickerview">
		<scroll-view class="selected-area" scroll-x="true" scroll-y="false" :show-scrollbar="false">
			<view class="selected-list">
				<template v-for="(item,index) in selected">
					<view class="selected-item"
						:class="{'selected-item-active':index==selectedIndex, 'selected-item-text-overflow': ellipsis}"
8
						 v-if="item.text" @click="handleSelect(index)">
9 10 11
						<text class="">{{item.text}}</text>
					</view>
				</template>
DCloud_JSON's avatar
DCloud_JSON 已提交
12
			</view>
13 14
		</scroll-view>
		<view class="tab-c">
15
			<template v-for="(child, i) in dataList" >
16
				<scroll-view class="list" :key="i" v-if="i==selectedIndex" :scroll-y="true">
17
					<view class="item" :class="{'is-disabled': !!item.disable}" v-for="(item, j) in child"
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
						@click="handleNodeClick(item, i, j)">
						<text class="item-text item-text-overflow">{{item[map.text]}}</text>
						<view class="check" v-if="selected.length > i && item[map.value] == selected[i].value"></view>
					</view>
				</scroll-view>
			</template>

			<view class="loading-cover" v-if="loading">
				<uni-load-more class="load-more" :contentText="loadMore" status="loading"></uni-load-more>
			</view>
			<view class="error-message" v-if="errorMessage">
				<text class="error-text">{{errorMessage}}</text>
			</view>
		</view>
	</view>
DCloud_JSON's avatar
DCloud_JSON 已提交
33 34 35
</template>

<script>
36
	import dataPicker from "./uni-data-picker.js"
DCloud_JSON's avatar
DCloud_JSON 已提交
37

38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
	/**
	 * DataPickerview
	 * @description uni-data-pickerview
	 * @tutorial https://ext.dcloud.net.cn/plugin?id=3796
	 * @property {Array} localdata 本地数据,参考
	 * @property {Boolean} step-searh = [true|false] 是否分布查询
	 * @value true 启用分布查询,仅查询当前选中节点
	 * @value false 关闭分布查询,一次查询出所有数据
	 * @property {String|DBFieldString} self-field 分布查询当前字段名称
	 * @property {String|DBFieldString} parent-field 分布查询父字段名称
	 * @property {String|DBCollectionString} collection 表名
	 * @property {String|DBFieldString} field 查询字段,多个字段用 `,` 分割
	 * @property {String} orderby 排序字段及正序倒叙设置
	 * @property {String|JQLString} where 查询条件
	 */
	export default {
		name: 'UniDataPickerView',
		emits: ['nodeclick', 'change', 'datachange', 'update:modelValue'],
		mixins: [dataPicker],
		props: {
			managedMode: {
				type: Boolean,
				default: false
61 62 63 64
			},
			ellipsis: {
				type: Boolean,
				default: true
65 66 67 68 69 70 71 72 73
			}
		},
		data() {
			return {}
		},
		created() {
			if (this.managedMode) {
				return
			}
DCloud_JSON's avatar
DCloud_JSON 已提交
74

75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
			this.$nextTick(() => {
				this.load()
			})
		},
		methods: {
			onPropsChange() {
				this._treeData = []
				this.selectedIndex = 0
				this.load()
			},
			load() {
				if (this.isLocaldata) {
					this.loadData()
				} else if (this.dataValue.length) {
					this.getTreePath((res) => {
						this.loadData()
					})
				}
			},
			handleSelect(index) {
				this.selectedIndex = index
			},
			handleNodeClick(item, i, j) {
				if (item.disable) {
					return
				}
				const node = this.dataList[i][j]
				const text = node[this.map.text]
				const value = node[this.map.value]
				if (i < this.selected.length - 1) {
					this.selected.splice(i, this.selected.length - i)
					this.selected.push({
						text,
						value
					})
				} else if (i === this.selected.length - 1) {
					this.selected.splice(i, 1, {
						text,
						value
					})
				}
DCloud_JSON's avatar
DCloud_JSON 已提交
116

117 118 119 120
				if (node.isleaf) {
					this.onSelectedChange(node, node.isleaf)
					return
				}
DCloud_JSON's avatar
DCloud_JSON 已提交
121

122 123 124 125
				const {
					isleaf,
					hasNodes
				} = this._updateBindData()
DCloud_JSON's avatar
DCloud_JSON 已提交
126

127 128 129 130
				if (!this._isTreeView() && !hasNodes) {
					this.onSelectedChange(node, true)
					return
				}
DCloud_JSON's avatar
DCloud_JSON 已提交
131

132 133 134 135
				if (this.isLocaldata && (!hasNodes || isleaf)) {
					this.onSelectedChange(node, true)
					return
				}
DCloud_JSON's avatar
DCloud_JSON 已提交
136

137 138 139 140 141 142 143 144 145 146 147 148
				if (!isleaf && !hasNodes) {
					this._loadNodeData((data) => {
						if (!data.length) {
							node.isleaf = true
						} else {
							this._treeData.push(...data)
							this._updateBindData(node)
						}
						this.onSelectedChange(node, node.isleaf)
					}, this._nodeWhere())
					return
				}
DCloud_JSON's avatar
DCloud_JSON 已提交
149

150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
				this.onSelectedChange(node, false)
			},
			updateData(data) {
				this._treeData = data.treeData
				this.selected = data.selected
				if (!this._treeData.length) {
					this.loadData()
				} else {
					//this.selected = data.selected
					this._updateBindData()
				}
			},
			onDataChange() {
				this.$emit('datachange')
			},
			onSelectedChange(node, isleaf) {
				if (isleaf) {
					this._dispatchEvent()
				}
DCloud_JSON's avatar
DCloud_JSON 已提交
169

170 171 172 173 174 175 176
				if (node) {
					this.$emit('nodeclick', node)
				}
			},
			_dispatchEvent() {
				this.$emit('change', this.selected.slice(0))
			}
DCloud_JSON's avatar
DCloud_JSON 已提交
177
		}
178
	}
DCloud_JSON's avatar
DCloud_JSON 已提交
179
</script>
180 181 182
<style lang="scss">
	$uni-primary: #007aff !default;

183 184 185 186 187 188 189 190 191
	.uni-data-pickerview {
		flex: 1;
		/* #ifndef APP-NVUE */
		display: flex;
		/* #endif */
		flex-direction: column;
		overflow: hidden;
		height: 100%;
	}
DCloud_JSON's avatar
DCloud_JSON 已提交
192

193 194 195
	.error-text {
		color: #DD524D;
	}
DCloud_JSON's avatar
DCloud_JSON 已提交
196

197 198 199 200 201 202 203 204 205 206 207 208 209 210
	.loading-cover {
		position: absolute;
		left: 0;
		top: 0;
		right: 0;
		bottom: 0;
		background-color: rgba(255, 255, 255, .5);
		/* #ifndef APP-NVUE */
		display: flex;
		/* #endif */
		flex-direction: column;
		align-items: center;
		z-index: 1001;
	}
DCloud_JSON's avatar
DCloud_JSON 已提交
211

212
	.load-more {
DCloud_JSON's avatar
DCloud_JSON 已提交
213
		/* #ifndef APP-NVUE */
214
		margin: auto;
DCloud_JSON's avatar
DCloud_JSON 已提交
215
		/* #endif */
216
	}
DCloud_JSON's avatar
DCloud_JSON 已提交
217

218 219 220 221 222 223 224 225 226 227 228
	.error-message {
		background-color: #fff;
		position: absolute;
		left: 0;
		top: 0;
		right: 0;
		bottom: 0;
		padding: 15px;
		opacity: .9;
		z-index: 102;
	}
DCloud_JSON's avatar
DCloud_JSON 已提交
229

230 231 232 233
	/* #ifdef APP-NVUE */
	.selected-area {
		width: 750rpx;
	}
DCloud_JSON's avatar
DCloud_JSON 已提交
234

235 236 237 238 239 240 241 242 243 244 245
	/* #endif */

	.selected-list {
		/* #ifndef APP-NVUE */
		display: flex;
		/* #endif */
		flex-direction: row;
		flex-wrap: nowrap;
		padding: 0 5px;
		border-bottom: 1px solid #f8f8f8;
	}
DCloud_JSON's avatar
DCloud_JSON 已提交
246

247 248 249 250 251 252 253 254 255
	.selected-item {
		margin-left: 10px;
		margin-right: 10px;
		padding: 12px 0;
		text-align: center;
		/* #ifndef APP-NVUE */
		white-space: nowrap;
		/* #endif */
	}
DCloud_JSON's avatar
DCloud_JSON 已提交
256

257 258 259 260
	.selected-item-text-overflow {
		width: 168px;
		/* fix nvue */
		overflow: hidden;
DCloud_JSON's avatar
DCloud_JSON 已提交
261
		/* #ifndef APP-NVUE */
262
		width: 6em;
DCloud_JSON's avatar
DCloud_JSON 已提交
263
		white-space: nowrap;
264 265
		text-overflow: ellipsis;
		-o-text-overflow: ellipsis;
DCloud_JSON's avatar
DCloud_JSON 已提交
266
		/* #endif */
267
	}
DCloud_JSON's avatar
DCloud_JSON 已提交
268

269
	.selected-item-active {
270
		border-bottom: 2px solid $uni-primary;
271
	}
DCloud_JSON's avatar
DCloud_JSON 已提交
272

273
	.selected-item-text {
274
		color: $uni-primary;
275
	}
DCloud_JSON's avatar
DCloud_JSON 已提交
276

277 278 279 280 281 282 283 284 285
	.tab-c {
		position: relative;
		flex: 1;
		/* #ifndef APP-NVUE */
		display: flex;
		/* #endif */
		flex-direction: row;
		overflow: hidden;
	}
DCloud_JSON's avatar
DCloud_JSON 已提交
286

287 288 289
	.list {
		flex: 1;
	}
DCloud_JSON's avatar
DCloud_JSON 已提交
290

291 292
	.item {
		padding: 12px 15px;
293
		/* border-bottom: 1px solid #f0f0f0; */
294 295 296 297 298 299
		/* #ifndef APP-NVUE */
		display: flex;
		/* #endif */
		flex-direction: row;
		justify-content: space-between;
	}
DCloud_JSON's avatar
DCloud_JSON 已提交
300

301 302 303
	.is-disabled {
		opacity: .5;
	}
DCloud_JSON's avatar
DCloud_JSON 已提交
304

305 306 307 308
	.item-text {
		/* flex: 1; */
		color: #333333;
	}
DCloud_JSON's avatar
DCloud_JSON 已提交
309

310 311 312 313 314 315 316 317 318 319 320 321 322 323
	.item-text-overflow {
		width: 280px;
		/* fix nvue */
		overflow: hidden;
		/* #ifndef APP-NVUE */
		width: 20em;
		white-space: nowrap;
		text-overflow: ellipsis;
		-o-text-overflow: ellipsis;
		/* #endif */
	}

	.check {
		margin-right: 5px;
324
		border: 2px solid $uni-primary;
325 326 327 328 329 330 331 332 333 334
		border-left: 0;
		border-top: 0;
		height: 12px;
		width: 6px;
		transform-origin: center;
		/* #ifndef APP-NVUE */
		transition: all 0.3s;
		/* #endif */
		transform: rotate(45deg);
	}
335
</style>