uni-th.vue 6.8 KB
Newer Older
DCloud_JSON's avatar
DCloud_JSON 已提交
1 2
<template>
	<!-- #ifdef H5 -->
3
	<th :rowspan="rowspan" :colspan="colspan" class="uni-table-th" :class="{ 'table--border': border }" :style="{ width: customWidth + 'px', 'text-align': align }">
DCloud_JSON's avatar
DCloud_JSON 已提交
4 5 6 7 8 9 10
		<view class="uni-table-th-row">
			<view class="uni-table-th-content" :style="{ 'justify-content': contentAlign }" @click="sort">
				<slot></slot>
				<view v-if="sortable" class="arrow-box">
					<text class="arrow up" :class="{ active: ascending }" @click.stop="ascendingFn"></text>
					<text class="arrow down" :class="{ active: descending }" @click.stop="descendingFn"></text>
				</view>
11 12
			</view>
			<dropdown v-if="filterType || filterData.length" :filterDefaultValue="filterDefaultValue" :filterData="filterData" :filterType="filterType" @change="ondropdown"></dropdown>
DCloud_JSON's avatar
DCloud_JSON 已提交
13 14 15 16
		</view>
	</th>
	<!-- #endif -->
	<!-- #ifndef H5 -->
study夏羽's avatar
update  
study夏羽 已提交
17
	<view class="uni-table-th" :class="{ 'table--border': border }" :style="{ width: customWidth + 'px', 'text-align': align }"><slot></slot></view>
DCloud_JSON's avatar
DCloud_JSON 已提交
18 19 20
	<!-- #endif -->
</template>

21 22 23 24
<script>
	// #ifdef H5
	import dropdown from './filter-dropdown.vue'
	// #endif
DCloud_JSON's avatar
DCloud_JSON 已提交
25 26 27 28
/**
 * Th 表头
 * @description 表格内的表头单元格组件
 * @tutorial https://ext.dcloud.net.cn/plugin?id=3270
29
 * @property {Number | String} 	width 	单元格宽度(支持纯数字、携带单位px或rpx)
DCloud_JSON's avatar
DCloud_JSON 已提交
30 31 32 33
 * @property {Boolean} 	sortable 					是否启用排序
 * @property {Number} 	align = [left|center|right]	单元格对齐方式
 * @value left   	单元格文字左侧对齐
 * @value center	单元格文字居中
34 35 36
 * @value right		单元格文字右侧对齐
 * @property {Array}	filterData 筛选数据
 * @property {String}	filterType	[search|select] 筛选类型
DCloud_JSON's avatar
DCloud_JSON 已提交
37
 * @value search	关键字搜素
38
 * @value select	条件选择
DCloud_JSON's avatar
DCloud_JSON 已提交
39 40 41 42 43 44
 * @event {Function} sort-change 排序触发事件
 */
export default {
	name: 'uniTh',
	options: {
		virtualHost: true
45 46 47 48 49
	},
	components: {
		// #ifdef H5
		dropdown
		// #endif
DCloud_JSON's avatar
DCloud_JSON 已提交
50
	},
51
	emits:['sort-change','filter-change'],
DCloud_JSON's avatar
DCloud_JSON 已提交
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
	props: {
		width: {
			type: [String, Number],
			default: ''
		},
		align: {
			type: String,
			default: 'left'
		},
		rowspan: {
			type: [Number, String],
			default: 1
		},
		colspan: {
			type: [Number, String],
			default: 1
		},
		sortable: {
			type: Boolean,
			default: false
		},
		filterType: {
			type: String,
			default: ""
		},
		filterData: {
			type: Array,
			default () {
				return []
			}
82 83 84 85 86 87
		},
		filterDefaultValue: {
			type: [Array,String],
			default () {
				return ""
			}
DCloud_JSON's avatar
DCloud_JSON 已提交
88 89 90 91 92 93 94 95 96
		}
	},
	data() {
		return {
			border: false,
			ascending: false,
			descending: false
		}
	},
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
	computed: {
		// 根据props中的width属性 自动匹配当前th的宽度(px)
		customWidth(){
			if(typeof this.width === 'number'){
				return this.width
			} else if(typeof this.width === 'string') {
				let regexHaveUnitPx = new RegExp(/^[1-9][0-9]*px$/g)
				let regexHaveUnitRpx = new RegExp(/^[1-9][0-9]*rpx$/g)
				let regexHaveNotUnit = new RegExp(/^[1-9][0-9]*$/g)
				if (this.width.match(regexHaveUnitPx) !== null) { // 携带了 px
					return this.width.replace('px', '')
				} else if (this.width.match(regexHaveUnitRpx) !== null) { // 携带了 rpx
					let numberRpx = Number(this.width.replace('rpx', ''))
					let widthCoe = uni.getSystemInfoSync().screenWidth / 750
					return Math.round(numberRpx * widthCoe)
				} else if (this.width.match(regexHaveNotUnit) !== null) { // 未携带 rpx或px 的纯数字 String
					return this.width
				} else { // 不符合格式
					return ''
				}
			} else {
				return ''
			}
		},
DCloud_JSON's avatar
DCloud_JSON 已提交
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
		contentAlign() {
			let align = 'left'
			switch (this.align) {
				case 'left':
					align = 'flex-start'
					break
				case 'center':
					align = 'center'
					break
				case 'right':
					align = 'flex-end'
					break
			}
			return align
		}
	},
	created() {
		this.root = this.getTable('uniTable')
		this.rootTr = this.getTable('uniTr')
study夏羽's avatar
update  
study夏羽 已提交
140
		this.rootTr.minWidthUpdate(this.customWidth ? this.customWidth : 140)
DCloud_JSON's avatar
DCloud_JSON 已提交
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
		this.border = this.root.border
		this.root.thChildren.push(this)
	},
	methods: {
		sort() {
			if (!this.sortable) return
			this.clearOther()
			if (!this.ascending && !this.descending) {
				this.ascending = true
				this.$emit('sort-change', { order: 'ascending' })
				return
			}
			if (this.ascending && !this.descending) {
				this.ascending = false
				this.descending = true
				this.$emit('sort-change', { order: 'descending' })
				return
			}

			if (!this.ascending && this.descending) {
				this.ascending = false
				this.descending = false
				this.$emit('sort-change', { order: null })
			}
		},
		ascendingFn() {
			this.clearOther()
			this.ascending = !this.ascending
			this.descending = false
			this.$emit('sort-change', { order: this.ascending ? 'ascending' : null })
		},
		descendingFn() {
			this.clearOther()
			this.descending = !this.descending
			this.ascending = false
			this.$emit('sort-change', { order: this.descending ? 'descending' : null })
		},
		clearOther() {
			this.root.thChildren.map(item => {
				if (item !== this) {
					item.ascending = false
					item.descending = false
				}
				return item
			})
186 187 188
		},
		ondropdown(e) {
			this.$emit("filter-change", e)
DCloud_JSON's avatar
DCloud_JSON 已提交
189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208
		},
		/**
		 * 获取父元素实例
		 */
		getTable(name) {
			let parent = this.$parent
			let parentName = parent.$options.name
			while (parentName !== name) {
				parent = parent.$parent
				if (!parent) return false
				parentName = parent.$options.name
			}
			return parent
		}
	}
}
</script>

<style lang="scss">
$border-color: #ebeef5;
209
$uni-primary: #007aff !default;
DCloud_JSON's avatar
DCloud_JSON 已提交
210 211 212 213 214 215 216 217 218 219 220

.uni-table-th {
	padding: 12px 10px;
	/* #ifndef APP-NVUE */
	display: table-cell;
	box-sizing: border-box;
	/* #endif */
	font-size: 14px;
	font-weight: bold;
	color: #909399;
	border-bottom: 1px $border-color solid;
221 222 223 224 225 226 227
}

.uni-table-th-row {
	/* #ifndef APP-NVUE */
	display: flex;
	/* #endif */
	flex-direction: row;
DCloud_JSON's avatar
DCloud_JSON 已提交
228 229 230 231 232 233 234
}

.table--border {
	border-right: 1px $border-color solid;
}
.uni-table-th-content {
	display: flex;
235
	align-items: center;
DCloud_JSON's avatar
DCloud_JSON 已提交
236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263
	flex: 1;
}
.arrow-box {
}
.arrow {
	display: block;
	position: relative;
	width: 10px;
	height: 8px;
	// border: 1px red solid;
	left: 5px;
	overflow: hidden;
	cursor: pointer;
}
.down {
	top: 3px;
	::after {
		content: '';
		width: 8px;
		height: 8px;
		position: absolute;
		left: 2px;
		top: -5px;
		transform: rotate(45deg);
		background-color: #ccc;
	}
	&.active {
		::after {
264
			background-color: $uni-primary;
DCloud_JSON's avatar
DCloud_JSON 已提交
265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280
		}
	}
}
.up {
	::after {
		content: '';
		width: 8px;
		height: 8px;
		position: absolute;
		left: 2px;
		top: 5px;
		transform: rotate(45deg);
		background-color: #ccc;
	}
	&.active {
		::after {
281
			background-color: $uni-primary;
DCloud_JSON's avatar
DCloud_JSON 已提交
282 283 284
		}
	}
}
285
</style>