假设我有一个表,有时很少有数据可以为空,但空数据在表中显示为未定义而不是显示未定义我想显示“N/A”我如何实现共享图像和代码下面。
在上图中,引用数据下的一些数据显示未定义,因为它是空的,我希望它显示“N/A”。我将在下面分享代码。代码下方有一个“a.normalRange”变量,该变量保存引用数据。
var col = ["Test Name", "Result", "Unit" , "Reference"];
this.xyz.forEach((a) => {
medicineInfo = this.xyz.find(x => x.id == a.id);
rows.push(['' + a.xyz+' '+ a.xyz+ '', '' + a.xyz+ '', '' + a.xyz,'' + a.xyz]);
});
这就是我打印表格的方式
doc.autoTable({
columnStyles: {
0: { cellWidth: 45 },
1: { cellWidth: 45 },
2: { cellWidth: 45 },
3: { cellWidth: 45 }
},
head: [col],
body: rows,
startY: 100,
theme: 'plain',
tableLineColor: [242, 238, 238],
tableLineWidth: 0.5,
styles: {
font: 'courier',
lineColor: [242, 238, 238],
lineWidth: 0.5
},
});
最佳答案
如果需要检查任何其他值,请使用该值调用is Value Empty 方法。
const col = ["Test Name", "Result", "Unit" , "Reference"];
this.hubxDataItemSpList.forEach((a) => {
medicineInfo = this.hubxDataItemSpList.find(x => x.id == a.id);
const normalRange = changeIfValueEmpty(a.normalRange);
rows.push(['' + a.categoryName +' '+ a.itemTitle + '', '' + a.itemValue + '', '' + a.itemUnit ,'' + normalRange]);
});
function changeIfValueEmpty(value) {
value = value ?? ''; // reassigned if value is null or undefined
return (value value.trim() === '') ? 'N/A' : value; // to check value only contains whitespace
}
关于angular - 如果 Angular 数据为空,如何设置 "N/A",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73905365/