ElementUI表格行編輯單元格編輯支援(輸入框,選擇框)Demo

itread01 2020-11-13 14:03:20
elementUI demo 表格 支援


嗯,需要做成這個樣子,所以網上查了些資料。整理了下。提供幾個一個思路。不足之處請小夥伴指出來。

  •  普通版的table可編輯內嵌select選擇框,輸出框,編輯刪除新增等
    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="UTF-8">
    <!-- import CSS -->
    <link rel="stylesheet" href="https://unpkg.com/element-ui/lib/theme-chalk/index.css">
    <!-- import Vue before Element -->
    <script src="https://unpkg.com/vue/dist/vue.js"></script>
    <!-- import JavaScript -->
    <script src="https://unpkg.com/element-ui/lib/index.js"></script>
    </head>
    <body style="display: flex;justify-content: center;width: 100%;">
    <div id="app">
    <el-card>
    <el-table :data="tableData" style="width: 100%" :row-class-name="tableRowClassName">
    <el-table-column prop="date" label="日期" width="180">
    <template slot-scope="scope">
    <el-select popper-class="role-option" v-model="scope.row.date" placeholder="請選擇" size="small">
    <el-option v-for="item in optionData" :key="item.value" :label="item.name"
    :value="item.value"></el-option>
    </el-select>
    </template>
    </el-table-column>
    <el-table-column prop="name" label="姓名" width="180">
    <template slot-scope="scope">
    <el-select popper-class="role-option" v-model="scope.row.name" placeholder="請選擇" size="small">
    <el-option v-for="item in optionName" :key="item.value" :label="item.name"
    :value="item.value"></el-option>
    </el-select>
    </template>
    </el-table-column>
    <el-table-column prop="addr" width="180" label="地址">
    <template slot-scope="scope">
    <el-select popper-class="role-option" v-model="scope.row.addr" placeholder="請選擇" size="small">
    <el-option v-for="item in optionAddr" :key="item.value" :label="item.name"
    :value="item.value"></el-option>
    </el-select>
    </template>
    </el-table-column>
    <el-table-column label="操作" width="100" header-align="right">
    <template slot-scope="scope">
    <div style="display: flex;justify-content: space-between;">
    <el-link icon="el-icon-plus" size="mini" type="primary"
    @click="handleAdd(scope.$index, scope.row)" :underline="false"
    style="font-size: 14px;margin-left: 40px"></el-link>
    <el-link icon="el-icon-delete" size="mini" @click="handleDelete(scope.$index, scope.row)"
    type="danger" :underline="false"></el-link>
    </div>
    </template>
    </el-table-column>
    </el-table>
    </el-card>
    </div>
    </body>
    <script>
    new Vue({
    el: '#app',
    data() {
    return {
    optionData: [{ value: "1", name: "2016-05-03" }, { value: "2", name: "2016-05-04" }, { value: "3", name: "2016-05-05" }],
    optionName: [{ value: "1", name: " 王小虎" }, { value: "2", name: "王小龍" }, { value: "3", name: "王小狗" }],
    optionAddr: [{ value: "1", name: "北涼" }, { value: "2", name: "西北道" }, { value: "3", name: "泰安城" }],
    tableData: [{
    date: '2016-05-02',
    name: '王小虎',
    addr: '北涼',
    }, {
    date: '2016-05-04',
    name: '王小狗',
    addr: '西北道'
    },]
    }
    },
    mounted() {
    },
    methods: {
    handleAdd(index, row) {
    this.tableData.push({
    date: '',
    name: '',
    addr: ''
    });
    },
    handleDelete(index, row) {
    if (index > 0) {
    this.tableData.splice(index, 1);
    }
    },
    tableRowClassName({ row, rowIndex }) {
    if ((rowIndex+1)%2 === 1) {
    return 'warning-row';
    } else{
    return 'success-row';
    }
    return '';
    }
    },
    props: {},
    computed: {},
    created() {
    },
    watch: {},
    })
    </script>
    <style>
    .el-table .warning-row {
    background: oldlace;
    }
    .el-table .success-row {
    background: #f0f9eb;
    }
    </style>
    </html>
  • 表格隱藏展示,資料聯動,所謂資料聯動,意思是通過第一列的選擇控制之後兩列的資料來源展示不同的資料項。這裡需要注意的如果選擇框的資料來源是動態通過介面獲取的,那麼需要把資料來源由變數設定為函式,同時,介面的請求必須是同步的,不可以是非同步,axios預設是非同步的,如果沒有合適的解決方案,可以使用jquery 的ajax方法。
    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="UTF-8">
    <!-- import CSS -->
    <link rel="stylesheet" href="https://unpkg.com/element-ui/lib/theme-chalk/index.css">
    <!-- import Vue before Element -->
    <script src="https://unpkg.com/vue/dist/vue.js"></script>
    <!-- import JavaScript -->
    <script src="https://unpkg.com/element-ui/lib/index.js"></script>
    </head>
    <body style="display: flex;justify-content: center;width: 100%;">
    <div id="app">
    <el-card>
    <el-table :data="tableData" style="width: 100%" :row-class-name="tableRowClassName">
    <el-table-column prop="date" label="日期" width="180">
    <template slot-scope="scope">
    <el-select popper-class="role-option" v-model="scope.row.date" placeholder="請選擇" size="small"
    @change="dateHandleChange">
    <el-option v-for="item in optionData" :key="item.value" :label="item.name"
    :value="item.value"></el-option>
    </el-select>
    </template>
    </el-table-column>
    <el-table-column prop="name" label="姓名" width="180">
    <template slot-scope="scope">
    <el-select popper-class="role-option" v-model="scope.row.name" placeholder="請選擇" size="small" v-show="scope.row.date != '2'" >
    <el-option v-for="item in optionName" :key="item.value" :label="item.name"
    :value="item.value"></el-option>
    </el-select>
    </template>
    </el-table-column>
    <el-table-column prop="addr" width="180" label="地址">
    <template slot-scope="scope">
    <el-select popper-class="role-option" v-model="scope.row.addr" placeholder="請選擇" size="small">
    <el-option v-for="item in optionsAddr(scope.row)" :key="item.value" :label="item.name"
    :value="item.value"></el-option>
    </el-select>
    </template>
    </el-table-column>
    <el-table-column label="操作" width="100" header-align="right">
    <template slot-scope="scope">
    <div style="display: flex;justify-content: space-between;">
    <el-link icon="el-icon-plus" size="mini" type="primary"
    @click="handleAdd(scope.$index, scope.row)" :underline="false"
    style="font-size: 14px;margin-left: 40px"></el-link>
    <el-link icon="el-icon-delete" size="mini" @click="handleDelete(scope.$index, scope.row)"
    type="danger" :underline="false"></el-link>
    </div>
    </template>
    </el-table-column>
    </el-table>
    </el-card>
    </div>
    </body>
    <script>
    /**
    * @description: 表格隱藏展示,資料聯動
    * @author: Liruilong
    */
    new Vue({
    el: '#app',
    data() {
    return {
    optionData: [{ value: "1", name: "2016-05-03" }, { value: "2", name: "2016-05-04" }, { value: "3", name: "2016-05-05" }],
    optionName: [{ value: "1", name: " 王小虎" }, { value: "2", name: "王小龍" }, { value: "3", name: "王小狗" }],
    tableData: [{
    date: '1',
    name: '1',
    addr: '1',
    }],
    optionAddrMapper: new Map(),
    }
    },
    mounted() {
    // 模擬ajax初始化資料集
    this.optionAddrMapper.set("1", [{ value: "1", name: "北涼" }, { value: "2", name: "西北道" }, { value: "3", name: "泰安城" }]);
    },
    methods: {
    optionsAddr(row) {
    console.log(row.date)
    console.log(this.optionAddrMapper.get(row.date));
    return this.optionAddrMapper.get(row.date);
    },
    //聯動處理//模擬ajax呼叫介面獲取資料.日期選擇會聯動地址
    dateHandleChange(val) {
    // 模擬根據日期得到地址的資料集 這裡寫ajax ,必須是同步的請求。
    this.optionAddrMapper.set("1", [{ value: "1", name: "北涼" }, { value: "2", name: "西北道" }, { value: "3", name: "泰安城" }]);
    this.optionAddrMapper.set("2", [{ value: "9", name: "西域" }, { value: "8", name: "苗疆" }, { value: "7", name: "劍氣長城" }]);
    this.optionAddrMapper.set("3", [{ value: "10", name: "聖賢林" }, { value: "11", name: "青冥天下" }, { value: "12", name: "藕花福地" }]);
    },
    handleAdd(index, row) {
    this.tableData.push({
    date: '',
    name: '',
    addr: ''
    });
    },
    handleDelete(index, row) {
    if (index > 0) {
    this.tableData.splice(index, 1);
    }
    },
    tableRowClassName({ row, rowIndex }) {
    if ((rowIndex + 1) % 2 === 1) {
    return 'warning-row';
    } else {
    return 'success-row';
    }
    return '';
    }
    },
    props: {},
    computed: {},
    created() {
    },
    watch: {},
    })
    </script>
    <style>
    .el-table .warning-row {
    background: oldlace;
    }
    .el-table .success-row {
    background: #f0f9eb;
    }
    </style>
    </html>
  • 表格整體與編輯檢視的切換,雙擊表格,或進行檢視切換。可以編輯也可以展示
    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="UTF-8">
    <!-- import CSS -->
    <link rel="stylesheet" href="https://unpkg.com/element-ui/lib/theme-chalk/index.css">
    <!-- import Vue before Element -->
    <script src="https://unpkg.com/vue/dist/vue.js"></script>
    <!-- import JavaScript -->
    <script src="https://unpkg.com/element-ui/lib/index.js"></script>
    <script src="https://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script>
    </head>
    <body style="display: flex;justify-content: center;width: 100%;">
    <div id="app">
    <el-card>
    <el-table :data="tableData" style="width: 100%" :row-class-name="tableRowClassName"
    @cell-dblclick="editdbClick">
    <el-table-column prop="date" label="日期" width="180">
    <template slot-scope="scope">
    <el-select v-show="!isVlable" popper-class="role-option" v-model="scope.row.date"
    placeholder="請選擇" size="small" @change="dateHandleChange">
    <el-option v-for="item in optionData" :key="item.name" :label="item.name"
    :value="item.value"></el-option>
    </el-select>
    <span style="line-height: 32px;" v-show="isVlable">{{ optionDataf(scope.row.date)}} </span>
    </template>
    </el-table-column>
    <el-table-column prop="name" label="姓名" width="180">
    <template slot-scope="scope">
    <el-select v-show="!isVlable && scope.row.date != '2' " popper-class="role-option"
    v-model="scope.row.name" placeholder="請選擇" size="small">
    <el-option v-for="item in optionName" :key="item.name" :label="item.name"
    :value="item.value"></el-option>
    </el-select>
    <span style="line-height: 32px;" v-show="isVlable">{{optionNamef(scope.row.name) }} </span>
    </template>
    </el-table-column>
    <el-table-column prop="addr" width="180" label="地址">
    <template slot-scope="scope">
    <el-select v-show="!isVlable" popper-class="role-option" v-model="scope.row.addr"
    placeholder="請選擇" size="small">
    <el-option v-for="item in optionsAddr(scope.row)" :key="item.name" :label="item.name"
    :value="item.value"></el-option>
    </el-select>
    <span style="line-height: 32px;" v-show="isVlable">{{optionsAddrf(scope.row.addr) }}
    </span>
    </template>
    </el-table-column>
    <el-table-column label="操作" width="100" header-align="right">
    <template slot-scope="scope">
    <div style="display: flex;justify-content: space-between;">
    <el-link icon="el-icon-plus" size="mini" type="primary"
    @click="handleAdd(scope.$index, scope.row)" :underline="false"
    style="font-size: 14px;margin-left: 40px"></el-link>
    <el-link icon="el-icon-delete" size="mini" @click="handleDelete(scope.$index, scope.row)"
    type="danger" :underline="false"></el-link>
    </div>
    </template>
    </el-table-column>
    </el-table>
    </el-card>
    </div>
    </body>
    <script>
    /**
    * @description: 表格與編輯檢視的切換,雙擊表格,或進行檢視切換。可以編輯也可以展示。
    * @author: Liruilong
    */
    new Vue({
    el: '#app',
    data() {
    return {
    isVlable: false,
    optionData: [{ value: "1", name: "2016-05-03" }, { value: "2", name: "2016-05-04" }, { value: "3", name: "2016-05-05" }],
    optionName: [{ value: "1", name: " 王小虎" }, { value: "2", name: "王小龍" }, { value: "3", name: "王小狗" }],
    optionAddr:[{ value: "1", name: "北涼" }, { value: "2", name: "西北道" }, { value: "3", name: "泰安城" }],
    tableData: [{
    date: '1',
    name: '1',
    addr: '1',
    }],
    optionAddrMapper: new Map(),
    date: '',
    name: '',
    address: '',
    }
    },
    created() {
    },
    mounted() {
    // 模擬ajax初始化資料集
    this.optionAddrMapper.set("1", [{ value: "1", name: "北涼" }, { value: "2", name: "西北道" }, { value: "3", name: "泰安城" }]);
    },
    methods: {
    optionDataf(id) {
    let name = id;
    this.optionData.forEach((element) => {
    if (element.value == id) {
    name = element.name;
    }
    });
    return name;
    },
    optionNamef(id) {
    let name = id;
    this.optionName.forEach((element) => {
    if (element.value == id) {
    name = element.name;
    }
    });
    return name;
    },
    optionsAddrf(id) {
    let name = id;
    if(this.optionAddrMapper){
    this.optionAddrMapper.forEach((value, key) => {
    value.forEach((e) => {
    if (e.value == id) {
    name = e.name;
    }
    })
    });
    }else{
    this.optionAddr.forEach( e =>{
    if (e.value == id) {
    name = e.name;
    }
    });
    }
    return name;
    },
    editdbClick(row, column, cell, event) {
    this.isVlable = !this.isVlable;
    },
    saveClick(row, column, cell, event) {
    },
    optionsAddr(row) {
    return this.optionAddrMapper.get(row.date);
    },
    //聯動處理//模擬ajax呼叫介面獲取資料.日期選擇會聯動地址
    dateHandleChange(val) {
    // 模擬根據日期得到地址的資料集 這裡寫ajax ,必須是同步的請求。
    this.optionAddrMapper.set("1", [{ value: "1", name: "北涼" }, { value: "2", name: "西北道" }, { value: "3", name: "泰安城" }]);
    this.optionAddrMapper.set("2", [{ value: "9", name: "西域" }, { value: "8", name: "苗疆" }, { value: "7", name: "劍氣長城" }]);
    this.optionAddrMapper.set("3", [{ value: "10", name: "聖賢林" }, { value: "11", name: "青冥天下" }, { value: "12", name: "藕花福地" }]);
    },
    // 新增操作。預設會儲存當前行的資料,並新增一條新資料
    handleAdd(index, row) {
    if ((row.date && row.name && row.addr) || (row.date && row.addr)) {
    this.tableData.push({
    date: '',
    name: '',
    addr: ''
    });
    }
    },
    handleDelete(index, row) {
    debugger
    if (index > 0) {
    this.tableData.splice(index, 1);
    }
    },
    tableRowClassName({ row, rowIndex }) {
    row.index = rowIndex;
    if ((rowIndex + 1) % 2 === 1) {
    return 'warning-row';
    } else {
    return 'success-row';
    }
    return '';
    }
    },
    props: {},
    computed: {},
    created() {
    },
    watch: {},
    filters: {
    },b
    })
    </script>
    <style>
    .el-table .warning-row {
    background: oldlace;
    }
    .el-table .success-row {
    background: #f0f9eb;
    }
    </style>
    </html>
  • 行編輯檢視手動切換,通過雙擊表格實現編輯的表格的切換。

     

    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="UTF-8">
    <!-- import CSS -->
    <link rel="stylesheet" href="https://unpkg.com/element-ui/lib/theme-chalk/index.css">
    <!-- import Vue before Element -->
    <script src="https://unpkg.com/vue/dist/vue.js"></script>
    <!-- import JavaScript -->
    <script src="https://unpkg.com/element-ui/lib/index.js"></script>
    <script src="https://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script>
    </head>
    <body style="display: flex;justify-content: center;width: 100%;">
    <div id="app">
    <el-card>
    <el-table :data="tableData" style="width: 100%" :row-class-name="tableRowClassName"
    @cell-dblclick="editdbClick">
    <el-table-column prop="date" label="日期" width="180">
    <template slot-scope="scope">
    <el-select v-show="!showEdit[scope.$index]" popper-class="role-option" v-model="scope.row.date"
    placeholder="請選擇" size="small" @change="dateHandleChange">
    <el-option v-for="item in optionData" :key="item.name" :label="item.name"
    :value="item.value"></el-option>
    </el-select>
    <span style="line-height: 32px;"
    v-show="showEdit[scope.$index]">{{ optionDataf(scope.row.date)}} </span>
    </template>
    </el-table-column>
    <el-table-column prop="name" label="姓名" width="180">
    <template slot-scope="scope">
    <el-select v-show="!showEdit[scope.$index] && scope.row.date != '2' " popper-class="role-option"
    v-model="scope.row.name" placeholder="請選擇" size="small">
    <el-option v-for="item in optionName" :key="item.name" :label="item.name"
    :value="item.value"></el-option>
    </el-select>
    <span style="line-height: 32px;"
    v-show="showEdit[scope.$index]">{{optionNamef(scope.row.name) }} </span>
    </template>
    </el-table-column>
    <el-table-column prop="addr" width="180" label="地址">
    <template slot-scope="scope">
    <el-select v-show="!showEdit[scope.$index]" popper-class="role-option" v-model="scope.row.addr"
    placeholder="請選擇" size="small">
    <el-option v-for="item in optionsAddr(scope.row)" :key="item.name" :label="item.name"
    :value="item.value"></el-option>
    </el-select>
    <span style="line-height: 32px;"
    v-show="showEdit[scope.$index]">{{optionsAddrf(scope.row.addr) }}
    </span>
    </template>
    </el-table-column>
    <el-table-column label="操作" width="100" header-align="right">
    <template slot-scope="scope">
    <div style="display: flex;justify-content: space-between;">
    <el-link icon="el-icon-plus" size="mini" type="primary"
    @click="handleAdd(scope.$index, scope.row)" :underline="false"
    style="font-size: 14px;margin-left: 40px"></el-link>
    <el-link icon="el-icon-delete" size="mini" @click="handleDelete(scope.$index, scope.row)"
    type="danger" :underline="false"></el-link>
    </div>
    </template>
    </el-table-column>
    </el-table>
    </el-card>
    </div>
    </body>
    <script>
    /**
    * @description: 雙擊行檢視手動切換,通過雙擊表格實現編輯的表格的切換
    * @author: Liruilong
    */
    new Vue({
    el: '#app',
    data() {
    return {
    optionData: [{ value: "1", name: "2016-05-03" }, { value: "2", name: "2016-05-04" }, { value: "3", name: "2016-05-05" }],
    optionName: [{ value: "1", name: " 王小虎" }, { value: "2", name: "王小龍" }, { value: "3", name: "王小狗" }],
    optionAddr: [{ value: "1", name: "北涼" }, { value: "2", name: "西北道" }, { value: "3", name: "泰安城" }],
    tableData: [{
    date: '1',
    name: '1',
    addr: '1',
    }],
    optionAddrMapper: new Map(),
    date: '',
    name: '',
    address: '',
    showEdit: [false],
    }
    },
    created() {
    },
    mounted() {
    // 模擬ajax初始化資料集
    this.optionAddrMapper.set("1", [{ value: "1", name: "北涼" }, { value: "2", name: "西北道" }, { value: "3", name: "泰安城" }]);
    },
    methods: {
    optionDataf(id) {
    let name = id;
    this.optionData.forEach((element) => {
    if (element.value == id) {
    name = element.name;
    }
    });
    return name;
    },
    optionNamef(id) {
    let name = id;
    this.optionName.forEach((element) => {
    if (element.value == id) {
    name = element.name;
    }
    });
    return name;
    },
    optionsAddrf(id) {
    let name = id;
    if (this.optionAddrMapper) {
    this.optionAddrMapper.forEach((value, key) => {
    value.forEach((e) => {
    if (e.value == id) {
    name = e.name;
    }
    })
    });
    } else {
    this.optionAddr.forEach(e => {
    if (e.value == id) {
    name = e.name;
    }
    });
    }
    return name;
    },
    editdbClick(row, column, cell, event) {
    if ((row.date && row.name && row.addr) || (row.date && row.addr)) {
    this.$set(this.showEdit, row.index, !this.showEdit[row.index])
    }
    },
    // openShow(index){
    // this.showEdit.forEach( (e) =>{
    // if(e != index){
    // this.$set(this.showEdit, e, false)
    // }
    // })
    // },
    saveClick(row, column, cell, event) {
    },
    optionsAddr(row) {
    return this.optionAddrMapper.get(row.date);
    },
    //聯動處理//模擬ajax呼叫介面獲取資料.日期選擇會聯動地址
    dateHandleChange(val) {
    // 模擬根據日期得到地址的資料集 這裡寫ajax ,必須是同步的請求。
    this.optionAddrMapper.set("1", [{ value: "1", name: "北涼" }, { value: "2", name: "西北道" }, { value: "3", name: "泰安城" }]);
    this.optionAddrMapper.set("2", [{ value: "9", name: "西域" }, { value: "8", name: "苗疆" }, { value: "7", name: "劍氣長城" }]);
    this.optionAddrMapper.set("3", [{ value: "10", name: "聖賢林" }, { value: "11", name: "青冥天下" }, { value: "12", name: "藕花福地" }]);
    },
    // 新增操作。預設會儲存當前行的資料,並新增一條新資料
    handleAdd(index, row) {
    let data = this.tableData[this.tableData.length - 1];
    if (((row.date && row.name && row.addr) || (row.date && row.addr)) && ((data.date && data.name && data.addr) || (data.date && data.addr))) {
    this.$set(this.showEdit, row.index, true)
    this.tableData.push({
    date: '',
    name: '',
    addr: ''
    });
    this.$set(this.showEdit, row.index + 1, false)
    }
    },
    handleDelete(index, row) {
    debugger
    if (index > 0) {
    this.tableData.splice(index, 1);
    }
    },
    tableRowClassName({ row, rowIndex }) {
    row.index = rowIndex;
    if ((rowIndex + 1) % 2 === 1) {
    return 'warning-row';
    } else {
    return 'success-row';
    }
    return '';
    }
    },
    props: {},
    computed: {},
    created() {
    },
    watch: {},
    filters: {
    },
    })
    </script>
    <style>
    .el-table .warning-row {
    background: oldlace;
    }
    .el-table .success-row {
    background: #f0f9eb;
    }
    </style>
    </html>
  • 單機檢視自動切換,即只留一條要編輯的操作。這個和之前的原理不一樣,通過樣式控制。
    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="UTF-8">
    <!-- import CSS -->
    <link rel="stylesheet" href="https://unpkg.com/element-ui/lib/theme-chalk/index.css">
    <!-- import Vue before Element -->
    <script src="https://unpkg.com/vue/dist/vue.js"></script>
    <!-- import JavaScript -->
    <script src="https://unpkg.com/element-ui/lib/index.js"></script>
    </head>
    <body style="display: flex;justify-content: center;width: 100%;margin-top: 20px;">
    <div id="app">
    <el-card>
    <!-- 關鍵程式碼 highlight-current-row -->
    <el-table :data="tableData" style="width: 100%" class="tb-edit" highlight-current-row @row-click="handleCurrentChange" :row-class-name="tableRowClassName" >
    <el-table-column prop="date" label="日期" width="180">
    <template slot-scope="scope">
    <el-input size="small" v-model="scope.row.date" placeholder="請輸入內容"
    @change="handleEdit(scope.$index, scope.row)"></el-input> <span>{{scope.row.date}}</span>
    </template>
    </el-table-column>
    <el-table-column prop="name" label="姓名" width="180">
    <template slot-scope="scope">
    <el-input size="small" v-model="scope.row.name" placeholder="請輸入內容"
    @change="handleEdit(scope.$index, scope.row)"></el-input> <span>{{scope.row.name}}</span>
    </template>
    </el-table-column>
    <el-table-column prop="addr" width="180" label="地址">
    <template slot-scope="scope">
    <el-input size="small" v-model="scope.row.addr" placeholder="請輸入內容"
    @change="handleEdit(scope.$index, scope.row)"></el-input> <span>{{scope.row.addr}}</span>
    </template>
    </el-table-column>
    <el-table-column label="操作" width="100" header-align="right">
    <template slot-scope="scope">
    <div style="display: flex;justify-content: space-between;">
    <el-link icon="el-icon-plus" size="mini" type="primary"
    @click="handleAdd(scope.$index, scope.row)" :underline="false"
    style="font-size: 14px;margin-left: 40px"></el-link>
    <el-link icon="el-icon-delete" size="mini" @click="handleDelete(scope.$index, scope.row)"
    type="danger" :underline="false"></el-link>
    </div>
    </template>
    </el-table-column>
    </el-table>
    </el-card>
    </div>
    </body>
    <script>
    /**
    * @description: 單機檢視自動切換,即只留一條要編輯的操作。這個和之前的原理不一樣,通過樣式控制。
    * @author: Liruilong
    */
    new Vue({
    el: '#app',
    data() {
    return {
    tableData: [{
    date: '2016-05-02',
    name: '王小虎',
    addr: '北涼',
    }, {
    date: '2016-05-04',
    name: '王小狗',
    addr: '西北道'
    }, {
    date: '2016-05-04',
    name: '王小狗',
    addr: '西北道'
    }, {
    date: '2016-05-04',
    name: '王小狗',
    addr: '西北道'
    }]
    }
    },
    mounted() {
    },
    methods: {
    handleCurrentChange(row, event, column) {
    console.log(row, event, column, event.currentTarget)
    console.log("根據 Element Table @row-click=handleCurrentChange 事件,當該行點選的時候會動態新增一個 .current-row 的class屬性,然後在 css 中進行 display 控制就行了。")
    },
    handleAdd(index, row) {
    this.tableData.push({
    date: '',
    name: '',
    addr: ''
    });
    },
    handleDelete(index, row) {
    if (index > 0) {
    this.tableData.splice(index, 1);
    }
    },
    tableRowClassName({ row, rowIndex }) {
    row.index = rowIndex;
    if ((rowIndex + 1) % 2 === 1) {
    return 'warning-row';
    } else {
    return 'success-row';
    }
    return '';
    }
    },
    props: {},
    computed: {},
    created() {
    },
    watch: {},
    })
    </script>
    <style>
    .el-table .warning-row {
    background: oldlace;
    }
    .el-table .success-row {
    background: #f0f9eb;
    }
    .tb-edit .el-input {
    display: none
    }
    .tb-edit .current-row .el-input {
    display: block
    }
    .tb-edit .current-row .el-input+span {
    display: none
    }
    </style>
    </html>

     

  •  

版权声明
本文为[itread01]所创,转载请带上原文链接,感谢
https://www.itread01.com/content/1605247203.html

  1. [front end -- JavaScript] knowledge point (IV) -- memory leakage in the project (I)
  2. This mechanism in JS
  3. Vue 3.0 source code learning 1 --- rendering process of components
  4. Learning the realization of canvas and simple drawing
  5. gin里获取http请求过来的参数
  6. vue3的新特性
  7. Get the parameters from HTTP request in gin
  8. New features of vue3
  9. vue-cli 引入腾讯地图(最新 api,rocketmq原理面试
  10. Vue 学习笔记(3,免费Java高级工程师学习资源
  11. Vue 学习笔记(2,Java编程视频教程
  12. Vue cli introduces Tencent maps (the latest API, rocketmq)
  13. Vue learning notes (3, free Java senior engineer learning resources)
  14. Vue learning notes (2, Java programming video tutorial)
  15. 【Vue】—props属性
  16. 【Vue】—创建组件
  17. [Vue] - props attribute
  18. [Vue] - create component
  19. 浅谈vue响应式原理及发布订阅模式和观察者模式
  20. On Vue responsive principle, publish subscribe mode and observer mode
  21. 浅谈vue响应式原理及发布订阅模式和观察者模式
  22. On Vue responsive principle, publish subscribe mode and observer mode
  23. Xiaobai can understand it. It only takes 4 steps to solve the problem of Vue keep alive cache component
  24. Publish, subscribe and observer of design patterns
  25. Summary of common content added in ES6 + (II)
  26. No.8 Vue element admin learning (III) vuex learning and login method analysis
  27. Write a mini webpack project construction tool
  28. Shopping cart (front-end static page preparation)
  29. Introduction to the fluent platform
  30. Webpack5 cache
  31. The difference between drop-down box select option and datalist
  32. CSS review (III)
  33. Node.js学习笔记【七】
  34. Node.js learning notes [VII]
  35. Vue Router根据后台数据加载不同的组件(思考-&gt;实现-&gt;不止于实现)
  36. Vue router loads different components according to background data (thinking - & gt; Implementation - & gt; (more than implementation)
  37. 【JQuery框架,Java编程教程视频下载
  38. [jQuery framework, Java programming tutorial video download
  39. Vue Router根据后台数据加载不同的组件(思考-&gt;实现-&gt;不止于实现)
  40. Vue router loads different components according to background data (thinking - & gt; Implementation - & gt; (more than implementation)
  41. 【Vue,阿里P8大佬亲自教你
  42. 【Vue基础知识总结 5,字节跳动算法工程师面试经验
  43. [Vue, Ali P8 teaches you personally
  44. [Vue basic knowledge summary 5. Interview experience of byte beating Algorithm Engineer
  45. 【问题记录】- 谷歌浏览器 Html生成PDF
  46. [problem record] - PDF generated by Google browser HTML
  47. 【问题记录】- 谷歌浏览器 Html生成PDF
  48. [problem record] - PDF generated by Google browser HTML
  49. 【JavaScript】查漏补缺 —数组中reduce()方法
  50. [JavaScript] leak checking and defect filling - reduce() method in array
  51. 【重识 HTML (3),350道Java面试真题分享
  52. 【重识 HTML (2),Java并发编程必会的多线程你竟然还不会
  53. 【重识 HTML (1),二本Java小菜鸟4面字节跳动被秒成渣渣
  54. [re recognize HTML (3) and share 350 real Java interview questions
  55. [re recognize HTML (2). Multithreading is a must for Java Concurrent Programming. How dare you not
  56. [re recognize HTML (1), two Java rookies' 4-sided bytes beat and become slag in seconds
  57. 【重识 HTML ,nginx面试题阿里
  58. 【重识 HTML (4),ELK原来这么简单
  59. [re recognize HTML, nginx interview questions]
  60. [re recognize HTML (4). Elk is so simple