@Html.DisplayFor(modelItem => item.role) 怎么写三目运算符
时间: 2023-06-19 07:03:52 浏览: 134
可以将该语句改写为三目运算符的形式,如下所示:
```
@(item.role == null ? "" : Html.DisplayFor(modelItem => item.role))
```
上述代码中,如果 `item.role` 的值为 `null`,则三目运算符返回空字符串 `""`;否则,返回 `Html.DisplayFor(modelItem => item.role)` 的值。
相关问题
if (this.ruleForm.id) { alarmClassifyUpdate(this.ruleForm).then((res => { if (res.code == 200) { this.$message({ type: 'success', message: '编辑成功!' }); this.getalarmlists() this.dialogVisible = false } else { this.$message('输入值有误,请检查后重试'); } })) } else { alarmConfigAdd(this.ruleForm).then((res => { if (res.code == 200) { this.$message({ type: 'success', message: '新增成功!' }); this.getalarmlists() this.dialogVisible = false } else { this.$message('输入值有误,请检查后重试'); } })) } 优化代码
你可以将相同的代码抽取出来,然后再使用三目运算符进行判断。优化后的代码如下:
```
const api = this.ruleForm.id ? alarmClassifyUpdate : alarmConfigAdd
api(this.ruleForm).then((res) => {
if (res.code == 200) {
this.$message({
type: 'success',
message: this.ruleForm.id ? '编辑成功!' : '新增成功!'
});
this.getalarmlists()
this.dialogVisible = false
} else {
this.$message('输入值有误,请检查后重试');
}
})
```
// 获取表单值 const form = getFieldsValue(); // 条件值的component类型会随着字段的变化发生变化 // 条件值发生变化时获取当前字段列详细数据 const currentFieldData = columnOptions.value.find( (item: Recordable) => item.value === form.field, ) as unknown as Recordable; // 根据字段label const fieldLabel = columnOptions.value.find((item) => item.value === form?.field)?.label; // 判断条件 const conditionType = ARITHMETIC_OPERATOR_BACK.find( (item) => item.value === form?.op, )?.operate; if (form.op === ConditionTypeEnum.IS_NULL || form.op === ConditionTypeEnum.IS_NOT_NULL) { // 判断条件为空、非空时,清空条件值 conditionName = ${fieldLabel ?? ''}${conditionType ?? ''}; } else { // 字段值和判断条件发生变化 let labelText; // 字段变化 if (key === 'field') { if (currentFieldData?.component !== FormElementEnum.SWITCH) { // 清空条件值 await setFieldsValue({ value: undefined }); labelText = ''; } else { await setFieldsValue({ value: 0 }); labelText = '否'; } } else { // 判断条件和条件值变化 // 当前字段有值集时,component类型为ApiSelect if (currentFieldData?.component === FormElementEnum.API_SELECT) { const fieldVsList = await getVsList(currentFieldData.vsId); labelText = fieldVsList?.find( (item: Recordable) => item.itemValue === form.value, )?.itemText; } else if (currentFieldData?.component === FormElementEnum.SWITCH) { // 当前字段类型为boolean时,component类型为Switch,条件值为开关 labelText = form.value === 1 ? '是' : '否'; } else { // 其他 labelText = form.value; } } conditionName = ${fieldLabel ?? ''}${conditionType ?? ''}${labelText ?? ''}; }简化if语句
可以将 if-else 语句简化为三目运算符,如下所示:
```
conditionName = form.op === ConditionTypeEnum.IS_NULL || form.op === ConditionTypeEnum.IS_NOT_NULL
? `${fieldLabel ?? ''}${conditionType ?? ''}`
: key === 'field'
? currentFieldData?.component !== FormElementEnum.SWITCH
? (await setFieldsValue({ value: undefined }), '')
: (await setFieldsValue({ value: 0 }), '否')
: currentFieldData?.component === FormElementEnum.API_SELECT
? ((await getVsList(currentFieldData.vsId)), labelText = fieldVsList?.find((item: Recordable) => item.itemValue === form.value)?.itemText)
: currentFieldData?.component === FormElementEnum.SWITCH
? (labelText = form.value === 1 ? '是' : '否')
: (labelText = form.value, '');
```
这样可以减少代码量,使代码更加简洁。
阅读全文
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![pptx](https://img-home.csdnimg.cn/images/20241231044947.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)