index.vue 4.74 KB
<template>
  <div>
    <el-table
      :data="tableData"
      border
      style="width: 100%;max-width: 600px;border: 1px solid #6C6C6C;"
			:style="{
				width: tableWidth + 'px',
			}"
			:height="tableHeight + 'px'"
      :cell-style="{borderColor: '#c4c4c4'}"
      :header-cell-style="{borderColor: '#6C6C6C'}"
    >
      <el-table-column
        v-for="(item, index) in tableColums"
        :key="index"
        :prop="item.prop"
        :label="item.label"
        :width="formatTableColumnWidth(item.width)"
      >
        <template slot-scope="scope" v-if="tableData.length > 0">
          <button
            v-if="scope.row[item.prop].isButton === 'Y'"
            disabled
            class="cus-btn"
          >{{ scope.row[item.prop].value }}</button>
					<div v-else>
						<div v-if="scope.row[item.prop].viewType == 'Checkbox'" style="text-align: center;">
							<el-checkbox
								class="checkbox"
								size="mini"
								disabled
								suffix-icon="el-icon-arrow-down"
								v-model.trim="scope.row[item.prop].value"
								style="font-size: 10px;line-height: 22px;width: 20px;height: 20px;float: none;"
								true-label="1"
								false-label="0"
								:style="{
									borderBottom: ['1', '2'].includes(scope.row[item.prop].isRedLine) ? borderLineType[scope.row[item.prop].isRedLine] : '',
								}"
							></el-checkbox>
						</div>
						<div
							v-else
							style="width: 100%;min-height: 23px;"
							:style="{
								borderBottom: ['1', '2'].includes(scope.row[item.prop].isRedLine) ? borderLineType[scope.row[item.prop].isRedLine] : '',
								color: scope.row[item.prop].redCode == '1' ? 'red' : ''
							}"
						>{{ scope.row[item.prop].value || '&nbsp;' }}</div>
					</div>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>
<script>
  import { cloneDeep } from 'lodash'
  export default {
    name: 'Table',
    props: {
      gridValues: {
        type: Array,
        default: () => []
			},
			tableWidth: {
				type: String,
        default: '100%'
			},
			tableHeight: {
				type: String,
        default: '100'
			}
    },
    computed: {
      tableColums () {
        console.log('this.gridValues', this.gridValues)
        let list = this.gridValues.filter((item) => {
          return item.row === '0'
        })
        let tableColums = []
        list.map((item, index) => {
          tableColums.push({
            prop: 'prop' + index,
            label: item.value,
            width: Number(item.width) < 50 ? 50 : Number(item.width)
          })
        })
        console.log('tableColums', tableColums)
        return tableColums
      },
      tableData () {
        let list = this.gridValues.filter((item) => {
          return item.row !== '0'
        })
				let tableData = []
				if (list.length > 0) {
					let obj = {}
					this.tableColums.map((item) => {
						obj[item.prop] = ''
					})
					let rowNumber = Number(list[list.length - 1].row)
					for (let i = 0; i < rowNumber; i++) {
						tableData.push(cloneDeep(obj))
						list.map((item, ind) => {
							if ((i + 1) === Number(item.row)) {
								tableData[i]['prop' + item.column] = {
									...item,
									value: item.value,
									isRedLine: item.isRedLine,
									isButton: item.isButton,
								}
							}
						})
					}
				}
				let flagColu = this.tableColums.some((item) => {
					return item.label == 'Disp.'
				})
				if (flagColu) {
					tableData.map((item) => {
						let keyList = Object.keys(item)
						keyList.map((keyItem, keyInd) => {
							if (item[keyItem].value == 'D' && this.tableColums[keyInd].label == '') {
								item[keyItem].redCode = '1'
								if (keyInd - 2 > 0 && this.tableColums[keyInd - 2].label == 'Disp.') {
									item[keyList[keyInd - 2]].redCode = '1'
								}
							}
						})
					})
				}
        return tableData
      }
    },
    data () {
      return {
        borderLineType: {
					'1': '2px solid red',
					'2': '2px solid green'
				}
      }
    },
    methods: {
			formatTableColumnWidth(val) {
				if (Number(val) < 100) {
					return 100
				} else if (Number(val) > 240) {
					return 240
				} else {
					return val
				}
			}
		}
  }
</script>
<style lang="less" scoped>
::v-deep .el-table th .cell {
  padding: 0!important;
  overflow: hidden;
  white-space: nowrap;
  font-size: 10px;
  color: black;
}
::v-deep .el-table th {
	background-color: #F0F0F0;
	line-height: 22px!important;
}
::v-deep .el-table td {
	padding: 0!important;
	line-height: 22px!important;
}
::v-deep .el-table td .cell {
  padding: 0!important;
  overflow: hidden;
  white-space: nowrap;
  font-size: 10px;
  color: black;
}
.cus-btn {
  background-color: #fff!important;
  color: black!important;
  font-size: 10px;
  padding: 0;overflow: hidden;white-space: nowrap;
  font-weight: bold;
}
</style>