当前位置: 首页 > 编程笔记 >

vue el-table实现行内编辑功能

卢知
2023-03-14
本文向大家介绍vue el-table实现行内编辑功能,包括了vue el-table实现行内编辑功能的使用技巧和注意事项,需要的朋友参考一下

最近做一个vue前后端分离的项目,前端框架用element ui,在 使用 el-table 的过程中,需要实现行内编辑,效果大概是这样:

分为下面几个步骤:

(1) 自定义 el-table 的表头(即添加 “新增” 按钮):

<el-table :data="propTableData.col.filter(data => handleAdd || data.name.toLowerCase().includes(handleAdd.toLowerCase()))"
   highlight-current-row
   border>
</el-table>
<template slot="header"
  slot-scope="scope">
  <el-button v-model="handleAdd"
    size="mini"
    type="success"
    round
    plain
    @click="handleAdd(scope.$index, scope.row)">{{ $t('common.add') }}</el-button>
</template>

表头自定义了一个“添加”按钮,点击 el-table 动态添加一行。

(2) el-table 动态添加一行:

let row = {
  code: '',
  maxValue: '',
  minValue: '',
  name: '',
  valueType: 'String',
  id: '',
  typeId: '',
  warning: false,
  isSet: true
}
this.propTableData.col.push(row)

vue 数据变化驱动 dom 进行更新,给 table 绑定的数据 propTableData.col 添加一个元素,则表格会添加一行。

propTableData.sel 保存当前行数据

this.propTableData.sel = row

(3) el-table 动态删除一行:

子组件中触发父组件的 delete 事件:

this.$emit('delete', row.id)

(4)当前行状态判断,即是否处于编辑状态,控制表格每一行的按钮元素的移除与插入:

<template slot-scope="scope">
   <el-button size="mini"
      type="primary"
      round
      plain
      v-if="!scope.row.isSet"
      @click="valChange(scope.row,scope.$index,true)">{{ $t('common.edit') }}</el-button>
   <el-button size="mini"
      type="primary"
      round
      plain
      v-else
      @click="valChange(scope.row,scope.$index,true)">{{ $t('common.save') }}</el-button>
   <el-button size="mini"
      type="danger"
      round
      plain
      v-if="!scope.row.isSet"
      @click="handleDelete(scope.$index, scope.row)">{{ $t('common.delete') }}</el-button>
   <el-button size="mini"
      type="danger"
      round
      plain
      v-else
      @click="valChange(scope.row,scope.$index,false)">{{ $t('common.cancel') }}</el-button>
</template>

上面代码中,通过 v-if=“scope.row.isSet” 来判断当前行是否处于编辑状态。

如果当前行处于编辑状态,则按钮为“保存”和“取消”,此时可对当前行进行保存和取消操作,且不能新增,除非先保存当前行;

如果当前行处于非编辑状态,则按钮为“编辑”和“删除”状态,此时可对当前行进行编辑和删除操作。

这样,就可以实现 el-table 行内编辑的需求。

完整版代码如下:

<template>
 <el-dialog class="uiot-dialog"
    width="900px"
    :visible.sync="isShow"
    :before-close="beforeClose"
    :close-on-click-modal="false"
    :title="$t("basicData.device.propDlg.dlgTitle')"
    @open="open">
 <el-table :data="propTableData.col.filter(data => handleAdd || data.name.toLowerCase().includes(handleAdd.toLowerCase()))"
    highlight-current-row
    border>
  <el-table-column label="No."
      type="index"
      align="center"
      width="50"></el-table-column>
  <el-table-column :label="$t('basicData.device.propDlg.code')">
  <template slot-scope="scope">
   <span v-if="scope.row.isSet">
   <el-input size="mini"
      v-model="scope.row.code"></el-input>
   </span>
   <span v-else>{{ scope.row.code }}</span>
  </template>
  </el-table-column>
  <el-table-column :label="$t('basicData.device.propDlg.name')">
  <template slot-scope="scope">
   <span v-if="scope.row.isSet">
   <el-input size="mini"
      v-model="scope.row.name"></el-input>
   </span>
   <span v-else>{{ scope.row.name }}</span>
  </template>
  </el-table-column>
  <el-table-column :label="$t('basicData.device.propDlg.minValue')">
  <template slot-scope="scope">
   <span v-if="scope.row.isSet">
   <el-input size="mini"
      v-model="scope.row.minValue"></el-input>
   </span>
   <span v-else>{{ scope.row.minValue }}</span>
  </template>
  </el-table-column>
  <el-table-column :label="$t('basicData.device.propDlg.maxValue')">
  <template slot-scope="scope">
   <span v-if="scope.row.isSet">
   <el-input size="mini"
      v-model="scope.row.maxValue"></el-input>
   </span>
   <span v-else>{{ scope.row.maxValue }}</span>
  </template>
  </el-table-column>
  <el-table-column :label="$t('basicData.device.propDlg.valueType')">
  <template slot-scope="scope">
   <span v-if="scope.row.isSet">
   <el-select size="mini"
      v-model="scope.row.valueType"
      :placeholder="$t('common.pleSelect')">
    <el-option v-for="item in valTypeOptions"
       :key="item.value"
       :label="item.label"
       :value="item.value"></el-option>
   </el-select>
   </span>
   <span v-else>{{ scope.row.valueType }}</span>
  </template>
  </el-table-column>
  <el-table-column :label="$t('basicData.device.propDlg.warning')">
  <template slot-scope="scope">
   <span v-if="scope.row.isSet">
   <el-select v-model="scope.row.warning"
      size="mini">
    <el-option v-for="item in warnOptions"
       :key="item.value"
       :label="item.label"
       :value="item.value"></el-option>
   </el-select>
   </span>
   <span v-else>{{ scope.row.warning===true?'是':'否' }}</span>
  </template>
  </el-table-column>
  <el-table-column align="center"
      width="170px">
  <template slot="header"
     slot-scope="scope">
   <el-button v-model="handleAdd"
      size="mini"
      type="success"
      round
      plain
      @click="handleAdd(scope.$index, scope.row)">{{ $t('common.add') }}</el-button>
  </template>
  <template slot-scope="scope">
   <el-button size="mini"
      type="primary"
      round
      plain
      v-if="!scope.row.isSet"
      @click="valChange(scope.row,scope.$index,true)">{{ $t('common.edit') }}</el-button>
   <el-button size="mini"
      type="primary"
      round
      plain
      v-else
      @click="valChange(scope.row,scope.$index,true)">{{ $t('common.save') }}</el-button>
   <el-button size="mini"
      type="danger"
      round
      plain
      v-if="!scope.row.isSet"
      @click="handleDelete(scope.$index, scope.row)">{{ $t('common.delete') }}</el-button>
   <el-button size="mini"
      type="danger"
      round
      plain
      v-else
      @click="valChange(scope.row,scope.$index,false)">{{ $t('common.cancel') }}</el-button>
  </template>
  </el-table-column>
 </el-table>
 </el-dialog>
</template>

<script>
import { open } from 'fs'
import '@/styles/uiot.scss'

const defaultProp = {
 code: '',
 maxValue: '',
 minValue: '',
 name: '',
 valueType: 'String',
 id: '',
 typeId: '',
 warning: false
}

export default {
 props: {
 isShow: Boolean,
 oneRow: {
  type: Array,
  default: function() {
  return defaultProp
  }
 }
 },
 data() {
 return {
  propTableData: {
  sel: null, // 选中行
  col: []
  },
  warnOptions: [
  {
   value: true,
   label: '是'
  },
  {
   value: false,
   label: '否'
  }
  ],
  valTypeOptions: [
  {
   value: 'String',
   label: 'String'
  },
  {
   value: 'Int',
   label: 'Int'
  },
  {
   value: 'Double',
   label: 'Double'
  },
  {
   value: 'Boolean',
   label: 'Boolean'
  },
  {
   value: 'Date',
   label: 'Date'
  }
  ]
 }
 },
 created() {},
 methods: {
 open() {
  this.propTableData.col = this.oneRow
  this.propTableData.col.map(i => {
  i.isSet = false
  })
  console.log('open', this.propTableData.col)
 },
 // 添加
 handleAdd() {
  for (let i of this.propTableData.col) {
  if (i.isSet) {
   return this.Message(
   this.$t('basicData.device.propDlg.pleSave'),
   'warning'
   )
  }
  }
  let row = {
  code: '',
  maxValue: '',
  minValue: '',
  name: '',
  valueType: 'String',
  id: '',
  typeId: '',
  warning: false,
  isSet: true
  }
  this.propTableData.col.push(row)
  this.propTableData.sel = row
 },
 //修改
 valChange(row, index, qx) {
  console.log('edit', this.propTableData)
  //点击修改,判断是否已经保存所有操作
  for (let i of this.propTableData.col) {
  console.log('i.isSet', i.isSet, i.id, row.id)
  if (i.isSet && i.id != row.id) {
   this.Message(this.$t('basicData.device.propDlg.pleSave'), 'warning')
   return false
  }
  }
  //是否是取消操作
  if (!qx) {
  console.log('qx', this.propTableData.sel.id)
  if (!this.propTableData.sel.id) {
   this.propTableData.col.splice(index, 1)
  }
  return (row.isSet = !row.isSet)
  }
  //提交数据
  if (row.isSet) {
  console.log('this.propTableData.sel', this.propTableData.sel)
  const v = this.propTableData.sel
  // 必填项判断
  if (v.code == '' || v.name == '') { 
   this.Message(this.$t('common.pleEnter'), 'warning')
  } else {
   this.$emit('confirm', this.propTableData.sel)
   row.isSet = false
  }
  } else {
  this.propTableData.sel = row
  row.isSet = true
  }
 },
 // 删除
 handleDelete(index, row) {
  this.$emit('delete', row.id)
 },
 beforeClose() {
  this.$emit('cancel')
 },
 Message(msg, type) {
  this.$message({ type: type ? type : 'info', message: msg })
 }
 }
}
</script>

<style lang="scss">
</style>

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持小牛知识库。

 类似资料:
  • 问题内容: 我对Java脚本很陌生,因此如果这很基本,我必须道歉。 如何使用Angularjs在Smart-Table中编辑行表?新的Smart- Table似乎没有教程。我想为用户创建一个简单的表格,以输入特定地点的开放时间。 我创建了可以在表上添加和删除行的按钮,但是当我添加contenteditable =“ true”时,更新对象时所有更改都不会保留。我知道contenteditable是

  • 本文向大家介绍jQuery Easyui datagrid行内实现【添加】、【编辑】、【上移】、【下移】,包括了jQuery Easyui datagrid行内实现【添加】、【编辑】、【上移】、【下移】的使用技巧和注意事项,需要的朋友参考一下 前几天项目中遇到一个需求用到了Easyui datagrd行内添加和编辑数据,同时对行内数据上移下移,所以对这几个功能做个总结。 1、首先大概说下这几个功能

  • 本文向大家介绍jQuery实现拖拽可编辑模块功能代码,包括了jQuery实现拖拽可编辑模块功能代码的使用技巧和注意事项,需要的朋友参考一下 在没给大家分享实现代码之前,先给大家展示下效果图: 具体实现代码如下所示: index.html inettuts.js inettuts.css inettuts.js.css

  • 本文向大家介绍jquery实现的table排序功能示例,包括了jquery实现的table排序功能示例的使用技巧和注意事项,需要的朋友参考一下 本文实例讲述了jquery实现的table排序功能。分享给大家供大家参考,具体如下: 运行效果图如下: 更多关于jQuery相关内容感兴趣的读者可查看本站专题:《jQuery表格(table)操作技巧汇总》、《jQuery切换特效与技巧总结》、《jQuer

  • 导语 前面已经在主窗口中添加了菜单和工具栏,这一篇中我们将实现基本的文本编辑功能。在开始正式写程序之前,我们先要考虑一下整个流程。因为这里要写一个记事本一样的程序,所以最好先打开Windows中的记事本,进行一些简单的操作,然后考虑怎样去实现这些功能。再者,再强大的软件,它的功能也是一个一个加上去的,不要设想一下子写出所有的功能。我们这里先实现新建文件,保存文件,和文件另存为三个功能,是因为它们联

  • 问题内容: 使用什么是能够编辑内容的最佳方法? 在我理想的情况下, 添加的 生日将是一个超链接,点击该链接将显示一个编辑表单-与带有更新按钮的当前添加表单相同。 实时预览(插播) HTML: App.js: 问题答案: 您应该将表单放在每个节点内,分别使用和启用和禁用编辑。像这样: 这里的关键点是: 我已将控件更改为本地范围 已添加到,因此我们可以在编辑时显示它 添加了带有的,以便在编辑时隐藏内容