element-ui table表格双击行内编辑

行内双击编辑的本质:点击后input框出现,保存后span标签出现

<el-dialog
               class="dialog"
               :visible.sync="splitBillDialog2"
               :close-on-click-modal="false"
               :close-on-press-escape="false"
               :append-to-body="true"
               width="800px"
               height="400px"
               @close ="resetForm"
    >
        <template>
            <el-table
                    ref="splitBillTable"
                    :data="spliteBilltableData"
                    border
                    highlight-current-row
                    show-summary
                    :summary-method="getSummaries"
                    @current-change="handleCurrentChange"
                    @row-dblclick="rowDblclick"
                    :cell-style="splitBillTableStyle"
                    style="width: 100%"
                    height="400px"
                    :row-class-name="tableRowClassName"
            >
                <el-table-column :show-overflow-tooltip="true" property="billUtil" label="开票件数" width="120px">
                    <template slot-scope="scope">
                        <el-input v-if="isEditSplit[scope.$index]" size="small" v-model="scope.row.billUtil">
                            <span v-if="isEditSplit[scope.$index]">{{scope.row.billUtil}}</span>
                        </el-input>
                        <span v-if="!isEditSplit[scope.$index]">{{scope.row.billUtil}}</span>
                    </template>
                </el-table-column>
<!--                <el-table-column label="操作" min-width="50px" class-name="tablebtns" fixed="right">-->
<!--                    <template slot-scope="scope">-->
<!--                        <el-button v-if="!isEditSplit[scope.$index]" @click="editSplit(scope.$index)" type="text">编辑</el-button>-->
<!--                        <el-button v-if="isEditSplit[scope.$index]" @click="saveSplit(scope.$index)" type="text">保存</el-button>-->
<!--                    </template>-->
<!--                </el-table-column>-->
            </el-table>
        </template>
        <div slot="footer" class="dialog-footer">
            <el-row style="margin: 0;">
                <el-col :span="24" align="center" class="table-panel">
                    <div v-if="isDoubleClick" style="display: inline">
                        <el-button v-if="isEditSplitBtn &&isDoubleClick" @click="saveSplit()" type="primary">保存</el-button>
                        <el-button v-if="!isEditSplitBtn &&isDoubleClick" @click="cancelSplit()" type="primary">撤销</el-button>
                    </div>
                    <el-button @click="splitBillDialog2 = false">取 消</el-button>
                    <el-button class="confirmBtn" type="primary" @click="preSubmitStockBill()">确 定</el-button>
                </el-col>
            </el-row>
        </div>
    </el-dialog>

data:{
	spliteBilltableData:[
         {mainCompany:'xxx',
             prodName:'xxx',
             prodCode:'xxx',
             billUtil:'xxx',
             billQat:'xxx',
             billAmount:'xxx',
             discountAmount:'xxx',
             totalPrice:'xxx',
             prodUnit:'xxx',
             netPrice:'xxx'
         }
     ],
     currentRow: null,
     currentRow2: null,
     beforeCheckRow2: null,
     isCurrentRow2EditFlag:false,
     isEditSplit:[false],
     index:'',
     index2:'',
     isEditSplitBtn:false,
     isDoubleClick:false,
}
handleCurrentChange(val){
   // console.log("val:",val)
    this.currentRow = val;
},
tableRowClassName({row, rowIndex}){
   row.index = rowIndex;
},
//双击编辑
rowDblclick(row, column){
    this.isEditSplitBtn=true
    this.isDoubleClick=true
    //如果编辑行和选中行不是同一行,则代表编辑了下一行,保存之前编辑的数据
    if (row.index !== this.index) {
        this.$set(this.isEditSplit, this.index, false);
    }
    this.index = row.index;
    this.$set(this.isEditSplit, this.index, true);
},
//保存按钮
saveSplit(){
    this.$set(this.isEditSplit, this.index, false);
    this.isDoubleClick=false
},
//关闭dialog框后重置
resetForm(){
    this.index2 = '';
    this.index = '';
    this.isEditSplit = [false];
    this.isCurrentRow2EditFlag = false;
    this.beforeCheckRow2 = null;
},

如果有编辑某一行的某列其他列随之变化的需求,则需要将上一步选中行存下来,不然双击后选中行就变了,随之修改的值也会产生错误

rowDblclick2(row, column){
    this.isEditSplitBtn=true
    this.isDoubleClick=true
    if (row.index !== this.index2) {   
        if (this.isCurrentRow2EditFlag){     //是否编辑过      
            var price = xxxx;//要改变的值           
            this.$set(this.beforeCheckRow2,"billAmount",price)
        }
        this.beforeCheckRow2=this.currentRow2
        this.isCurrentRow2EditFlag=true
        this.$set(this.isEditSplit, this.index2, false);
    }
    this.index2 = row.index;
    this.$set(this.isEditSplit, this.index2, true);
},

此外,因为用到了tableRowClassName({row, rowIndex}){ row.index = rowIndex;},,所以导致表格的每一行元素多了一个index,有些项目的后端是以类的形式接收,所以会报错,这时需要在发起请求前去掉index

rows.forEach((item)=>{
   for (let key in item) {
        if (key !== 'index') {
            newItems[key] = item[key];
        }
    }
    totalRows.push(deepCopy(newItems))
})

:cell-style="splitBillTableStyle"用法:表格元素样式修改

 splitBillTableStyle({ row, column, rowIndex, columnIndex }){
	if (column.property === "xxx") {  //要改样式的列名,这里是改变某列的颜色
	    return 'background-color:#50E3A5;border-bottom:1px solid white'
	}
},
  • 2
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
element-ui是一款非常常用的前端组件库,其中的table组件对于数据展示和操作非常方便。在使用table组件时,有时需要对表格中的某些字段进行编辑。同时,有些字段可能是预定义的枚举值,此时需要使用下拉框来展示可选的选项。下面介绍如何在element-ui table中实现这个功能。 首先,在table组件的columns中定义需要编辑的列。例如,我们将某一列的prop属性定义为status,并在scopedSlots中定义一个edit组件来展示可选的枚举值。代码如下: ``` <template> <el-table :data="tableData" :columns="columns"> <template v-slot:edit="{ row, column, $index }"> <el-select v-model="row.status" placeholder="请选择" @change="handleChange(row)" filterable> <el-option v-for="(item, index) in options" :key="index" :label="item.label" :value="item.value"> </el-option> </el-select> </template> </el-table> </template> <script> export default { data() { return { tableData: [{ name: '用户1', status: '1', }, { name: '用户2', status: '2', }], columns: [{ prop: 'name', label: '姓名', }, { prop: 'status', label: '状态', scopedSlots: { customRender: 'edit' }, }], options: [{ label: '待审核', value: '1', }, { label: '已通过', value: '2', }, { label: '已拒绝', value: '3', }], }; }, methods: { handleChange(row) { console.log(row); }, }, }; </script> ``` 在上面的代码中,我们定义了一个options数组来存储枚举值的选项。在每个行上的下拉框中,我们将对应的状态值绑定到了row.status上,并监听了下拉框值的变化,以便在handleChange方法中处理状态值的提交等操作。 使用element-uitable组件实现带编辑功能的下拉框不仅简单而且十分方便,可以大大加快前端开发的速度。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值