zouyu
2023-11-13 ba296fa0cc7711ea745564bda80b2d1d8a3a09f0
src/views/common/param-mergeTemplate.vue
@@ -1,166 +1,218 @@
<template>
  <div>
        <avue-crud
        ref="crud"
        class="l-mes"
        rowKey="oneId"
        :data="tableData"
        :option="option"
        :span-method="spanMethod"
    <div>
        <avue-crud
        ref="crud"
        class="l-mes"
        rowKey="id"
        :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>
        :table-loading="loading">
            <template slot="code" slot-scope="scope">
                <avue-text-ellipsis use-tooltip :text="scope.row.code" :height="textEllipsisHeight" :width="textEllipsisWidth">
                    <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)" :disabled="routingTemplateId!=null || !scope.row.isUpdate" :placeholder="'请输入'+scope.row.paramType" v-model="scope.row.paramValue"/>
            </template>
            <template slot="menu" slot-scope="scope">
                <el-button type="text" v-if="scope.row.isUpdate && isModel" size="mini"
                @click="handleParamSave(scope.$index, scope.row)">保存</el-button>
                <el-button type="text" v-if="!scope.row.isUpdate && isModel" size="mini"
                @click="handleParamEdit(scope.$index, scope.row)">编辑</el-button>
                <el-button type="text" size="mini" v-if="isModel"
                @click.stop="handleParamInsert(scope.row)">插入</el-button>
                <el-button type='text' :disabled="routingTemplateId!=null"
                @click.stop="handleParamDelete(scope.row, scope.index)">删除</el-button>
            </template>
        </avue-crud>
  </div>
    </div>
</template>
<script>
export default {
    props:{
       option:{
         type: Object,
         default: ()=>{
            return {
            height: 600,
            loadingText: "加载中...",
            columnBtn: false,
            index: true,
            indexLabel: '序号',
            menu: false,
            menuAlign: 'center',
            editBtn: false,
            delBtn: false,
            addBtn: false,
            border: true,
            size: 'small',
            refreshBtn: false,
            align: 'center',
            column: [
            {
                label: '参数编号',
                prop: 'code',
                slot: true
            },
            {
                label: '参数项',
                prop: 'paramItem',
            },
            {
                label: '二级参数',
                prop: 'paramItemTwo',
            },
            {
                label: '三级参数',
                prop: 'paramItemThree',
            },
            {
                label: '参数类型',
                prop: 'paramType',
                formslot: true,
                rules: [{
                required: true,
                message: "请选择参数类型",
                trigger: "change"
                }]
            },
            {
                label: '值',
                prop: 'paramValue',
                slot: true,
                formslot: true,
                labelslot: true
            }, {
                label: '单位',
                prop: 'unit',
                rules: [{
                required: true,
                message: "单位不能为空",
                trigger: "blur"
                }]
            },]
         }
         }
       },
       tableData:{
         type: Array,
         default: () => {
            return []
         },
         required: true
       },
       loading:{
        type: Boolean,
        default: false
       },
       page:{
         type: Object,
         default:()=>{
            return {
                total: 10,
                currentPage: 1,
                pageSize: 10
    props: {
        //是否为参数模板
        isModel:{
            type: Boolean,
            default:()=>{
                return false
            }
         }
       },
       spanArr:{
         type: Array,
         default: ()=>{
            return [{
                prop: 'code',
                span: []
            }, {
                prop: 'paramItem',
                span: []
            }, {
                prop: 'paramItemTwo',
                span: []
            }]
         }
       },
       routingTemplateId:{
         type: String,
         default: ()=>{
            return ''
         }
       }
        },
        textEllipsisWidth:{
            type: Number,
            default:()=>{
                return 100
            }
        },
        textEllipsisHeight:{
            type: Number,
            default:()=>{
                return 40
            }
        },
        option: {
            type: Object,
            default: () => {
                return {
                    height: 600,
                    columnBtn: false,
                    index: true,
                    indexLabel: '序号',
                    menu: true,
                    menuWidth: 100,
                    menuAlign: 'center',
                    editBtn: false,
                    delBtn: false,
                    addBtn: false,
                    border: true,
                    size: 'small',
                    refreshBtn: false,
                    align: 'center',
                    column: [
                        {
                            label: '参数编号',
                            prop: 'code',
                            slot: true
                        },
                        {
                            label: '参数项',
                            prop: 'paramItem',
                        },
                        {
                            label: '二级参数',
                            prop: 'paramItemTwo',
                        },
                        {
                            label: '三级参数',
                            prop: 'paramItemThree',
                        },
                        {
                            label: '参数类型',
                            prop: 'paramType',
                            formslot: true,
                            rules: [{
                                required: true,
                                message: "请选择参数类型",
                                trigger: "change"
                            }]
                        },
                        {
                            label: '值',
                            prop: 'paramValue',
                            slot: true,
                            formslot: true,
                            labelslot: true
                        }, {
                            label: '单位',
                            prop: 'unit',
                            rules: [{
                                required: true,
                                message: "单位不能为空",
                                trigger: "blur"
                            }]
                    },]
                }
            }
        },
        tableTreeData: {
            type: Array,
            default: () => {
                return []
            },
            required: true
        },
        page: {
            type: Object,
            default: () => {
                return {
                    total: 10,
                    currentPage: 1,
                    pageSize: 10
                }
            }
        },
        spanArr: {
            type: Array,
            default: () => {
                return [{
                    prop: 'code',
                    span: []
                }, {
                    prop: 'paramItem',
                    span: []
                }, {
                    prop: 'paramItemTwo',
                    span: []
                }]
            }
        },
        routingTemplateId: {
            type: String,
            default: () => {
                return null
            }
        },
        handleParamDelete:{
            type: Function
        },
        handleParamEdit:{
            type: Function
        },
        handleParamInsert:{
            type: Function
        },
        handleParamSave:{
            type: Function
        },
    },
    created(){
        // this.rowSort()
        // this.rowCalc()
    watch:{
        table(val) {
        this.doLayout()
        },
        tableTreeData:{
            handler(old,newval){
                this.loading = true
                this.tableData=JSON.parse(JSON.stringify(this.tableTreeData))
                this.rowSort()
                this.rowCalc()
                this.loading = false
            },
            deep: true
        },
    },
    beforeMount(){
    created() {
        this.loading = true
        this.tableData=JSON.parse(JSON.stringify(this.tableTreeData))
        this.rowSort()
        this.rowCalc()
    },
    mounted(){
        setTimeout(()=>{
            this.loading = false
        },1000)
    },
    data(){
        return {
            checkType(row){
                if(row.paramType == '数值格式'){
                    let val = row.paramValue
                    const reg = /^[0-9]*$/
                    if(!reg.test(val)){
                        row.paramValue = ''
                        this.$message.warning("请输入数值格式的数据!")
                    }
                }
            }
            tableData: [],
            loading: false,
        }
    },
    methods:{
        checkType(row){
            if(row.paramType == '数值格式'){
                let val = row.paramValue
                const reg = /^[0-9]*$/
                if(!reg.test(val)){
                    row.paramValue = ''
                    this.$message.warning("请输入数值格式的数据!")
                }
            }
        },
        //动态合并方法
        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)
            })
@@ -170,17 +222,17 @@
            this.spanArr.forEach((ele, index) => {
                let key = ele.prop
                this.tableData = this.tableData.sort((a, b) => {
                let flag = true;
                for (let i = 0; i < index; i++) {
                    let prop = this.spanArr[i].prop
                    flag = flag && a[prop] == b[prop]
                }
                if (flag) {
                    if (a[key] < b[key]) { return 1; }
                    else if (a[key] > b[key]) { return -1; }
                    let flag = true;
                    for (let i = 0; i < index; i++) {
                        let prop = this.spanArr[i].prop
                        flag = flag && a[prop] == b[prop]
                    }
                    if (flag) {
                        if (a[key] < b[key]) { return 1; }
                        else if (a[key] > b[key]) { return -1; }
                        return 0;
                    }
                    return 0;
                }
                return 0;
                })
            })
        },
@@ -189,21 +241,21 @@
            let position = 0;
            this.tableData.forEach((item, index) => {
                if (index === 0) {
                list.push(1)
                let position = 0;
                } else {
                if (this.tableData[index][key] === this.tableData[index - 1][key]) {
                    if (parent && parent[index] !== 0) {
                    list.push(1)
                    position = index
                    let position = 0;
                } else {
                    if (this.tableData[index][key] === this.tableData[index - 1][key]) {
                        if (parent && parent[index] !== 0) {
                            list.push(1)
                            position = index
                        } else {
                            list[position] += 1;
                            list.push(0)
                        }
                    } else {
                    list[position] += 1;
                    list.push(0)
                        list.push(1)
                        position = index
                    }
                } else {
                    list.push(1)
                    position = index
                }
                }
            })
            return list
@@ -212,12 +264,12 @@
            for (let i = 0; i < this.spanArr.length; i++) {
                const ele = this.spanArr[i]
                if (column.property == ele.prop) {
                const _row = ele.span[rowIndex];
                const _col = _row > 0 ? 1 : 0;
                return {
                    rowspan: _row,
                    colspan: _col
                }
                    const _row = ele.span[rowIndex];
                    const _col = _row > 0 ? 1 : 0;
                    return {
                        rowspan: _row,
                        colspan: _col
                    }
                }
            }
        }
@@ -226,7 +278,12 @@
</script>
<style>
 .avue-crud__menu{
.avue-crud_menu{
    display: none;
 }
}
.avue-crud .el-table th {
    word-break: break-word;
    color: rgba(102,102,102,1);
    background-color: white;
}
</style>