shikeying
2023-04-07 c192f834c4e092bc7c0f2722c343c25c1be619ab
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
<template>
  <div class="edit-cell" @click="onFieldClick">
    <el-tooltip
      v-if="!editMode && !showInput"
      :placement="toolTipPlacement"
      :open-delay="toolTipDelay"
      :content="toolTipContent"
    >
      <div
        tabindex="0"
        class="cell-content"
        :class="{'edit-enabled-cell': canEdit}"
      >
        <!-- @keyup.enter="onFieldClick"-->
        <slot name="content" />
      </div>
 
    </el-tooltip>
    <!--@focus="onFieldClick"-->
    <el-input
      v-if="editMode || showInput"
      ref="input"
      v-model="model"
      placeholder="请输入内容"
      :class="error"
      @keyup.enter.native="onInputEnter"
      v-on="listeners"
    />
  </div>
</template>
 
<script>
import { multiply, abs } from '@/utils/tools'
import { validInteger } from '@/utils/validate'
export default {
  name: 'EditableCell',
  inheritAttrs: false,
  props: {
    value: {
      type: String,
      default: ''
    },
    toolTipContent: {
      type: String,
      default: '点击编辑'
    },
    toolTipDelay: {
      type: Number,
      default: 500
    },
    toolTipPlacement: {
      type: String,
      default: 'top-start'
    },
    showInput: {
      type: Boolean,
      default: false
    },
    editableComponent: {
      type: String,
      default: 'el-input'
    },
    closeEvent: {
      type: String,
      default: 'blur'
    },
    canEdit: {
      type: Boolean,
      default: false
    },
    rowData: {
      type: Object,
      default: function() {
        return {}
      }
    },
    flag: {
      type: String
    }
  },
  data() {
    return {
      editMode: false,
      error: ''
    }
  },
  computed: {
    model: {
      get() {
        return this.value
      },
      set(val) {
        this.$emit('input', val)
      }
    },
    listeners() {
      return {
        [this.closeEvent]: this.onInputExit,
        ...this.$listeners
      }
    }
  },
  methods: {
    onFieldClick() {
      if (this.canEdit) {
        this.editMode = true
        this.$nextTick(() => {
          const inputRef = this.$refs.input
          if (inputRef && inputRef.focus) {
            inputRef.focus()
          }
        })
      }
    },
    onInputEnter() {
      // enter 触发blur
      this.$refs.input.blur()
    },
    // 在这里做自定义校验
    onInputExit() {
      this.error = ''
      console.log(this.rowData)
      if (this.flag == 'bargain') {
        const rate = this.rowData.minEarningRate
        const price = abs(this.rowData.negotiatedPrice * (1 + abs(rate, 100)), 100)
        console.log('控制价:', price)
        console.log('输入价:', this.model)
        if (this.model < price) {
          this.$refs.input.focus()
          this.$message.error('不能小于最低盈利率')
          this.error = 'error'
          return false
        }
      } else {
        const ratio = this.rowData.batchRatio
        if (!validInteger(ratio)) {
          this.$message.error('请输入整数')
          return false
        }
        if (ratio < 0 || ratio > 100) {
          this.$message.error('请输入0~100之间的数字')
          return false
        }
      }
      this.editMode = false
      return false
    }
  }
}
</script>
<style>
  .cell-content {
    min-height: 28px;
    padding-left: 5px;
    padding-top: 2px;
    border: 1px solid transparent;
  }
  .error input{
    border-color: red!important;
  }
</style>