elementUI 動(dòng)態(tài)生成幾行幾列的方法示例
elementUI 動(dòng)態(tài)生成幾行幾列 table
現(xiàn)在碰到一個(gè)需求:就是根據(jù)用戶選擇的行列,來(lái)自動(dòng)生成相應(yīng)大小的 table,如下這個(gè)實(shí)現(xiàn)還不完善,因?yàn)閿?shù)據(jù)不對(duì),只是實(shí)現(xiàn)了動(dòng)態(tài)的效果,僅是提供一種實(shí)現(xiàn)思路吧,后續(xù)我會(huì)再想想看怎么實(shí)現(xiàn)為好,先記錄一下吧
直接看代碼吧
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>elementUI table 動(dòng)態(tài)生成列</title> <script src="https://cdn.jsdelivr.net/npm/vue"></script> <script src="https://unpkg.com/element-ui/lib/index.js"></script> <style type="text/css"> @import url("https://unpkg.com/element-ui/lib/theme-chalk/index.css"); </style> </head> <body> <div id="app"> <el-form inline> <!--先選擇 排數(shù)--> <el-form-item label="請(qǐng)選擇排" style="margin-left: 50px;"> <el-select style="width: 100% ;" v-model="row1" placeholder="請(qǐng)選擇排" @change="row1Change"> <el-option v-for="item in floorNumList" :key="item.floorId" :value="item.floorId"></el-option> </el-select> </el-form-item> <!--再選擇 列數(shù)--> <el-form-item label="請(qǐng)選擇列"> <el-select style="width: 100% ;" v-model="col1" placeholder="請(qǐng)選擇列" @change="col1Change"> <el-option v-for="item in floorNumList" :key="item.floorId" :value="item.floorId"></el-option> </el-select> </el-form-item> <el-table ref="multipleTable" :data="rowDataList1" style="width:80%; border: 2px solid red; max-height: 500px; margin-left: 30px;" highlight-current-row :cell-style="cellStyle"> <el-table-column fixed type="selection" align="center" width="50" label="列"></el-table-column> <!-- <el-table-column type="index" align="center" width="50" label="索引"></el-table-column>--> <el-table-column v-for="col in colDataList1" :prop="col.id" :label="col.id" align="center" > <el-table-column prop="id" align="center" > <template slot-scope="scope"> <el-button @click="handleClick(scope.row, col.id, scope.$index)" class="el-icon-cherry" v-bind:style="{ color: activeColor}">></el-button> </template> </el-table-column> </el-table-column> </el-table> </el-form> </div> </div> <script> let vm = new Vue({ el: '#app', data(){ return{ floorNumList: [ {floorId: 1}, {floorId: 2}, {floorId: 3}, {floorId: 4}, {floorId: 5}, {floorId: 6}, {floorId: 7}, {floorId: 8}, {floorId: 9}, {floorId: 10} ], floorNum: '', // 第1層 默認(rèn)選擇的排數(shù) 和 列數(shù) row1: 1, col1: 1, // 第2層 默認(rèn)選擇的排數(shù) 和 列數(shù) row2: 1, col2: 1, // 第3層 默認(rèn)選擇的排數(shù) 和 列數(shù) row3: 1, col3: 1, // 第4層 默認(rèn)選擇的排數(shù) 和 列數(shù) row4: 1, col4: 1, // 第5層 默認(rèn)選擇的排數(shù) 和 列數(shù) row5: 1, col5: 1, activeColor: 'green', colPos: '', rowPos: '', rowDataList1: [{ // 默認(rèn)給一個(gè)對(duì)象,即默認(rèn)狀態(tài)是 1行數(shù)據(jù) id: Math.ceil(Math.random()*100) }], colDataList1: [ {id: '1'} ], } }, methods:{ col1Change(){ // 每觸發(fā)一次,清空數(shù)據(jù) this.colDataList1 = [{id: '1'}]; // 取得 選中的第一層的第一排的數(shù)值 let len = this.col1; if(len > 1){ for (let i = 2; i <= len; i++){ this.colDataList1.push({id: i + ''}); } return this.colDataList1; }else{ return this.colDataList1; } }, row1Change(){ // 每觸發(fā)一次,清空數(shù)據(jù) this.rowDataList1 = [{ id: Math.ceil(Math.random()*100)}]; let len = this.row1; if (len > 1){ for (let i = 2; i <= len ; i++){ this.rowDataList1.push({id: Math.ceil(Math.random()*100) + i}); } return this.rowDataList1; }else { return this.rowDataList1; } }, handleClick(row, col, index) { // console.log(JSON.stringify(row)); // console.log(JSON.stringify(col)); // console.log("點(diǎn)擊的cell 行數(shù): " + JSON.stringify(index)); // index 是 行數(shù),0 表示第一行,從 0 開(kāi)始 // 一點(diǎn)擊獲取 行縱坐標(biāo) this.colPos = col; this.rowPos = index; }, cellStyle({row, column, rowIndex, columnIndex}){ // console.log(JSON.stringify(row)) // console.log(JSON.stringify(column)) // console.log("要渲染的行數(shù): " + JSON.stringify(rowIndex)) // console.log(JSON.stringify(columnIndex)) if(rowIndex == 0 && columnIndex == 0){ return ''; }else { if(rowIndex == this.rowPos && columnIndex == this.colPos){ //指定坐標(biāo) return 'background: pink'; }else{ return ''; } } }, } }); </script> </body> </html>
為了方便大家直接使用理解,我這里使用的腳本等都是在線鏈接,確保大家直接 down 下來(lái)就能運(yùn)行處效果的。
效果圖
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
vue-cli之引入Bootstrap問(wèn)題(遇到的坑,以及解決辦法)
這篇文章主要介紹了vue-cli之引入Bootstrap問(wèn)題(遇到的坑,以及解決辦法),具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-10-10vue路由權(quán)限和按鈕權(quán)限的實(shí)現(xiàn)示例
本文主要介紹了vue路由權(quán)限和按鈕權(quán)限的實(shí)現(xiàn)示例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-04-04淺談Vue.js之初始化el以及數(shù)據(jù)的綁定說(shuō)明
今天小編就為大家分享一篇淺談Vue.js之初始化el以及數(shù)據(jù)的綁定說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-11-11Vue中使用element-ui給按鈕綁定一個(gè)單擊事件實(shí)現(xiàn)點(diǎn)擊按鈕就彈出dialog對(duì)話框
最近遇到了個(gè)需求是使用element-ui插件編寫(xiě)頁(yè)面,點(diǎn)擊按鈕,彈出對(duì)話框,這篇文章主要給大家介紹了關(guān)于Vue中使用element-ui給按鈕綁定一個(gè)單擊事件實(shí)現(xiàn)點(diǎn)擊按鈕就彈出dialog對(duì)話框的相關(guān)資料,需要的朋友可以參考下2022-11-11Vue infinite update loop的問(wèn)題解決
這篇文章主要介紹了Vue "...infinite update loop..."的問(wèn)題解決,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2019-04-04vue使用Luckysheet插件實(shí)現(xiàn)excel導(dǎo)入導(dǎo)出
本文主要介紹了vue使用Luckysheet插件實(shí)現(xiàn)excel導(dǎo)入導(dǎo)出,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2024-03-03