vue + element 实现可编辑列表(超详细)

vue + element 实现可编辑列表(超详细)

1.适用场景

​ 当我们对于主子表数据同时保存的时候,我们又不想做弹窗一条一条的新增子表数据的时候,并且子表数据并不是特别复杂,完全可用table来操作,这种情况下可选择可编辑列表来完成主子表的同时操作。

2.功能简介

​ 基于Tlement-Table编写,并借助于一些表单元素控件。实现表格数据的填写。以及表格操作,包括:新增行,删除行,插入行,上移,下移,置顶,置底等功能。废话不多说直接上代码。

3.页面布局

​ 包含对每一行数据的简单校验。

<template>
  <el-row>
    <el-card>
      <el-row class="text-right m-b">
        <el-button type="primary" @click="handleAdd()">
          添加行
        </el-button>
        <el-button type="danger" plain @click="batchRemove">
          删除行
        </el-button>
        <el-button type="primary" @click="handleInsert">
          插入行
        </el-button>
        <el-button type="primary" @click="handleMove(1)">
          上移
        </el-button>
        <el-button type="primary" @click="handleMove(0)">
          下移
        </el-button>
        <el-button type="primary" @click="handleTopOrBottom(1)">
          置顶
        </el-button>
        <el-button type="primary" @click="handleTopOrBottom(0)">
          置底
        </el-button>
        <el-button type="primary" @click="clearList">
          清空
        </el-button>
      </el-row>
      <el-form ref="form2" :model="submitData" :rules="rules" label-width="150px">
        <el-row :span="24">
          <el-table
            ref="studentTable"
            :data="submitData.studentTableData"
            style="width: 100%"
            highlight-current-row
            stripe
            border
            :row-class-name="tableRowClassName"
            @current-change="currentChange"
            @selection-change="clickLogCheckboxHandler"
          >
            <el-table-column
              type="index"
              label="序号"
              width="80"
            />
            <el-table-column
              prop="studentCode"
              label="学号"
            >
              <template slot-scope="scope">
                <el-form-item :prop="'studentTableData.'+scope.$index+'.studentCode'" :rules="rules.studentTableDataRules.studentCode" label-width="0">
                  <el-input v-if="scope.row.showFormDom" v-model="scope.row.studentCode"/>
                  <div v-else>
                    {{ scope.row.studentCode }}
                  </div>
                </el-form-item>
              </template>
            </el-table-column>
            <el-table-column
              prop="studentName"
              label="学生姓名"
            >
              <template slot-scope="scope">
                <el-form-item :prop="'studentTableData.'+scope.$index+'.studentName'" :rules="rules.studentTableDataRules.studentName" label-width="0">
                  <el-input v-if="scope.row.showFormDom" v-model="scope.row.studentName"/>
                  <div v-else>
                    {{ scope.row.studentName }}
                  </div>
                </el-form-item>
              </template>
            </el-table-column>
            <el-table-column
              prop="studentAge"
              label="学生年龄"
            >
              <template slot-scope="scope">
                <el-form-item :prop="'studentTableData.'+scope.$index+'.studentAge'" :rules="rules.studentTableDataRules.studentAge" label-width="0">
                  <el-input v-if="scope.row.showFormDom" v-model="scope.row.studentAge"/>
                  <div v-else>
                    {{ scope.row.studentAge }}
                  </div>
                </el-form-item>
              </template>
            </el-table-column>
            <el-table-column
              prop="studentSex"
              label="学生性别"
            >
              <template slot-scope="scope">
                <el-form-item :prop="'studentTableData.'+scope.$index+'.studentSex'" :rules="rules.studentTableDataRules.studentSex" label-width="0">
                  <el-input v-if="scope.row.showFormDom" v-model="scope.row.studentSex"/>
                  <div v-else>
                    {{ scope.row.studentSex }}
                  </div>
                </el-form-item>
              </template>
            </el-table-column>
          </el-table>
        </el-row>
        <el-row class="text-center m-t">
          <el-button type="primary" @click="submitHandler">
            保存
          </el-button>
        </el-row>
      </el-form>
    </el-card>
  </el-row>
</template>

4.数据声明

data() {
    return {
	  // 选中的行
      selected: null,
      rowIndex: 0,
      // 提交数据
      submitData: {
        studentTableData: []
      },
	  // 校验信息
      rules: {
        studentTableDataRules: {
          studentName: [
            { required: true, message: '请输入学生姓名', trigger: 'blur' }
          ],
          studentCode: [
            { required: true, message: '请输入学号', trigger: 'blur' }
          ],
          studentAge: [
            { required: true, message: '请输入学生年龄', trigger: 'blur' }
          ],
          studentSex: [
            { required: true, message: '请输入学生性别', trigger: 'blur' }
          ]
        }
      },
	  // 多选情况下多选数据存储
      selecteds: []
    }
  }

5.功能实现

5.1 获取选中的行

注:多选情况。

clickLogCheckboxHandler(selection) {
      this.selecteds = selection
    }

5.2 为row添加索引

注:由于row中获取不到索引值,无法操作数组中的数据,因此增加此方法为row增加索引值。

tableRowClassName({ row, rowIndex }) {
      row.row_index = rowIndex
    }

5.3 新增行

注:此方法为列表底部新增一行,新增前会执行校验,并且新增行获得高亮;高亮方法为element-ui提供。

handleAdd() {
      const list = {
        studentCode: '',
        studentName: '',
        studentAge: '',
        studentSex: '',
        showFormDom: true
      }
      this.$refs['form2'].validate((valid) => {
        if (valid) {
          if (this.selected !== null) {
            const index = this.selected.row_index
            this.$set(this.submitData.studentTableData[index], 'showFormDom', false)
          }
          this.submitData.studentTableData.push(list)
          this.selected = list
          this.selected.row_index = this.submitData.studentTableData.length - 1
          this.$refs.studentTable.setCurrentRow(this.submitData.studentTableData[this.selected.row_index])
        } else {
          // 此处为验证失败代码
          this.$message.warning('请先为当前行填值!')
        }
      })
    }

5.4 删除行

注:删除当前选中行

batchRemove() {
      this.$confirm('确认删除选中记录吗?', '提示', {
        type: 'warning'
      })
        .then(() => {
          if (this.selected !== null) {
            this.submitData.studentTableData.splice(this.selected.row_index, 1)
            this.selected = null
            this.$message({
              message: '删除成功',
              type: 'success'
            })
          } else {
            this.$message({
              message: '请选择要删除的行!',
              type: 'warning'
            })
          }
        })
        .catch(() => {})
    }

5.5 插入行

注:在选中数据上方插入一个空行

handleInsert() {
      if (this.selected !== null) {
        const list = {
          studentCode: '',
          studentName: '',
          studentAge: '',
          studentSex: '',
          showFormDom: true
        }
        if (this.submitData.studentTableData.length > 0) {
          this.$refs['form2'].validate((valid) => {
            if (valid) {
              const index = this.selected.row_index
              this.$set(this.submitData.studentTableData[index], 'showFormDom', false)
              this.submitData.studentTableData.splice(index, 0, list)
              this.selected = list
              this.selected.row_index = index
              this.$refs.studentTable.setCurrentRow(this.submitData.studentTableData[index])
            } else {
              // 此处为验证失败代码
              this.$message.warning('请先为当前行填值!')
            }
          })
        } else {
          this.submitData.studentTableData.push(list)
          this.selected = list
          this.selected.row_index = 0
          this.$refs.studentTable.setCurrentRow(this.submitData.studentTableData[0])
        }
      } else {
        this.$message.warning('请选择插入位置!')
      }
    }

5.6 上移,下移

注:根据传入标识判断上移,下移(1:上移;0:下移)

handleMove(dir) {
      if (this.selected !== null) {
        const moveComm = (curIndex, nextIndex) => {
          const arr = this.submitData.studentTableData
          arr[curIndex] = arr.splice(nextIndex, 1, arr[curIndex])[0]
          return arr
        }
        this.submitData.studentTableData.some((val, index) => {
          if (val.studentCode === this.selected.studentCode) {
            if (dir === 1 && index === 0) {
              this.$message.warning('已在顶部!')
            } else if (dir === 0 && index === this.submitData.studentTableData.length - 1) {
              this.$message.warning('已在底部!')
            } else {
              const nextIndex = dir === 1 ? index - 1 : index + 1
              this.submitData.studentTableData = moveComm(index, nextIndex)
            }
            return true
          }
          return false
        })
      } else {
        this.$message.warning('请选择操作的行!')
      }
    }

5.7 置顶/置底

注:根据传入标识判断置顶/置底(1:置顶;0:置底)

handleTopOrBottom(dir) {
      if (this.selected !== null) {
        const moveComm = (curIndex, nextIndex) => {
          const arr = this.submitData.studentTableData
          arr[curIndex] = arr.splice(nextIndex, 1, arr[curIndex])[0]
          return arr
        }
        this.submitData.studentTableData.some((val, index) => {
          if (val.studentCode === this.selected.studentCode) {
            if (dir === 1 && index === 0) {
              this.$message.warning('已在顶部!')
            } else if (dir === 0 && index === this.submitData.studentTableData.length - 1) {
              this.$message.warning('已在底部!')
            } else {
              const nextIndex = dir === 1 ? 0 : this.submitData.studentTableData.length - 1
              this.submitData.studentTableData = moveComm(index, nextIndex)
            }
            return true
          }
          return false
        })
      } else {
        this.$message.warning('请选择操作的行!')
      }
    }

5.8 清空列表

注:vue基于数据驱动,清空列表只需要清空列表数组数据即可。

clearList() {
      this.$confirm('确认执行清空操作吗?', '提示', {
        type: 'warning'
      })
        .then(() => {
          this.submitData.studentTableData = []
          this.selected = null
          this.$message({
            message: '清空完成!',
            type: 'success'
          })
        })
    }

5.9 改变列表选中

注:由于这次编写的可编辑列表有这样一个特性,即:选中的行才可以编辑,未选中的行则是只读状态。因此在改变选中时应该增加一些操作。例如:对row.showFormDom的操作。还是直接看代码:

currentChange(newRow, oldRow) {
      if (this.selected !== null) {
        if (newRow.row_index !== this.selected.row_index) {
          this.$refs['form2'].validate((valid) => {
            if (valid) {
              const index = this.selected.row_index
              this.$set(this.submitData.studentTableData[newRow.row_index], 'showFormDom', true)
              this.$set(this.submitData.studentTableData[index], 'showFormDom', false)
              this.selected = newRow
            } else {
              // 此处为验证失败代码
              this.$refs.studentTable.setCurrentRow(this.submitData.studentTableData[this.selected.row_index])
              // this.$message.warning('请先为当前行填值!')
            }
          })
        }
      } else {
        this.$set(this.submitData.studentTableData[newRow.row_index], 'showFormDom', true)
        this.selected = newRow
      }
    }

5.10 最终操作:保存

注:这个应该无须赘述。

submitHandler() {
      this.$refs['form2'].validate((valid) => {
        if (valid) {
          this.$message({
            showClose: true,
            message: '请完善列表中的数据!',
            type: 'warning'
          })
        } else {
          this.$message({
            showClose: true,
            message: '请完善列表中的数据!',
            type: 'warning'
          })
        }
      })
    }

6 总结

​ 可编辑列表常用的基本功能大致就想到了这么多,在就是本demo中表单元素均用input实现,对于其他需求,可以替换为其他表单元素控件,例如:select等。也可针对于字段值的改变触发一些特定的方法来实现特定的需求。再就是上移、下移、置顶、置底均用到了studentCode这个属性,那么这个属性应该保持其唯一性,这里没有为其增加唯一性校验,实际开发中应该注意。再就是习惯js的=赋值,在这里很多采用了$set去赋值,有时间我会单独聊一下这样赋值的原因。

​ 最后希望大家开源自己,共同进步。

  • 4
    点赞
  • 24
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
是的,Vue.js可以实现编辑的Excel格。你可以使用一些库来帮助你实现这个功能,例如`handsontable`或`xlsx-style`。这些库可以让你在Vue.js中创建和编辑Excel格,并提供了一些丰富的功能,例如单元格合并、公式计算、样式设置等。 你可以通过在Vue组件中引入这些库,并使用它们的API来创建一个可编辑的Excel格。例如,你可以使用`handsontable`库创建一个基于格的界面,然后通过Vue的数据绑定将数据与格进行关联,实现数据的动态更新和编辑。另外,你还可以使用`xlsx-style`库来读取和写入Excel文件,以实现文件的导入和导出功能。 以下是一个简单示例,演示如何在Vue.js中使用`handsontable`库创建可编辑的Excel格: ```vue <template> <div> <hot-table :settings="hotSettings"></hot-table> </div> </template> <script> import 'handsontable/dist/handsontable.full.css'; import Handsontable from 'handsontable'; export default { data() { return { hotSettings: { data: [['John', 'Doe', 30], ['Jane', 'Smith', 25]], colHeaders: true, rowHeaders: true, contextMenu: true, licenseKey: 'non-commercial-and-evaluation' } }; }, mounted() { const container = this.$el.querySelector('div'); new Handsontable(container, this.hotSettings); } }; </script> ``` 在这个示例中,我们通过`hot-table`组件来创建一个格,并使用`hotSettings`对象来配置格的属性。`hotSettings`对象中的`data`属性用于指定格的数据,`colHeaders`和`rowHeaders`属性用于显示列头和行头,`contextMenu`属性用于启用右键菜单,`licenseKey`属性用于设置许可证密钥。 需要注意的是,这只是一个简单的示例,你可以根据具体需求进行更复杂的配置和功能扩展。同时,你还可以结合其他库或自定义指令来实现更高级的功能,例如数据校验、图展示等。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值