jinlin
2024-07-16 91861fec4e3113339b2e7e34a10318ff7d891df2
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
162
163
<template>
  <div class="mod-taskReliability-taskPhaseModel fa-card-a" style="margin-left: 5px;">
    <zt-table-wraper ref="tableObj" defaultNotQuery="true" query-url="/taskReliability/TaskPhaseModel/page"
                     :paging='false'
                     @dataLoaded="dataLoaded"
                     delete-url="/taskReliability/TaskPhaseModel/" v-slot="{ table }">
      <el-form :inline="true" :model="dataForm" @keyup.enter.native="table.query()">
        <el-form-item>
          <zt-button v-if="dataForm.phaseId" type="warning" @click="handleSaveRows">保存</zt-button>
        </el-form-item>
      </el-form>
      <el-table v-loading="table.dataLoading" :data="table.dataList" height="100px"
                v-adaptive="{bottomOffset:30}" border
                @cell-click="handleCellClick"
                @selection-change="table.selectionChangeHandle">
        <el-table-column prop="operatConditName" label="工况名称" width="140" align="center"/>
        <el-table-column prop="operatConditDurationRate" label="工况时长比" align="right" width="120">
          <template slot-scope="scope">
                    <span
                      v-if="editingCell && editingCell.row === scope.row && editingCell.column.property === scope.column.property">
                       <el-input ref="editInput"
                                 autosize v-model="scope.row.operatConditDurationRate"
                                 placeholder="工况时长比" @input="change()"></el-input>
                    </span>
            <span v-else>{{scope.row.operatConditDurationRate}}</span>
          </template>
        </el-table-column>
        <el-table-column align="center" label="选用">
          <template v-slot="{ row }">
            <el-checkbox true-label="1" false-label="0" v-model="row.isCheck" @change="change"></el-checkbox>
          </template>
        </el-table-column>
      </el-table>
    </zt-table-wraper>
  </div>
</template>
 
<script>
  import cloneDeep from 'lodash/cloneDeep'
 
  export default {
    name: 'TaskPhaseModel',
    data() {
      return {
        dataForm: {
          productId: '',
          phaseId: '',
          operatConditName: '',
          operatConditId: '',
          operatConditDurationRate: '',
          dataList: [],
          isCheck: '',
          dataThreeList:[]
        },
        lastSaveData: [],
        isChange: false,
        editingCell: null,
        dataList: [],
        originalTableData: [],
        originalData: null,
      }
    },
    components: {},
    methods: {
      init(param) {
        console.log(param, '')
        this.dataForm.phaseId = param.row.id
        this.dataForm.productId = param.productId
        this.$refs.tableObj.query()
      },
      //批量保存
      async handleSaveRows() {
        let operatConditDurationRate = null
        let flag = true
        let list = []
        let reg = /^(-?\d+)\.?(\d*)$/;
        this.dataForm.dataThreeList = this.$refs.tableObj.dataList
        for (let i = 0; i < this.dataForm.dataThreeList.length; i++) {
          operatConditDurationRate = this.dataForm.dataThreeList[i].operatConditDurationRate
          console.log(operatConditDurationRate)
          if (this.dataForm.dataThreeList[i].operatConditDurationRate == null)
            this.dataForm.dataThreeList[i].operatConditDurationRate = '0'
          if (this.dataForm.dataThreeList[i].isCheck === '0')
            this.dataForm.dataThreeList[i].operatConditDurationRate = '0'
          if (String(operatConditDurationRate).match(reg) == null && operatConditDurationRate !== null) {
            this.$alert("输入的数据格式有误")
            flag = false
          }
          // time = time + Number(this.dataForm.dataThreeList[i].operatConditDurationRate)
          if (this.dataForm.dataThreeList[i].isCheck === '1' && (this.dataForm.dataThreeList[i].operatConditDurationRate == null || this.dataForm.dataThreeList[i].operatConditDurationRate == 0)) {
            this.$alert("有未填写的工况时长")
            flag = false
          }
          list.push(this.dataForm.dataThreeList[i])
        }
        this.dataForm.dataThreeList = list
        /*console.log(time, 'async handleSaveRows')
        if (time > 1) {
          this.$alert("请重新设置当前时长比分配")
          return
        }*/
        if (!flag) {
          return
        }
        let res = await this.$http.post('/taskReliability/TaskPhaseModel/save', this.dataForm)
        if (res.success) {
          this.isChange = false;
          this.lastSaveData = cloneDeep(list)
          console.log(res.data)
          await this.$tip.success()
          this.originalData = null
          this.dataForm.dataThreeList = null
          this.$refs.tableObj.query()
          this.$emit('getList')
          this.originalTableData = JSON.parse(JSON.stringify(this.dataList)); // 更新初始数据为当前数据
          console.log(this.originalTableData, 'this.originalTableData 当前表格json数据')
        }
      },
      dataLoaded(data) {
        this.dataList = data
        for (let i = 0; i < this.dataList.length; i++) {
          this.dataList[i].productId = this.dataForm.productId
          this.dataList[i].phaseId = this.dataForm.phaseId
        }
        this.lastSaveData = cloneDeep(this.$refs.tableObj.dataList)
        console.log(this.lastSaveData)
      },
      handleCellClick(row, column) {
        this.editingCell = {row, column}
        this.$nextTick(() => {
          if (this.$refs.editInput) {
            this.$refs.editInput.focus()
          }
        })
        console.log(this.editingCell, 'this.editingCell')
      },
      change() {
        console.log(this.$refs.tableObj.dataList, "this.$refs.tableObj.dataList")
        console.log(this.lastSaveData, "this.lastSaveData")
 
        let r = JSON.stringify(this.$refs.tableObj.dataList);
        let l = JSON.stringify(this.lastSaveData);
        console.log(r,'this.$refs.tableObj.dataList[i]')
        console.log(l,'this.lastSaveData[i]')
        if (r!==l){
          this.isChange = true
          return
        }
          /* for (let i = 0; i < this.$refs.tableObj.dataList.length; i++) {
             if (this.$refs.tableObj.dataList[i] != this.lastSaveData[i]) {
               console.log(i,'no')
               console.log(this.$refs.tableObj.dataList[i],'this.$refs.tableObj.dataList[i]')
               console.log(this.lastSaveData[i],'this.lastSaveData[i]')
               this.isChange = true
               return
             }
           }*/
          this.isChange = false
      },
 
    }
  }
</script>