Home > Enterprise >  How to set "N/A" if data is null in angular
How to set "N/A" if data is null in angular

Time:09-30

Lets say i have a table and sometimes few data's can be null but null data are showing undefined at the table instead of showing undefine i want to show "N/A" how can i achieve that ill share an image and a code below.

at the above image under reference data some of the data showing undefine because it is null i want it to show "N/A". I'll share the code below. Below code there is "a.normalRange" variable that variable holds the reference data.

 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]);
    });

And this is how i print the table

 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
            },
          });

CodePudding user response:

If any other value need to be checked call isValueEmpty method with that value.

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
}

CodePudding user response:

Create angular 'pipe' that returns 'N/A' when value is undefined or null. And use it in html code when needed.

CodePudding user response:

Use the coalescing operator, which tests for null and undefined.

{{ item.value ?? 'N/A' }}

If you also want 0 and '' to be counted as N/A, then use the short circuit :

{{ item.value || 'N/A' }}

Or, apply this to your TS logic (and also improve it) :

rows.push([
  `${a.categoryName} ${a.itemTitle}`.trim() ?? 'N/A',
  a.itemValue ?? 'N/A',
  a.itemUnit ?? 'N/A',
  a.normalRange ?? 'N/A',
]);

(You can apply this to every line if you want)

  • Related