data-disabled="{{(m1.test(regx, item) || item == 'clear') ? '' : 'disabled'}} 解释解释
时间: 2024-01-06 18:03:43 浏览: 21
这是一段 Vue 模板代码,含义如下:
1. `m1` 是一个正则表达式对象。
2. `regx` 是一个正则表达式字符串,用于构造正则表达式对象 `m1`。
3. `item` 是一个字符串变量,表示当前的某个值。
4. `?` 表示三目运算符的开始。
5. `m1.test(regx, item) || item == 'clear'` 是一个条件表达式,如果 `m1` 正则匹配 `item` 或者 `item` 等于 `'clear'`,则返回 `true`,否则返回 `false`。
6. 如果条件表达式返回 `true`,则 `data-disabled` 属性的值为 `''`,表示该属性可用。
7. 如果条件表达式返回 `false`,则 `data-disabled` 属性的值为 `'disabled'`,表示该属性不可用。
相关问题
<template slot-scope="scope"> <el-form-item :prop="'list.' + scope.$index + '.controlok'" :rules="fromDataRules.controlok" > <el-input v-if="scope.row.controluse === 'S2F21'" :disabled=" scope.row.iscontrol === false || scope.row.iscontrol == null || scope.row.iscontrol == '' " v-model="scope.row.controlok" filterable clearable /> <el-select v-else v-model="scope.row.controlok" :disabled=" scope.row.iscontrol === false || scope.row.iscontrol == null || scope.row.iscontrol == '' " :placeholder="t('web_ems_tip_pleaseInput')" filterable clearable > <el-option v-for="item in identifyingList" :key="item.id" :label="item" :value="item" /> </el-select> </el-form-item> </template>中表单验证controlok: [{ validator: checkPass, trigger: "blur" }],checkPass方法中怎么获取到该行数
可以通过传入参数的方式获取到该行数。在定义checkPass方法时,可以在参数中传入当前表单项的值和当前表单项的索引。例如:
```
checkPass(rule, value, callback, index) {
// index即为当前表单项的索引
// 可以根据索引来获取到该行数
const row = this.list[index];
// ...
}
```
在模板中调用该方法时,需要将当前表单项的索引作为参数传入:
```
<el-form-item
:prop="'list.' + scope.$index + '.controlok'"
:rules="fromDataRules.controlok"
>
<el-input
v-if="scope.row.controluse === 'S2F21'"
:disabled="
scope.row.iscontrol === false ||
scope.row.iscontrol == null ||
scope.row.iscontrol == ''
"
v-model="scope.row.controlok"
filterable
clearable
:class="{ 'is-invalid': errors['list.' + scope.$index + '.controlok'] }"
/>
<el-select
v-else
v-model="scope.row.controlok"
:disabled="
scope.row.iscontrol === false ||
scope.row.iscontrol == null ||
scope.row.iscontrol == ''
"
:placeholder="t('web_ems_tip_pleaseInput')"
filterable
clearable
:class="{ 'is-invalid': errors['list.' + scope.$index + '.controlok'] }"
>
<el-option
v-for="item in identifyingList"
:key="item.id"
:label="item"
:value="item"
/>
</el-select>
</el-form-item>
```
可以看到,在el-input和el-select中都有一个:class绑定,用来判断当前表单项是否验证通过,如果不通过,则添加一个is-invalid类名,可以在样式中对该类名进行样式设置。同时,在prop中也需要将当前表单项的索引作为属性名的一部分。这样,在定义checkPass方法时,就可以通过该属性名来获取到该行数的值。
优化代码 for (let item of that.parentTemplate) { if (item.key == 'existContract') { if (!item.disabled || !Array.isArray(item.disabled)) { item.disabled = []; } if (e === 1) { item.disabled = item.disabled.filter((disabledItem) => disabledItem != index); } else if (e === 2 || e == undefined || e === '') { that.changeForm({ ['existContract' + more]: '', }); item.disabled.push(index); } else { item.disabled = item.disabled.filter((disabledItem) => disabledItem != index); } } if (item.key == 'contractCode') { if (!item.disabled || !Array.isArray(item.disabled)) { item.disabled = []; } if (e === 1) { } else if (e === 2 || e == undefined || e === '') { that.changeForm({ ['contractCode' + more]: '', }); item.disabled.push(index); } else { item.disabled = item.disabled.filter((disabledItem) => disabledItem != index); } } if (item.key == 'isProjectCost') { if (!item.disabled || !Array.isArray(item.disabled)) { item.disabled = []; } if (e === 1) { item.disabled = item.disabled.filter((disabledItem) => disabledItem != index); } else if (e === 2 || e == undefined || e === '') { that.changeForm({ ['isProjectCost' + more]: '', }); item.disabled.push(index); } else { item.disabled = item.disabled.filter((disabledItem) => disabledItem != index); } } }
你可以尝试优化这段代码,减少重复的代码块。你可以使用一个数组来存储需要处理的字段名,然后使用循环来处理每个字段。
```javascript
const fields = ['existContract', 'contractCode', 'isProjectCost'];
for (let item of that.parentTemplate) {
for (let field of fields) {
if (item.key === field) {
if (!item.disabled || !Array.isArray(item.disabled)) {
item.disabled = [];
}
if (e === 1) {
item.disabled = item.disabled.filter((disabledItem) => disabledItem != index);
} else if (e === 2 || e == undefined || e === '') {
that.changeForm({
[field + more]: '',
});
item.disabled.push(index);
} else {
item.disabled = item.disabled.filter((disabledItem) => disabledItem != index);
}
}
}
}
```
通过引入一个 `fields` 数组,我们可以避免多次重复判断每个字段的 key。这样可以使代码更简洁,并且易于维护。