Fixiaobai
2023-11-10 7d960355fd82a106ae8e15f6f83d4324145e7b24
src/views/common/param-mergeTemplate.vue
@@ -1,29 +1,50 @@
<template>
  <div>
        <avue-crud
        ref="crud"
        class="l-mes"
        rowKey="oneId"
        :data="tableData"
        :option="option"
        :span-method="spanMethod"
        :page="page"
        :table-loading="loading"
        >
        <avue-crud ref="crud" class="l-mes" rowKey="oneId" :data="tableData" :option="option" :span-method="spanMethod"
            :page="page" :table-loading="loading">
        <template slot="code" slot-scope="scope">
            <avue-text-ellipsis use-tooltip :text="scope.row.code" :height="40" :width="100">
                <small slot="more">...</small>
            </avue-text-ellipsis>
        </template>
            <template slot="paramValue" slot-scope="scope">
                <el-input @blur="checkType(scope.row)" :readonly="routingTemplateId!=null" :placeholder="'请输入'+scope.row.paramType" v-model="scope.row.paramValue"/>
                <el-input @blur="checkType(scope.row)" :readonly="routingTemplateId != null"
                    :placeholder="'请输入' + scope.row.paramType" v-model="scope.row.paramValue" />
            </template>
        </avue-crud>
        <!-- <el-table :data="tableData" ref="commonTree" :span-method="objectSpanMethod" border row-key="id" style="width: 100%">
            <el-table-column prop="code" label="编码"></el-table-column>
            <el-table-column prop="paramItem" label="一级"></el-table-column>
            <el-table-column prop="paramItemTwo" label="二级"></el-table-column>
            <el-table-column prop="paramItemThree" label="三级"></el-table-column>
            <el-table-column prop="paramType" label="类型"></el-table-column>
            <el-table-column prop="paramValue" label="值"></el-table-column>
            <el-table-column prop="unit" label="单位"></el-table-column>
        </el-table> -->
  </div>
</template>
<script>
export default {
    // props: ["tableTreeData"],
    data() {
        return {
            checkType(row) {
                if (row.paramType == '数值格式') {
                    let val = row.paramValue
                    const reg = /^[0-9]*$/
                    if (!reg.test(val)) {
                        row.paramValue = ''
                        this.$message.warning("请输入数值格式的数据!")
                    }
                }
            },
            mergeObj: {},
            mergeArr: ['code','paramItem', 'paramItemTwo',],
            tableData: [],
            // routingTemplateId: null
        }
    },
    props:{
       option:{
         type: Object,
@@ -89,7 +110,7 @@
         }
         }
       },
       tableData:{
        tableTreeData: {
         type: Array,
         default: () => {
            return []
@@ -132,35 +153,89 @@
         }
       }
    },
    created(){
        // this.rowSort()
        // this.rowCalc()
    },
    beforeMount(){
    watch:{
        tableTreeData:{
            handler(old,newval){
            console.log(this.tableTreeData);
            this.tableData=JSON.parse(JSON.stringify(this.tableTreeData))
            // this.getSpanArr(this.tableData);
        this.rowSort()
        this.rowCalc()
    },
    mounted(){
            deep: true
        }
    },
    data(){
        return {
            checkType(row){
                if(row.paramType == '数值格式'){
                    let val = row.paramValue
                    const reg = /^[0-9]*$/
                    if(!reg.test(val)){
                        row.paramValue = ''
                        this.$message.warning("请输入数值格式的数据!")
                    }
                }
            }
        }
    created() {
        // this.$nextTick(() => {
        //     this.$refs.crud.doLayout()
        // })
        this.tableData=JSON.parse(JSON.stringify(this.tableTreeData))
        this.rowSort()
        this.rowCalc()
    },
    beforeMount() {
        // this.rowSort()
        // this.rowCalc()
        // this.$nextTick(()=>{
        // })
    },
    beforeDestroy() {
    },
    mounted() {
    },
    activated(){
        this.doLayout()
    },
    methods:{
        doLayout(){
            let that=this
            this.$nextTick(()=>{
                that.$refs.commonTree.dolayout()
            })
        },
            getSpanArr(data) {
            this.mergeArr.forEach((key, index1) => {
                let count = 0; // 用来记录需要合并行的起始位置
                this.mergeObj[key] = []; // 记录每一列的合并信息
                data.forEach((item, index) => {
                    // index == 0表示数据为第一行,直接 push 一个 1
                    if(index === 0) {
                        this.mergeObj[key].push(1);
                    } else {
                        // 判断当前行是否与上一行其值相等 如果相等 在 count 记录的位置其值 +1 表示当前行需要合并 并push 一个 0 作为占位
                        if(item[key] === data[index - 1][key]) {
                            this.mergeObj[key][count] += 1;
                            this.mergeObj[key].push(0);
                        } else {
                            // 如果当前行和上一行其值不相等
                            count = index; // 记录当前位置
                            this.mergeObj[key].push(1); // 重新push 一个 1
                        }
                    }
                })
            })
        },
        // 默认接受四个值 { 当前行的值, 当前列的值, 行的下标, 列的下标 }
        objectSpanMethod({ row, column, rowIndex, columnIndex }) {
            // 判断列的属性
            if(this.mergeArr.indexOf(column.property) !== -1) {
                // 判断其值是不是为0
                if(this.mergeObj[column.property][rowIndex]) {
                    return [this.mergeObj[column.property][rowIndex], 1]
                } else {
                    // 如果为0则为需要合并的行
                    return [0, 0];
                }
            }
        },
        //动态合并方法
        rowCalc() {
            this.spanArr.forEach((ele, index) => {
                let parent
                let parent = null
                if (index !== 0) parent = this.spanArr[ele.parent || index - 1].span
                ele.span = this.rowSpan(ele.prop, parent)
            })