angular - 如果 Angular 数据为空,如何设置 "N/A"

标签 angular

假设我有一个表,有时很少有数据可以为空,但空数据在表中显示为未定义而不是显示未定义我想显示“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/

相关文章:

angular - 以 Angular 7 的 Multipart/formdata 形式发送数组

Angular 6 使用新的 RxJS 处理 403 响应

css - Angular 6-在生产中未加载自定义CSS

html - mat-card 中的内容未使用 flex 居中

Angular Material Datepicker - 仅限月份和年份

angular - 应用程序模块提供程序抛出 NullInjectorError : No provider for InjectionToken MSAL_GUARD_CONFIG

Angular PWA 在没有 WorkBox 的情况下添加后台同步

angular - .net 核心 2.2 和 Angular 7 : IFormFile in file upload controller is always null

angular - 在 angular2 中迁移到 RC4 后的问题

javascript - 设置文本颜色并根据数据文本更改图标