element-ui table 行内编辑

<p>EditRow.ts</p> <a href="http://www.jqhtml.com/19816.html" target="_blank">vue+element-ui+slot-scope原生实现可编辑表格</a>html

interface NoParamConstructor&lt;T&gt; {
  new(): T;
}

export default class EditRow&lt;T&gt; {
  origin: T = null
  copy: T = null
  is_edit: boolean = false
  ctor: NoParamConstructor&lt;T&gt;;

  constructor(ctor: NoParamConstructor&lt;T&gt;, origin: T) {
    this.ctor = ctor
    this.origin = origin
    this.show_edit = this.show_edit.bind(this)
    this.save = this.save.bind(this)
  }
  show_edit() {
    this.copy = Object.assign(new this.ctor(), this.origin)
    this.is_edit = true
  }
  save() {
    this.origin = this.copy
    this.is_edit = false
  }
}

<hr> <p>vue file</p>vue

&lt;template&gt;
  &lt;el-row&gt;
    &lt;el-table :data="rows" size="mini"&gt;
      &lt;el-table-column label="name"&gt;
        &lt;template slot-scope="scope"&gt;
          &lt;template v-if="scope.row.is_edit"&gt;
            &lt;input v-model="scope.row.copy.name" /&gt;
            &lt;el-button size="mini" @click="scope.row.save"&gt;save&lt;/el-button&gt;
            &lt;el-button size="mini" @click="scope.row.is_edit = false"&gt;cancel&lt;/el-button&gt;
          &lt;/template&gt;
          &lt;template v-else&gt;
            &lt;span @click="scope.row.show_edit"&gt;{{ scope.row.origin.name }}&lt;/span&gt;
          &lt;/template&gt;
        &lt;/template&gt;
      &lt;/el-table-column&gt;
    &lt;/el-table&gt;
  &lt;/el-row&gt;
&lt;/template&gt;

&lt;script&gt;
  import EditRow from './EditRow.ts'

  class entity {
    constructor() {
      this.name = '123'
    }
  }

  const rows = [
    new EditRow(entity, new entity())
  ]

  export default {
    data() {
      return {
        rows
      }
    }
  }
&lt;/script&gt;

原文地址:http://www.noobyard.com/article/p-posvpmzz-er.htmlelement-ui