技術共有

要素 el-table は、検証ルールを使用してテーブル行を動的に追加/削除/編集する機能を実装します。

2024-07-12

한어Русский языкEnglishFrançaisIndonesianSanskrit日本語DeutschPortuguêsΕλληνικάespañolItalianoSuomalainenLatina

この記事では、追加、削除、変更のために編集可能なデータ列を el-table に追加する方法を記録します。

1. 空行を追加する

ページのマウント時にフォームのテーブル リストにデータの行を直接追加し、push() メソッドを直接使用してデータの列を追加することもできます。この時点でいくつかのデフォルト値を設定することもできます。たとえば、次のような場合 製品の個数。

  1. mounted() {
  2. this.$nextTick(() => {
  3. this.addFormData.productList.push({
  4. productName: '',//产品名称
  5. price: '',//单价(元/㎡)
  6. productCount: '1', //产品件数
  7. totalAmount: '', //小计¥元
  8. })
  9. })
  10. },

2. データを取得する製品名を選択し、リスト行に表示します

現在のケースでは製品名がドロップダウン リストから選択されているため、データを取得してドロップダウン リストにレンダリングするインターフェイスを要求する必要があります。ここでは偽のデータが直接使用されます。

  1. data() {
  2. return {
  3. addFormData: {
  4. // 产品列表
  5. productList: [],
  6. },
  7. addFormRules: {
  8. productName: [{
  9. required: true,
  10. message: '请选择产品',
  11. trigger: 'blur'
  12. }],
  13. price: [{
  14. required: true,
  15. message: '请输入单价',
  16. trigger: 'blur'
  17. }
  18. ],
  19. productCount: [{
  20. required: true,
  21. message: '请输入产品件数',
  22. trigger: 'blur'
  23. }]
  24. },
  25. optionsList: [
  26. {
  27. id:1,
  28. productName:'橘子',
  29. price:'10',
  30. },
  31. {
  32. id:2,
  33. productName:'苹果',
  34. price:'8',
  35. }
  36. ]
  37. }
  38. },
  39. <el-form ref="addFormRef" :model="addFormData" :rules="addFormRules" size="mini" :inline="true">
  40. <el-table tooltip-effect="light" :data="addFormData.productList" >
  41. <el-table-column label="产品名称" prop="productName" min-width="150">
  42. <template slot-scope="scope">
  43. <el-form-item size="mini" :prop="'productList.' + scope.$index + '.productName'"
  44. :rules="addFormRules.productName" class="all">
  45. <el-select v-model="scope.row.productName" filterable value-key="id" placeholder="请选择"
  46. @change="pestChange($event, scope.$index)">
  47. <el-option v-for="item in optionsList" :key="item.id" :label="item.productName"
  48. :value="item">
  49. </el-option>
  50. </el-select>
  51. </el-form-item>
  52. </template>
  53. </el-table-column>
  54. <el-table-column label="操作" align="center" class-name="small-padding fixed-width" fixed="right"
  55. width="150">
  56. <template slot-scope="scope">
  57. <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdateYes(scope.row)"
  58. v-hasPermi="['system:order:edit']">增加</el-button>
  59. <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDeleteProduct(scope.row)"
  60. v-hasPermi="['system:order:remove']">删除</el-button>
  61. </template>
  62. </el-table-column>
  63. </el-table>
  64. <el-form-item size="large">
  65. <el-button type="primary" @click="handleSubmitAdd">提交</el-button>
  66. <el-button @click="handleCancelAdd">取消</el-button>
  67. </el-form-item>
  68. </el-form>
  69. pestChange(e, index) {
  70. //此时的e 就是optionsList中的某一项
  71. //让后解构赋值给我们这一行对应的值
  72. let data = this.addFormData.productList[index]
  73. Object.keys(data).forEach(key => {
  74. data[key] = e[key]
  75. })
  76. this.addFormData.productList[index].productCount = 1
  77. },

3.小計合格(計算された属性の計算値)

  1. <el-form ref="addFormRef" :model="addFormData" :rules="addFormRules" size="mini" :inline="true">
  2. <el-table tooltip-effect="light" :data="addFormData.productList" >
  3. <el-table-column label="小计¥元" prop="totalAmount" width="100">
  4. <template slot-scope="scope">
  5. <div class="notext">
  6. {{ getTotalAmount(scope.row) }}
  7. </div>
  8. </template>
  9. </el-table-column>
  10. <el-table-column label="操作" align="center" class-name="small-padding fixed-width" fixed="right"
  11. width="150">
  12. <template slot-scope="scope">
  13. <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdateYes(scope.row)"
  14. v-hasPermi="['system:order:edit']">增加</el-button>
  15. <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDeleteProduct(scope.row)"
  16. v-hasPermi="['system:order:remove']">删除</el-button>
  17. </template>
  18. </el-table-column>
  19. </el-table>
  20. <el-form-item size="large">
  21. <el-button type="primary" @click="handleSubmitAdd">提交</el-button>
  22. <el-button @click="handleCancelAdd">取消</el-button>
  23. </el-form-item>
  24. </el-form>
  1. computed: {
  2. getTotalAmount(){
  3. return (data) => {
  4. //先判断单价和数量必须存在
  5. if (data.productCount && data.price) {
  6. data.totalAmount = parseInt(data.productCount) * parseInt(parseFloat(data.price))
  7. return data.totalAmount
  8. } else {
  9. return 0.00
  10. }
  11. }
  12. }
  13. },

4. 別の行を追加して、前の行のデータを再利用します。

  1. handleUpdateYes(row) {
  2. //拿到上一行数据再往数组中push()新的数据
  3. this.addFormData.productList.push({
  4. productName: row.productName,//产品名称
  5. price: row.price,//单价(元/㎡)
  6. productCount: row.productCount, //产品件数
  7. totalAmount: '', //小计¥元
  8. })
  9. },

5. 特定の行を削除する

  1. // 删除产品
  2. handleDeleteProduct(row) {
  3. this.$confirm('此操作将永久删除该产品信息, 是否继续?', '提示', {
  4. confirmButtonText: '确定',
  5. cancelButtonText: '取消',
  6. type: 'warning'
  7. }).then(() => {
  8. this.$message({
  9. type: 'success',
  10. message: '删除成功!'
  11. });
  12. this.addFormData.productList.splice(row.index, 1)
  13. }).catch(() => {
  14. this.$message({
  15. type: 'info',
  16. message: '已取消删除'
  17. });
  18. });
  19. },

6. [送信] をクリックしてフォームを確認します。

  1. // 添加订单表单提交
  2. handleSubmitAdd() {
  3. this.$refs.addFormRef.validate(async (valid) => {
  4. if (!valid) return
  5. //校验通过 往下执行
  6. })
  7. },