vue iview表格单元格可编辑input

方法:

/**
 * 表格内Input组件
 * @param {function} h // render 内部h函数
 * @param {object} params  表格参数
 * @param {string/array} prop // params.row[...prop]
 * @param {function} obj, 可选参数
 */

// TODO:修改为组件
export const inTableInputItem = (
  h,
  params,
  prop,
  {
    changeCall,
    blurCall,
    type,
    numberObj,
    disabled,
    styles,
    badSty,
    className,
    prepend
  }
) => {
  let value = params.row
  let val = value[prop]
  if (typeof prop === "object") {
    try {
      prop.forEach(key => {
        value = value[key]
      })
    } catch (error) {}

    val = value || 0
  }

  if (disabled) {
    return <span>{val}</span>
  } else {
    let _val = val
    if(prepend){
      _val =  Math.abs(val)
    }
    if(!blurCall){
      blurCall = ()=>{};
    }
    return (
      <Input
        value={ _val || ""}
        class={className}
        v-inputnumber
        style="top:0"
        onOn-change={changeCall.bind(this, params, prop)}
        onOn-blur={blurCall.bind(this, params)}
      >
      {prepend?<Button slot="prepend"  on-click={prepend.call.bind(this, params, prop, val)} icon={val>=0?"md-add":"md-remove"}></Button>:''}
      </Input>
      
    )
  }

  // if (type === "number") {
  //   if (!numberObj) numberObj = { max: 100000, min: 0, step: 1 }
  //   return (
  //     //const {max,max,} = nember

  //     <InputNumber
  //       max={numberObj.max}
  //       min={numberObj.max}
  //       step={numberObj.step}
  //       value={val || 0}
  //       onOn-change={changeCall.bind(this, params, prop)}
  //       onOn-blur={blurCall.bind(this, params)}
  //     />
  //   )
  // } else {
   
  // }

  // return (
  //   <Input
  //     value={val || ""}
  //     onOn-change={changeCall.bind(this, params, prop)}
  //     onOn-blur={blurCall.bind(this, params)}
  //   />
  // )
}
/**
 * 过滤数值
 */
export const filterNum = val => {
  if (isNaN(parseFloat(val))) {
    return 0
  } else {
    return parseFloat(val)
  }
}

调用方式:

  {
                    title: '标题',
                    key: 'tt',
                    width: 120,
                    render: (h, params) => {
                        return inTableInputItem(h,params,'tt',{changeCall:this.ttChange, blurCall: this.blurChange})  
                    }
                },

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
使用 Vue.js 和 Element UI 实现表格单元格编辑的步骤如下: 1. 导入 Element UI 的表格和表单组件: ```vue <template> <div> <el-table :data="tableData" style="width: 100%"> <!-- 表格列定义 --> </el-table> </div> </template> <script> import { ElTable, ElTableColumn, ElInput } from 'element-ui' export default { components: { ElTable, ElTableColumn, ElInput }, data() { return { tableData: [ { name: '张三', age: 20 }, { name: '李四', age: 25 }, { name: '王五', age: 30 } ], editIndex: -1 } } } </script> ``` 2. 在表格中的每一行定义一个编辑按钮,点击编辑按钮后将当前行进入编辑状态: ```vue <el-table :data="tableData" style="width: 100%"> <el-table-column label="姓名"> <template slot-scope="scope"> <template v-if="editIndex !== scope.$index"> {{ scope.row.name }} </template> <template v-else> <el-input v-model="scope.row.name"></el-input> </template> </template> </el-table-column> <el-table-column label="年龄"> <template slot-scope="scope"> <template v-if="editIndex !== scope.$index"> {{ scope.row.age }} </template> <template v-else> <el-input v-model="scope.row.age"></el-input> </template> </template> </el-table-column> <el-table-column label="操作"> <template slot-scope="scope"> <template v-if="editIndex !== scope.$index"> <el-button type="primary" size="small" @click="edit(scope.$index)">编辑</el-button> </template> <template v-else> <el-button type="success" size="small" @click="save(scope.$index)">保存</el-button> <el-button type="warning" size="small" @click="cancel(scope.$index)">取消</el-button> </template> </template> </el-table-column> </el-table> ``` 3. 定义编辑按钮的点击事件和保存、取消按钮的点击事件: ```vue export default { // 省略其他代码 methods: { edit(index) { this.editIndex = index }, save(index) { this.editIndex = -1 }, cancel(index) { this.editIndex = -1 } } } ``` 完整代码如下: ```vue <template> <div> <el-table :data="tableData" style="width: 100%"> <el-table-column label="姓名"> <template slot-scope="scope"> <template v-if="editIndex !== scope.$index"> {{ scope.row.name }} </template> <template v-else> <el-input v-model="scope.row.name"></el-input> </template> </template> </el-table-column> <el-table-column label="年龄"> <template slot-scope="scope"> <template v-if="editIndex !== scope.$index"> {{ scope.row.age }} </template> <template v-else> <el-input v-model="scope.row.age"></el-input> </template> </template> </el-table-column> <el-table-column label="操作"> <template slot-scope="scope"> <template v-if="editIndex !== scope.$index"> <el-button type="primary" size="small" @click="edit(scope.$index)">编辑</el-button> </template> <template v-else> <el-button type="success" size="small" @click="save(scope.$index)">保存</el-button> <el-button type="warning" size="small" @click="cancel(scope.$index)">取消</el-button> </template> </template> </el-table-column> </el-table> </div> </template> <script> import { ElTable, ElTableColumn, ElInput, ElButton } from 'element-ui' export default { components: { ElTable, ElTableColumn, ElInput, ElButton }, data() { return { tableData: [ { name: '张三', age: 20 }, { name: '李四', age: 25 }, { name: '王五', age: 30 } ], editIndex: -1 } }, methods: { edit(index) { this.editIndex = index }, save(index) { this.editIndex = -1 }, cancel(index) { this.editIndex = -1 } } } </script> ``` 这样就可以通过 Vue.js 和 Element UI 实现表格单元格编辑了。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值