3

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
            },
          });
3
  • can u post ur html code of that row Commented Sep 30, 2022 at 7:32
  • i didnt use html for this code i use the jspdf and solve it in typescript(angular) Commented Sep 30, 2022 at 7:47
  • It's one of Angular's most frustrating thing: Building tables and manipulate the data for display. In my personal experience (or what I've done) I either, build a middle-ware on the back-end so that the API sends the data as expected for display, or, I build a special "table" model. But I'm sure someone might have a more effective solution... Commented Sep 30, 2022 at 7:54

3 Answers 3

4

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
}
Sign up to request clarification or add additional context in comments.

Comments

3

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)

7 Comments

since numbers are coming OR (||) operator might show N/A for 0 value too.
@sasi66 maybe you should pay better attention to what I wrote.
still showing undefined i have tried both
@KingCk in that case it means the value of your variable is 'undefined', as a string.
a.normalRange data is a string so how do i fix it
|
2

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

2 Comments

That one could actually be very elegant.
Overkill when a simple operator ?? can do the same thing.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.