{ field: 'progress', title: '反馈进度', formatter: function(value, row, index) { var planStatus = row.planStatus; var style = ''; if (planStatus == 1) { style = 'background-color: green; color: white; border: 1px solid green;'; } else if (planStatus == 2) { style = 'background-color: red; color: white; border: 1px solid red;'; } return '<span style="' + style + '">' + value + '</span>'; } }该代码中如果判断语句比较的是字符1和字符2该如何表示
时间: 2024-04-13 18:29:16 浏览: 18
如果要比较的是字符 `'1'` 和字符 `'2'`,则可以将条件语句中的比较运算符改为字符串相等比较运算符 `===`。以下是修改后的代码示例:
```javascript
{
field: 'progress',
title: '反馈进度',
formatter: function(value, row, index) {
var planStatus = row.planStatus;
var style = '';
if (planStatus === '1') {
style = 'background-color: green; color: white; border: 1px solid green;';
} else if (planStatus === '2') {
style = 'background-color: red; color: white; border: 1px solid red;';
}
return '<span style="' + style + '">' + value + '</span>';
}
}
```
在上述代码中,使用了字符串相等比较运算符 `===` 来比较 `planStatus` 是否等于 `'1'` 或 `'2'`。这样可以确保比较的是字符而不是数字。
相关问题
field: 'actionId', title: "操作", formatter: function (value, row, index) {}操作里面添加方法,传递所有参数
您可以在formatter函数里面添加一个按钮,并且绑定一个点击事件,点击事件传递row对象作为参数,示例代码如下:
```javascript
field: 'actionId',
title: "操作",
formatter: function (value, row, index) {
var $btn = $('<button>').text('点击');
$btn.click(function() {
// 在这里处理点击事件,传递row对象作为参数
console.log(row);
});
return $btn;
}
```
在这个例子中,我们创建了一个按钮,并且为它绑定了一个点击事件,当用户点击这个按钮时,会在控制台上打印出row对象的内容。您可以按照您的需求修改点击事件的处理逻辑。
使用以下代码作为 progress 列的 formatter 方法,来根据 planStatus 的值来设置不同的样式: { field: 'progress', title: '反馈进度', formatter: function(value, row, index) { var planStatus = row.planStatus; var style = ''; if (planStatus == 1) { style = 'background-color: green; color: white; border: 1px solid green;'; } else if (planStatus == 2) { style = 'background-color: red; color: white; border: 1px solid red;'; } return '<span style="' + style + '">' + value + '</span>'; } } 上述代码会根据 planStatus 的值设置不同的样式,如果 planStatus 等于 1,则使用绿色背景、白色字体和绿色边框的样式显示 progress 值;如果 planStatus 不等于 1,则使用红色背景、白色字体和红色边框的样式显示 progress 值。此方法在表格列中没有效果是什么原因
如果上述代码在表格列中没有效果,可能有以下几个原因:
1. 代码未正确应用到表格列:请确保你将上述代码正确地应用到了表格的 `columns` 配置中,即将其放置在 `progress` 列的配置中。检查一下代码中的 `columns` 数组是否正确配置了所有的列,并且在正确的位置插入了 `progress` 列的配置。
2. 样式未正确生效:确认一下代码中设置的样式是否正确。可能需要调试一下样式设置,比如检查样式的选择器是否正确、样式属性是否生效等。
3. `planStatus` 字段不存在或值不符合条件:检查一下数据中是否存在 `planStatus` 字段,并且确认它的值是否符合代码中设置的条件。如果 `planStatus` 字段不存在或者值与条件不匹配,那么样式就不会被应用。
请仔细检查以上几个方面,以确定为什么在表格列中没有看到预期的样式效果。如果问题仍然存在,请提供更多的代码或数据示例,以便我能够更好地帮助你。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
columns: [{ checkbox: true }, { field : 'indexId', title : '指标id', visible: false }, { field : 'indexName', title : '指标名称', }, { field : 'relationChar', title : '关系符号', }, { field : 'yearIndex', title : '年度指标', }, { field : 'feedbackTime', title : '反馈周期', }, { field : 'statisticsPattern', title : '统计方式', }, { field : 'measurementUnit', title : '计量单位', }, { field : 'complete', title : '完成度', }, { field : 'progress', title : '反馈进度' }, { field : 'planId', title : '计划表id', visible: false }, { field : 'indexStatus', title : '指标状态', visible: false }, { title: '操作', align: 'center', formatter: function(value, row, index) { var actions = []; actions.push('编辑 '); actions.push('反馈记录'); actions.push('删除'); return actions.join(''); } }]帮我在progress列中写一个formatter:方法要求是如果该行中planStatus值等于1这用绿色低框白字样式显示progress值,如果等于等于1这用红色低框白字样式显示progress值
![](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)
![](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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)