Element中el-form嵌套el-table双击编辑提交检验

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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
<el-form ref="form" :rules="rules" :model="form">
     <el-table
       class="treat-table"
       :data="form.dataTable.slice((pageIndex-1)*pageSize,pageIndex*pageSize)"
       height="100%"
       @cell-dblclick="dbClickHandle"
       @selection-change="handleSelectionChange"
     >
       <el-table-column align="center" type="selection" width="50" />
 
       <el-table-column label="序号" width="60px" align="center">
         <template slot-scope="scope">
           {{ setIndex(scope.$index,pageIndex,pageSize) }}
         </template>
       </el-table-column>
 
       <el-table-column label="设备名称" width="100px" show-overflow-tooltip prop="AssetName" />
 
       <el-table-column label="策略名称" prop="RuleName">
         <template slot-scope="{row,$index}">
           <el-form-item v-if="showEdit[$index]['RuleName']" class="el-form-item-class" :prop=" 'dataTable.' + $index + '.RuleName' " :rules="rules.RuleName" :data="form.dataTable.RuleName">
             <el-input
               :ref="'RuleName'+$index"
               v-model="row.RuleName"
               @keyup.enter.native="$event.target.blur"
               @change="handleEdit(row, $index)"
               @blur="inputBlur(row,$index,'RuleName')"
             />
           </el-form-item>
           <span v-if="!showEdit[$index]['RuleName']">{{ row.RuleName }}</span>
         </template>
       </el-table-column>
 
       <el-table-column label="协议" prop="Prot" width="100">
         <template slot-scope="{row,$index}">
           <el-form-item v-if="showEdit[$index]['Prot']" class="el-form-item-class" :prop=" 'dataTable.' + $index + '.Prot' " :data="form.dataTable.Prot">
             <el-select
               :ref="'Prot'+$index"
               v-model="row.Prot"
               filterable
               @visible-change="visibleChange($event,row,$index,'Prot')"
               @change="selectChange(row, $index)"
             >
               <el-option
                 v-for="item in protList"
                 :key="item.id"
                 :label="item.Prot"
                 :value="item.Prot"
               />
             </el-select>
           </el-form-item>
 
           <span v-if="!showEdit[$index]['Prot']">{{ row.Prot }}</span>
         </template>
       </el-table-column>
 
       <el-table-column label="业务规则" prop="BusinessIndex" width="150">
         <template slot-scope="{row,$index}">
           <el-form-item v-if="showEdit[$index]['BusinessIndex']" class="el-form-item-class" :prop=" 'dataTable.' + $index + '.BusinessIndex' " :data="form.dataTable.BusinessIndex">
             <el-select
               :ref="'BusinessIndex'+$index"
               v-model="row.BusinessIndex"
               filterable
               @visible-change="visibleChange($event,row,$index,'BusinessIndex')"
               @change="selectChange(row, $index)"
             >
               <el-option
                 v-for="item in businessList"
                 :key="item.BusinessIndex"
                 :label="item.BusinessName"
                 :value="item.BusinessIndex"
               />
             </el-select>
           </el-form-item>
 
           <span v-if="!showEdit[$index]['BusinessIndex']">{{ toBusiness(row.BusinessIndex) }}</span>
         </template>
       </el-table-column>
 
       <el-table-column label="源地址" prop="Srcip" width="220">
         <template slot-scope="{row,$index}">
           <el-form-item v-if="showEdit[$index]['Srcip']" :prop=" 'dataTable.' + $index + '.Srcip' " :rules="rules.Srcip">
             <el-input
               :ref="'Srcip'+$index"
               v-model="row.Srcip"
               oninput="value=value.replace(/[ a-zA-Z`~!@#$%^&()_+=<>?{}|,;'\\[\]·~!@#¥%……&()——+={}|《》?:“”【】、;‘’,。、\u4e00-\u9fa5]/g, '')"
               @keyup.enter.native="$event.target.blur"
               @change="handleEdit(row, $index)"
               @blur="inputBlur(row,$index,'Srcip')"
             />
           </el-form-item>
           <span v-if="!showEdit[$index]['Srcip']">{{ row.Srcip }}</span>
 
         </template>
       </el-table-column>
 
       <el-table-column label="源端口" prop="Srcport" width="120">
         <template slot-scope="{row,$index}">
           <el-form-item v-if="showEdit[$index]['Srcport']" :prop=" 'dataTable.' + $index + '.Srcport' " :rules="rules.Srcport">
             <el-input
               :ref="'Srcport'+$index"
               v-model="row.Srcport"
               oninput="value=value.replace(/[ a-zA-Z`~!@#$%^&()_+=<>?{}|,;'\\[\]·~!@#¥%……&()——+={}|《》?:“”【】、;‘’,。、\u4e00-\u9fa5]/g, '')"
               @keyup.enter.native="$event.target.blur"
               @change="handleEdit(row, $index)"
               @blur="inputBlur(row,$index,'Srcport')"
             />
           </el-form-item>
           <span v-if="!showEdit[$index]['Srcport']">{{ row.Srcport }}</span>
 
         </template>
       </el-table-column>
 
       <el-table-column label="目的地址" prop="Dstip" width="220">
         <template slot-scope="{row,$index}">
           <el-form-item v-if="showEdit[$index]['Dstip']" :prop=" 'dataTable.' + $index + '.Dstip' " :rules="rules.Dstip">
             <el-input
               :ref="'Dstip'+$index"
               v-model="row.Dstip"
               oninput="value=value.replace(/[ a-zA-Z`~!@#$%^&()_+=<>?{}|,;'\\[\]·~!@#¥%……&()——+={}|《》?:“”【】、;‘’,。、\u4e00-\u9fa5]/g, '')"
               @keyup.enter.native="$event.target.blur"
               @change="handleEdit(row, $index)"
               @blur="inputBlur(row,$index,'Dstip')"
             />
           </el-form-item>
           <span v-if="!showEdit[$index]['Dstip']">{{ row.Dstip }}</span>
 
         </template>
       </el-table-column>
 
       <el-table-column label="目的端口" prop="Dstport" width="120">
         <template slot-scope="{row,$index}">
           <el-form-item v-if="showEdit[$index]['Dstport']" :prop=" 'dataTable.' + $index + '.Dstport' " :rules="rules.Dstport">
             <el-input
               :ref="'Dstport'+$index"
               v-model="row.Dstport"
               oninput="value=value.replace(/[ a-zA-Z`~!@#$%^&()_+=<>?{}|,;'\\[\]·~!@#¥%……&()——+={}|《》?:“”【】、;‘’,。、\u4e00-\u9fa5]/g, '')"
               @keyup.enter.native="$event.target.blur"
               @change="handleEdit(row, $index)"
               @blur="inputBlur(row,$index,'Dstport')"
             />
           </el-form-item>
           <span v-if="!showEdit[$index]['Dstport']">{{ row.Dstport }}</span>
 
         </template>
       </el-table-column>
 
       <el-table-column label="启用" align="center" width="50">
         <template slot-scope="scope">
           <el-switch
             :value="scope.row.using"
             active-color="#227EE1"
             inactive-color="#BCBCBC"
             @change="changeC(scope.row)"
           />
         </template>
       </el-table-column>
 
       <el-table-column label="操作" align="center" width="250">
         <template slot-scope="scope">
           <el-tooltip content="下发" placement="top" effect="light">
             <img
               style="cursor: pointer;height:13px;width:13px"
               :src="require('@/assets/button-Icon/down1.png')"
               @click="IssueHandleCheck([scope.row])"
             >
           </el-tooltip>
           <el-tooltip content="关联事件" placement="top" effect="light">
             <img
               style="margin-top:5px;cursor: pointer;margin-left:10px"
               :src="require('@/assets/button-Icon/associate.png')"
               @click="openEventDialog(scope.row)"
             >
           </el-tooltip>
         </template>
       </el-table-column>
 
     </el-table>
   </el-form>

  js:

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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
export default {
  name: 'GatekeeperTreat',
  components: {
    ComponentEventsDialog,
    ComponentLoginValidate
  },
  directives: {
    focus: {
      // 指令的定义
      inserted: function(el) {
        el.focus()
      }
    }
  },
  data() {
    // 判断Ip
    var checkIp = (rule, value, callback) => {
      checkValidateIpAndPort(value, 'IP', callback)
      callback()
    }
    //  判断端口
    var checkPort = (rule, value, callback) => {
      checkValidateIpAndPort(value, 'PORT', callback)
      callback()
    }
    return {
      form: {
        dataTable: [
        ]
      },
      // dataTable: [],
      pageIndex: 1,
      pageSize: 15,
      totleCount: 0,
      multipleSelection: [],
      loading: false,
      businessList: [
        { BusinessIndex: 0,
          BusinessName: '无' }
      ],
 
      // 显示编辑框
      showEdit: [],
 
      // 协议数据
      protList: [
        {
          id: 0,
          Prot: 'TCP'
        },
        {
          id: 1,
          Prot: 'UDP'
        },
        {
          id: 2,
          Prot: 'ICMP'
        }
      ],
 
      loginIshow: false, // 是否显示二次验证弹框
 
      // 校验
      rules: {
        // 名称
        RuleName: [
          { required: true, message: '名称不能为空', trigger: 'blur' },
          { min: 1, max: 63, message: '长度在 1 到 63 个字符', trigger: 'blur' }
        ],
        // 源地址
        Srcip: [
          { required: true, message: '源地址不能为空', trigger: 'blur' },
          { validator: checkIp, trigger: 'blur' }
        ],
        // 源端口
        Srcport: [
          { required: true, message: '源端口不能为空', trigger: 'blur' },
          { validator: checkPort, trigger: 'blur' }
        ],
        // 目的地址
        Dstip: [
          { required: true, message: '目的地址不能为空', trigger: 'blur' },
          { validator: checkIp, trigger: 'blur' }
        ],
        // 目的端口
        Dstport: [
          { required: true, message: '目的端口不能为空', trigger: 'blur' },
          { validator: checkPort, trigger: 'blur' }
        ]
      }
    }
  },
  watch: {
    // 监控tableData数据,发生改变的时候跟新单元格显示状态
    'form.dataTable': function() {
      this.setShowEdit()
    }
  },
  methods: {
    showUs(datas) {
      this.analysis(datas)
    },
    setIndex(index, page, size) {
      return index + 1 + (page - 1) * size
    },
    visibleChange(flag, row, index, column) {
      if (!flag) {
        this.inputBlur(row, index, column)
      }
    },
    // 初始化单元格显示状态
    setShowEditInit() {
      for (const item of this.showEdit) {
        for (const innerItem in item) {
          item[innerItem] = false
        }
      }
    },
 
    // 设置单元显示转换数据
    setShowEdit() {
      const tempShowEdit = []
      for (const item of this.form.dataTable) {
        const tempShow = {}
        for (const innerItem in item) {
          tempShow[innerItem] = false
        }
        tempShowEdit.push(tempShow)
      }
      this.showEdit = tempShowEdit
    },
 
    // 切换单元格为编辑
    dbClickHandle(row, column, cell, event) {
      const nowObj = column.property
      const index = this.form.dataTable.indexOf(row)
      this.showEdit[index][nowObj] = true
 
      if (nowObj === 'BusinessIndex') {
        this.getBusinessHandle(row.Code)
      }
 
      this.$nextTick(() => {
        this.$refs[nowObj + index].focus()
      })
    },
 
    // 点击修改
    handleEdit(row, index) {
      this.form.dataTable[index] = row
    },
 
    // 失焦
    inputBlur(row, index, column) {
      const idx = this.form.dataTable.indexOf(row)
      if (idx === index) {
        var datas = this.showEdit[index]
        var prop = 'dataTable.' + index + '.' + column
 
        this.$refs['form'].validateField(prop, (error) => {
          if (!error) {
            if (datas) {
              for (const innerItem in datas) {
                if (innerItem === column && datas[innerItem]) {
                  datas[innerItem] = false
                }
              }
            }
          }
        })
      }
    },
 
    // 下拉框修改
    selectChange(row, index) {
      const tempTableData = this.form.dataTable
      tempTableData[index] = row
      this.form.dataTable = tempTableData
    },
 
 
    // 下发(校验有错误不让下发)
    IssueHandleCheck(datas) {
      this.$refs['form'].validate((valid) => {
        if (!valid) return
        if (datas && datas.length > 0) {
          this.$refs.LoginValidateRef.showUs()
 
          if (this.loginIshow) {
            this.$refs.LoginValidateRef.executePar(() => {
              this.loading = true
              this.bulkAddGatekeeper(datas)
            })
          } else {
            this.loading = true
            this.bulkAddGatekeeper(datas)
          }
        } else {
          this.$message.info('请选择下发策略')
          this.loading = false
        }
      })
    },
 
    bulkAddGatekeeper(datas) {
      bulkAddGatekeeper(datas, this.$store.state.user.accountName).then(res => {
        if (res) {
          if (res.code === 0) {
            this.$message.success('策略下发成功')
            this.$emit('initData', true)
          } else {
            this.$message.error(res.msg)
          }
        }
        this.loading = false
      }).catch(err => {
        this.loading = false
        console.error(err)
      })
    },
 
    toBusiness(str) {
      var result = '无'
      for (var business of this.businessList) {
        if (business.BusinessIndex === str) {
          result = business.BusinessName
          break
        }
      }
      return result
    },
 
    // 单选框选中获取数据
    handleSelectionChange(val) {
      this.multipleSelection = val
    },
 
    // 页面改变
    pageSizeChange(pageSize) {
      this.pageSize = pageSize
    },
 
    // 当前页
    pageCurrentChange(pageIndex) {
      this.pageIndex = pageIndex
    },
 
    // 获取业务规则
    async getBusinessHandle(id) {
      const data = {
        id: id
      }
      this.businessList = [
        { BusinessIndex: 0,
          BusinessName: '无' }
      ]
      await LoadBusiness(data).then(res => {
        if (res) {
          if (res.code === 0) {
            this.businessList = []
            this.businessList = res.data.data.Data
            this.businessList.unshift({
              BusinessIndex: 0,
              BusinessName: '无'
            })
          } else {
            this.$message.error(res.msg)
          }
        }
      })
    },
    openEventDialog(data) {
      this.$refs.eventsDialogRef.showUs(data)
    },
    // 是否启用
    changeC(data) {
      data.using = !data.using
    }
  }
}

  

posted @   石shi  阅读(565)  评论(0编辑  收藏  举报
编辑推荐:
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
阅读排行:
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 字符编码:从基础到乱码解决
点击右上角即可分享
微信分享提示