uniapp uview二级分类选择

uniapp yekong 1438℃

uniapp二级分类选择

源码

<template>
	<div>
		<pcates :pid="0" @getdata="getpcates">
			<u-form-item :label="title" prop="cname" borderBottom ref="item1">
				<u--input disabledColor="#ffffff" disabled fontSize="13" placeholder="请选择职位分类" v-model="data.cname1"
					border="none">
				</u--input>
				<u-icon slot="right" size="12" name="arrow-right"></u-icon>
			</u-form-item>
		</pcates>
		<pcates v-if="data.cid1 && type==2" :pid="data.cid1" @getdata="getpcates2">
			<u-form-item label="职位子类" prop="cname" borderBottom ref="item1">
				<u--input disabledColor="#ffffff" disabled fontSize="13" placeholder="请选择职位子分类" v-model="data.cname2"
					border="none">
				</u--input>
				<u-icon slot="right" size="12" name="arrow-right"></u-icon>
			</u-form-item>
		</pcates>
	</div>
</template>

<script>
	import pcates from '@/components/typeSelector/pcates.vue'
	export default {
		components: {
			pcates
		},
		data() {
			return {
				show: false,
				data: {
					cname1: '',
					cid1: 0,
					cname2: '',
					cid2: ''
				}
			}
		},
		props: {
			name: {
				type: String | Number,
				default () {
					return '';
				}
			},
			cid: {
				type: String | Number,
				default () {
					return '';
				}
			},
			title: {
				type: String | Number,
				default () {
					return '';
				}
			},
			type: {
				type: String | Number,
				default () {
					return '';
				}
			},
		},
		methods: {
			getpcates(e) {
				console.log(e)
				this.data.cid1 = e.id
				this.data.cname1 = e.value
				this.data.cid2 = ''
				this.data.cname2 = ''
				this.getName()
			},
			getpcates2(e) {
				console.log(e)
				this.data.cid2 = e.id
				this.data.cname2 = e.value
				this.getName()
			},
			getName() {
				var that=this;
				if (this.data.cid2) {
					that.$emit("update:cid", this.data.cid2);
					that.$emit("update:name", this.data.cname2);
				} else {
					that.$emit("update:cid", this.data.cid1);
					that.$emit("update:name", this.data.cname1);
				}
			}
		}
	}
</script>
<style>
</style>

子组件

/**
* @Author: 858834013@qq.com
* @Name: typeSelector
* @Date: 2022-01-18
* @Desc: 行业岗位类别
*/
<template>
	<div>
		<div @click="show=true">
			<slot></slot>
		</div>
		<u-picker :show="show" @cancel="show=false" @confirm="getConfirm" :columns="columns"></u-picker>
	</div>
</template>

<script>
	import {
		getpcates
	} from '@/config/api.js'
	export default {
		name: 'typeSelector',
		components: {},
		data() {
			return {
				show: false,
				list: [],
				columns: [
					[]
				]
			};
		},
		mounted() {
			this.getdata()
		},
		props: {
			pid: {
				type: Number | String,
				default () {
					return 0
				}
			}
		},
		watch: {
			pid() {
				this.getdata()
			},
		},
		methods: {
			getConfirm(e) {
				var data = {
					id: this.list[e.indexs[0]].id,
					value: this.list[e.indexs[0]].name
				}
				this.$emit('getdata', data)
				this.show = false
			},
			getdata() {
				var that = this;
				getpcates({
					params: {
						pid: that.pid
					},
					custom: {
						auth: true
					}
				}).then(res => {
					if (res.code == 1) {
						that.columns[0] = []
						res.data.forEach((type) => {
							that.columns[0].push(type.name)
						});
						that.list = res.data
					}
				}).catch(err => {

				})
			},
		}
	}
</script>

<style lang="scss" scoped>

</style>

使用

<twopcates title="职位类别" type='2' :cid.sync="data.jobid"></twopcates>
喜欢 (3)