uni-tr.vue 3.9 KB
Newer Older
DCloud_JSON's avatar
DCloud_JSON 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 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
<template>
	<!-- #ifdef H5 -->
	<tr class="uni-table-tr">
		<th v-if="selection === 'selection' && ishead" class="checkbox" :class="{ 'tr-table--border': border }">
			<table-checkbox :checked="checked" :indeterminate="indeterminate" :disabled="disabled" @checkboxSelected="checkboxSelected"></table-checkbox>
		</th>
		<slot></slot>
		<!-- <uni-th class="th-fixed">123</uni-th> -->
	</tr>
	<!-- #endif -->
	<!-- #ifndef H5 -->
	<view class="uni-table-tr">
		<view v-if="selection === 'selection' " class="checkbox" :class="{ 'tr-table--border': border }">
			<table-checkbox :checked="checked" :indeterminate="indeterminate" :disabled="disabled" @checkboxSelected="checkboxSelected"></table-checkbox>
		</view>
		<slot></slot>
	</view>
	<!-- #endif -->
</template>

<script>
	import tableCheckbox from './table-checkbox.vue'
/**
 * Tr 表格行组件
 * @description 表格行组件 仅包含 th,td 组件
 * @tutorial https://ext.dcloud.net.cn/plugin?id=
 */
export default {
	name: 'uniTr',
	components: { tableCheckbox },
	props: {
		disabled: {
			type: Boolean,
			default: false
		},
		keyValue: {
			type: [String, Number],
			default: ''
		}
	},
	options: {
		virtualHost: true
	},
	data() {
		return {
			value: false,
			border: false,
			selection: false,
			widthThArr: [],
			ishead: true,
			checked: false,
			indeterminate:false
		}
	},
	created() {
		this.root = this.getTable()
		this.head = this.getTable('uniThead')
		if (this.head) {
			this.ishead = false
			this.head.init(this)
		}
		this.border = this.root.border
		this.selection = this.root.type
		this.root.trChildren.push(this)
		const rowData = this.root.data.find(v => v[this.root.rowKey] === this.keyValue)
		if(rowData){
			this.rowData = rowData
		}
		this.root.isNodata()
	},
	mounted() {
		if (this.widthThArr.length > 0) {
			const selectionWidth = this.selection === 'selection' ? 50 : 0
			this.root.minWidth = this.widthThArr.reduce((a, b) => Number(a) + Number(b)) + selectionWidth
		}
	},
study夏羽's avatar
update  
study夏羽 已提交
77
	// #ifndef VUE3
DCloud_JSON's avatar
DCloud_JSON 已提交
78 79 80 81
	destroyed() {
		const index = this.root.trChildren.findIndex(i => i === this)
		this.root.trChildren.splice(index, 1)
		this.root.isNodata()
study夏羽's avatar
update  
study夏羽 已提交
82 83 84 85 86 87 88 89 90
	},
	// #endif
	// #ifdef VUE3
	unmounted() {
		const index = this.root.trChildren.findIndex(i => i === this)
		this.root.trChildren.splice(index, 1)
		this.root.isNodata()
	},
	// #endif
DCloud_JSON's avatar
DCloud_JSON 已提交
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 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 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
	methods: {
		minWidthUpdate(width) {
			this.widthThArr.push(width)
		},
		// 选中
		checkboxSelected(e) {
			let rootData = this.root.data.find(v => v[this.root.rowKey] === this.keyValue)
			this.checked = e.checked
			this.root.check(rootData||this, e.checked,rootData? this.keyValue:null)
		},
		change(e) {
			this.root.trChildren.forEach(item => {
				if (item === this) {
					this.root.check(this, e.detail.value.length > 0 ? true : false)
				}
			})
		},
		/**
		 * 获取父元素实例
		 */
		getTable(name = 'uniTable') {
			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;

.uni-table-tr {
	/* #ifndef APP-NVUE */
	display: table-row;
	transition: all 0.3s;
	box-sizing: border-box;
	/* #endif */
}

.checkbox {
	padding: 0 8px;
	width: 26px;
	padding-left: 12px;
	/* #ifndef APP-NVUE */
	display: table-cell;
	vertical-align: middle;
	/* #endif */
	color: #333;
	font-weight: 500;
	border-bottom: 1px $border-color solid;
	font-size: 14px;
	// text-align: center;
}

.tr-table--border {
	border-right: 1px $border-color solid;
}

/* #ifndef APP-NVUE */
.uni-table-tr {
	::v-deep .uni-table-th {
		&.table--border:last-child {
			// border-right: none;
		}
	}

	::v-deep .uni-table-td {
		&.table--border:last-child {
			// border-right: none;
		}
	}
}

/* #endif */
芊里 已提交
171
</style>