Vue+EleMentUI は、el-table-colum テーブル選択ドロップダウン ボックスが編集可能であることを認識します

説明します:

仕入れや入庫の過程で、テーブルの行をすばやく編集して保存する必要があり、時間を節約して作業効率を向上させます! 、ソースコードを編集するたびにポップアップする代わりに: https://gitee.com/charlinchenlin/store-pos

レンダリング:

ここに画像の説明を挿入

1.データにサプライヤー配列などの要素を定義する

data() {
    
    
      return {
    
           
        suppliers:[],   //保存供应商数据    
        showInput: "", //用来判断是否显示哪个单元格
        oldData: {
    
    }, //用来存修改前的数据
        currentData: {
    
    },  //用来保存新的数据     
      }
    },

2. el-table テーブルのクリックとダブルクリックの tableDbEdit イベントを追加します。

<el-table :data="dataList" size="mini" v-loading="dataListLoading" @selection-change="selectionChangeHandle"
      style="width: 100%;" @cell-click="tableDbEdit" @cell-dblclick="tableDbEdit" height="320"
      :header-cell-style="{ background: '#fcfcfc', color: '#606266', height:'36px'}">
      <el-table-column type="selection" header-align="center" align="center" width="50">
      </el-table-column>
     
        ------
      
    </el-table>

選択ドロップダウン ボックスを表示するかどうかを制御する

tableDbEdit(row, column, event) {
    
    
   this.showInput = column.property + row.inboundId;
   this.oldData[column.property] = row[column.property];
 },

3. サプライヤー列のドロップダウン ボックスを追加します。

showInput の値が現在の inboundId と同じ場合、ドロップダウン オプションが表示されます。それ以外の場合は、データ情報が表示されます。

<el-table-column prop="supplierName" header-align="center" align="center" label="供应商名称" width="150" show-overflow-tooltip>
        <template slot-scope="scope">
          <el-select size="mini" @focus="getSuppliers()" @click="getSuppliers()" @change='blurInput(scope.row, "supplierName", scope.row.supplierName)' v-model="scope.row.supplierName" clearable
            v-if="showInput == `supplierName${
      
      scope.row.inboundId}`"
            placeholder="请选择">
            <el-option v-for="item in suppliers" :key="item.supplierId" :label="item.supplierName" :value="item.supplierName">
            </el-option>
          </el-select>
          <span v-else class="active-input">{
    
    {
    
    scope.row.supplierName}}</span>
        </template>
</el-table-column>

フォーカスまたはクリックでサプライヤー データを取得する

async getSuppliers() {
    
    
        const res = await this.$http({
    
    
          url: `/product/supplier/getSupplies`,
          method: 'get',
          params: this.$http.adornParams()
        })
        let data = res.data
        if (data && data.code === 0) {
    
    
          this.suppliers = data.data
        }
      },

変更イベントがトリガーされたら、現在の列に値を割り当て、サプライヤー情報を設定します

// 当input失去光标后进行的操作
      async blurInput(row, name, value) {
    
    
        // 判断数据是否有所改变,如果数据有改变则调用修改接口
        if (this.oldData[name] != value) {
    
    
          row[name] = value         
        }
        this.showInput = ""
        this.currentData = row
        if(name === 'supplierName'){
    
    
          this.setSuppliers(row)
        }
      },

	setSuppliers(row) {
    
    
        for (let index in this.suppliers) {
    
    
          let item = this.suppliers[index]
          if (row.supplierName === item.supplierName) {
    
    
              row.supplierId = item.supplierId
              return
          }
        }
      },

4.現在の列を保存し、成功後にデータを再ロードします

async saveHandle(row) {
    
    
        console.log("saveHandle row===", row)
        row.status = row.status ? 1 : 0
        const res = await this.$http({
    
    
          url: `/purchase/purchasesinboundorder/update`,
          method: 'post',
          data: this.$http.adornData(row)
        });
        let data = res.data
        if (data && data.code !== 0) {
    
    
          row.status = !row.status;
          return this.$message.error('修改失败!');
        }
        this.$message.success('更新成功!');
        this.getDataList();
      },

5. v フォーカスを定義する

directives: {
    
    
      // 通过自定义指令实现的表单自动获得光标的操作
      focus: {
    
    
        inserted: function(el) {
    
    
          if (el.tagName.toLocaleLowerCase() == "input") {
    
    
            el.focus()
          } else {
    
    
            if (el.getElementsByTagName("input")) {
    
    
              el.getElementsByTagName("input")[0].focus()
            }
          }
          el.focus()
        }
      },
      focusTextarea: {
    
    
        inserted: function(el) {
    
    
          if (el.tagName.toLocaleLowerCase() == "textarea") {
    
    
            el.focus()
          } else {
    
    
            if (el.getElementsByTagName("textarea")) {
    
    
              el.getElementsByTagName("textarea")[0].focus()
            }
          }
          el.focus()
        }
      }
    },

おすすめ

転載: blog.csdn.net/lovoo/article/details/129533617