中文字幕av专区_日韩电影在线播放_精品国产精品久久一区免费式_av在线免费观看网站

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

怎么用vue+elemet實現表格手動合并行列

發布時間:2022-08-30 15:05:19 來源:億速云 閱讀:151 作者:iii 欄目:開發技術

本篇內容介紹了“怎么用vue+elemet實現表格手動合并行列”的有關知識,在實際案例的操作過程中,不少人都會遇到這樣的困境,接下來就讓小編帶領大家學習一下如何處理這些情況吧!希望大家仔細閱讀,能夠學有所成!

1.初始化一個element的table表格,選中一個單元格選擇合并行和列,參考element文檔需要使用到的方法是objectSpanMethod,注意:objectSpanMethod參數行和列都是從0開始,比如第一行第一列單元格位置是(0,0),要合并的行和列最小為1

2.實現效果如下:

怎么用vue+elemet實現表格手動合并行列

怎么用vue+elemet實現表格手動合并行列

怎么用vue+elemet實現表格手動合并行列

3.代碼如下:

<template>
    <div class="content">
        <div class="table_box" v-if="show">
             <el-table
                ref="singleTable"
                :data="tableData"
                highlight-current-row
                @current-change="handleCurrentChange"
                border
                :span-method="objectSpanMethod"
                @cell-dblclick = "dbclick"
                :cell-class-name="tableCellClassName"
                @cell-click="cellClick"
               >
                <el-table-column
                    v-for="(item,key,index) in tableColumns"
                    :key="index"
                    :label="item.label"
                    :prop="item.children?'':item.prop"
                    
                    width="180">
                    <el-popover
                        placement="right"
                        width="400"
                        slot-scope="scope"
                        trigger="click">
                        <el-button size="mini" type="primary" @click="dialogVisible = true">合并單元格</el-button>
                        <el-button size="mini" type="primary" @click="addTr('top',selectTab.tr)">向上插入一行</el-button>
                        <el-button size="mini" type="primary" @click="addTr('bottom',selectTab.tr)">向下插入一行</el-button>
                        <div slot="reference">
                            <span>{{ scope.row[item.prop] }}</span>
                        </div>
                    </el-popover>
                    <div v-if="item.children && item.children.length>0">
                        <el-table-column
                            v-for="(val,i) in item.children"
                            :key="i"
                            :prop="val.prop"
                            :label="val.label"
                            width="180">
                            <!-- <template slot-scope="scope">
                                <span>{{ scope.row[val.prop] }}22</span>
                            </template> -->
                            <el-popover
                                placement="right"
                                width="400"
                                slot-scope="scope"
                                trigger="click">
                                <el-button size="mini" type="primary" @click="dialogVisible = true">合并單元格</el-button>
                        <el-button size="mini" type="primary" @click="addTr('top',selectTab.tr)">向上插入一行</el-button>
                        <el-button size="mini" type="primary" @click="addTr('bottom',selectTab.tr)">向下插入一行</el-button>
                                <div slot="reference">
                                    <span>{{ scope.row[val.prop] }}</span>
                                </div>
                            </el-popover>
                        </el-table-column>
                    </div>
                </el-table-column>
                
            </el-table>
        </div>
        <el-dialog
        title="合并"
        :visible.sync="dialogVisible"
        width="300px"
        >
        <el-form ref="form" label-width="80px">
            <el-form-item label="合并行">
                <el-input :min="0" v-model="selectTab.trSpan"/>
            </el-form-item>
            <el-form-item label="合并列">
                <el-input :min="0" v-model="selectTab.tdSpan"/>
            </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
            <el-button @click="dialogVisible = false" size="small">取 消</el-button>
            <el-button type="primary" @click="addMerge" size="small">確 定</el-button>
        </span>
        </el-dialog>
    </div>
</template>
<script>
export default {
    data(){
        return{
            tableData:[],//表格數據
            tableColumns:[],//表頭數據    
            show:false,
            currentRow: null,
            spanArr:[],
            selectTab:{
                tr:0,//第幾行
                td:0,//第幾列
                trSpan:1,//占幾行
                tdSpan:1,//占幾列
            },
            mergeList:[],//記錄合并詳情
            dialogVisible:false,
        }
    },
    watch:{
        // mergeList:{//監聽合并列表項
        //     handler(newVal,oldVal){
        //         console.log('刷新');
        //         this.show = false;
        //         this.$nextTick(()=>{
        //             this.show = true;
        //         })
        //     },
        //     deep:true
        // }
    },
    mounted(){
        this.init();
    },
    methods:{
        tableCellClassName({row, column, rowIndex, columnIndex}){
            //注意這里是解構
            //利用單元格的 className 的回調方法,給行列索引賦值
            row.index=rowIndex;
            column.index=columnIndex;
        },
        cellClick(row, column, cell, event){
            console.log('rowIndex',row.index);
            console.log('colIndex',column.index);
            this.selectTab.tr = row.index+1;
            this.selectTab.td = column.index+1;
        },

        dbclick(row, column, cell, event){
            console.log('row:',row);
            console.log('column:',column,);
            console.log('cell:',cell);
            console.log('event:',event);
            this.selectTab.tr = row.index+1;
            this.selectTab.td = column.index+1;
        },
        addMerge(){//合并項添加進合并列表中
            // let obj = this.mergeList.filter(item=>item.td==this.selectTab.td&&item.tr)
            let flag = false;
            let w = -1;
            this.mergeList.forEach((item,index)=>{
                if(item.td==this.selectTab.td && item.tr==this.selectTab.tr){//是否有重復項,有則替換
                    flag = true;
                    w = index;
                }
            })
            if(flag){
                this.mergeList[w] = Object.assign({},this.selectTab);
            }else{
                this.mergeList.push(Object.assign({},this.selectTab));
            }
           
            this.dialogVisible = false;
            this.show = false;//手動刷新
                this.$nextTick(()=>{
                    this.show = true;
                })
        },
        objectSpanMethod({ row, column, rowIndex, columnIndex }) {//表格的合并函數
            //console.log('列',columnIndex,rowIndex);
            //console.log('行',row,column);0,2
          
            // let td = Number(this.selectTab.td)-1;//columnIndex從0開始
            // let tr = Number(this.selectTab.tr)-1;//rowIndex從0開始
            // if( columnIndex>=td && columnIndex<=((Number(td)+Number(this.selectTab.trSpan))-1)){
            //     if( rowIndex>=tr && rowIndex<=((Number(tr)+Number(this.selectTab.tdSpan))-1)){
            //         if(columnIndex==td && rowIndex==tr){
            //             return [this.selectTab.tdSpan,this.selectTab.trSpan]
            //         }else{
            //             console.log('選擇的行列',columnIndex,rowIndex);
            //             console.log('選擇的列',this.selectTab.td,(Number(this.selectTab.td)+Number(this.selectTab.tdSpan))-1);
            //             console.log('選擇的行',this.selectTab.tr,(Number(this.selectTab.tr)+Number(this.selectTab.trSpan))-1);
            //             return [0,0]
            //         }
            //     }
            // }
            if(this.mergeList.length>0){
                console.log('merge',this.mergeList);
                for(let i=0;i<this.mergeList.length;i++){
                    let item = this.mergeList[i];
                    let td = Number(item.td)-1;//columnIndex從0開始
                    let tr = Number(item.tr)-1;//rowIndex從0開始
                    if( columnIndex>=td && columnIndex<=((Number(td)+Number(item.trSpan))-1)){
                        if( rowIndex>=tr && rowIndex<=((Number(tr)+Number(item.tdSpan))-1)){
                            if(columnIndex==td && rowIndex==tr){
                                console.log('選中的行列',columnIndex,rowIndex,item.tdSpan,item.trSpan);
                                return [item.tdSpan,item.trSpan]
                            }else{
                                console.log('選擇的行列',columnIndex,rowIndex);
                                // console.log('選擇的列',this.selectTab.td,(Number(this.selectTab.td)+Number(this.selectTab.tdSpan))-1);
                                // console.log('選擇的行',this.selectTab.tr,(Number(this.selectTab.tr)+Number(this.selectTab.trSpan))-1);
                                return [0,0]
                            }
                        }
                    }
                }
            }
            //forEach中使用return不會中斷,相當于continue
            // this.mergeList.forEach(item=>{
            //     let td = Number(item.td)-1;//columnIndex從0開始
            //     let tr = Number(item.tr)-1;//rowIndex從0開始
            //     if( columnIndex>=td && columnIndex<=((Number(td)+Number(item.trSpan))-1)){
            //         if( rowIndex>=tr && rowIndex<=((Number(tr)+Number(item.tdSpan))-1)){
            //             if(columnIndex==td && rowIndex==tr){
            //                 console.log('選中的行列',columnIndex,rowIndex,item.tdSpan,item.trSpan);
            //                 return [item.tdSpan,item.trSpan]
            //             }else{
            //                 console.log('選擇的行列',columnIndex,rowIndex);
            //                 // console.log('選擇的列',this.selectTab.td,(Number(this.selectTab.td)+Number(this.selectTab.tdSpan))-1);
            //                 // console.log('選擇的行',this.selectTab.tr,(Number(this.selectTab.tr)+Number(this.selectTab.trSpan))-1);
            //                 return [0,0]
            //             }
            //         }
            //     }
            // })
            
        },
        init(){//初始化表格
            // if(this.tdCount==0){
            //     this.tableColumns = [{label:'默認',prop:'default'}];
            //     this.tableData = [{default:0}];
            // }
            this.tableColumns = [
                {label:'默認',prop:'default'},
                {label:'雙十一',prop:'',children:[{label:'庫存',prop:'count'},{label:'銷量',prop:'sale'}]},
            ];
            this.tableData = [
                {default:0,count:111,sale:100},
                 {default:0,count:173,sale:220},
                  {default:0,count:89,sale:120}
            ];
            this.show = true;
            
        },
        addTab(){//插入表頭
            if(this.tableColumns.every(item=>item.label!=this.tab.label)){
                this.tableColumns.push(Object.assign({},this.tab));
            }else{
                this.$notify.error({
                    title: '錯誤',
                    message: '表頭重復'
                });
            }
        },
        addTr(way,index){//插入一行數據
        
            let obj = {};
            // this.tableColumns.forEach(item=>{
            //     obj[item.prop] = Math.floor(Math.random()*100);
            // })
            obj = this.getObj(this.tableColumns,obj);
            console.log('obj',obj);
            if(way=='top'){//在第index行之前插入一行
                this.tableData.splice(index-1,0,obj);
            }else{在第index行之后插入一行
                this.tableData.splice(index,0,obj);
            }
           
           
        },
        getObj(tabs,obj){
            tabs.forEach(item=>{
                if(item.children){
                    this.getObj(item.children,obj);
                }else{
                    obj[item.prop] = Math.floor(Math.random()*100);
                }
            })
            return obj;
        },
        setCurrent(row) {
            this.$refs.singleTable.setCurrentRow(row);
        },
        handleCurrentChange(val) {//點擊行事件
            console.log('val',val);
            this.currentRow = val;
        },
    }
}
</script>
<style scoped>
</style>

“怎么用vue+elemet實現表格手動合并行列”的內容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業相關的知識可以關注億速云網站,小編將為大家輸出更多高質量的實用文章!

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

民县| 奎屯市| 通化市| 自贡市| 定结县| 陕西省| 新源县| 密云县| 芦山县| 江西省| 乐清市| 安多县| 宿州市| 江永县| 阿克| 东明县| 通榆县| 岳池县| 许昌市| 建水县| 文化| 武义县| 老河口市| 永修县| 来凤县| 吉木萨尔县| 邢台县| 佛山市| 阳泉市| 五河县| 广丰县| 泗水县| 龙游县| 仲巴县| 广河县| 康保县| 沧源| 平凉市| 乐业县| 鲁山县| 五家渠市|