点击单元格自动聚焦可编辑,且失去焦点即修改成功的实现方法

//页面使用
   <InputNumber
      v-model="scope.row.subWeightage"
      :type="1"
      @change="(value) => changeTime(scope.$index)"
   />
//点击编辑日期框,失去焦点保存日期,InputNumber组件
<template>
  <div style="width: 100%; height: 100%">
    <div
      v-if="!isEdit"
      style="width: 100%; height: 100%"
      @click.stop="clickHandle"
    >
      <span>{{ value | formatValue }}</span>
    </div>
    <fks-date-picker
      v-else
      ref="inputRef"
      v-model="innerValue"
      type="date"
      placeholder="选择日期"
      format="yyyy-MM-dd"
      value-format="yyyy-MM-dd"
      style="width: 130px;"
      :clearable="false"
      @blur="handleChange"
    />
  </div>
</template>

<script>
export default {
  name: 'DateInput',
  filters: {
    formatValue(value){
      if(!value || value == 'null') return '/';
      return value;
    }
  },
  model: {
    prop: 'value',
    event: 'change',
  },
  props: {
    value: {
      type: [Number, String],
    },
  },
  data() {
    return {
      isEdit: false,
      innerValue: this.value,
    };
  },

  methods: {
    clickHandle() {
      this.isEdit = !this.isEdit;
      if (this.isEdit) {
        this.$nextTick(() => {
          this.$refs.inputRef.focus();
        });
      }
    },
    handleChange(e) {
      const value = e.value;
      this.clickHandle();
      this.$emit('change', value);
    },
  },
};
</script>

<style>
</style>


//页面使用
<DateInput
    v-model="scope.row.completionTime"
    @change="(value) => changeTime(scope.$index)"
 />
//点击编辑数字输入框,失去焦点保存
<template>
  <div style="width: 100%; height: 100%" class="rowContainer">
    <div
      class="rowContainer"
      v-if="!isEdit"
      style="width: 100%; height: 100%"
      @click.stop="clickHandle"
    >
      <span>{{ formatValue(value) }}</span>
    </div>
    <fks-input-number
      v-else
      ref="inputRef"
      v-model="innerValue"
      controls-position="right"
      :min="0"
      style="width: 100px; height: 100%"
      @blur="handleChange"
    />
  </div>
</template>

<script>
export default {
  name: 'InputNumber',
  model: {
    prop: 'value',
    event: 'change',
  },
  props: {
    value: {
      type: [Number, String],
    },
    type: {
      type: [Number, String],
    },
  },
  data() {
    return {
      isEdit: false,
      innerValue: this.value,
    };
  },

  methods: {
    formatValue(value) {
      const unit = this.type == 1 ? '%' : '';
      if(!value || value == 'null') return 0 + unit;
      if(isNaN(Number(value))) return (0 + unit);
      return value + unit;
    },
    clickHandle() {
      this.isEdit = !this.isEdit;
      if (this.isEdit) {
        this.$nextTick(() => {
          this.$refs.inputRef.focus();
        });
      }
    },
    handleChange(e) {
      const value = Number(e.target.value);
      this.clickHandle();
      this.$emit('change', value);
    },
  },
};
</script>

<style scoped lang="scss">
.rowContainer{
  display: flex;
  align-items: center;
  justify-content: center;
}
::v-deep {
  .fks-input-number {
    display: flex;
    align-items: center;
    justify-content: center;
    .is-controls-right .fks-input__inner {
      padding-right: 0px;
      padding-left: 0px;
    }
    .fks-input-number__decrease {
      display: none;
    }
    .fks-input-number__increase {
      display: none;
    }
  }
  .fks-input-number--medium .fks-input__inner {
    padding-right: 0px;
    padding-left: 0px;
  }
}
</style>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值