How to add multiple column conditions in tanstack column useMemo?

64 Views Asked by At

I am just beginning to learn tanstack/react-table v8.11.7. Here is what I need some help with:

I fetch my data from an api, that returns 5 columns - College code & Name, Branch Code & Name and the Degree it offers. e.g. "143, Modern College, FT, Food Technology, 4 years bachelor of engineering'

In the react-table useMemo, I want to have only two columns - College Code and Name, and Branch (Degree)

Here is my column UseMemo code.

 const columns = useMemo(
    () => [
      {
        accessorFn: (row) => `${row.c001_instcode}-${row.c001_instname}`,
        header: "Institute",
      },
      {
        accessorFn: (row) => row.s001_branchcode !== "NAN"
            ? `${row.s001_branchcode}-${row.s001_branchname}`
            : `${row.s001_branchname}`,
        header: "Branch (Degree)",
      },
],[] 
);

Print the institute(merge of code and name) and branch(check if branch code is NAN, then print only branchname, else print both branchcode and branchname separated by dash.

So far, it is working fine.

Now, I want to add another condition to the existing branch condition, that should now include the degree as well.

e.g. if degree = "4 YEARS BACHELOR OF ENGINEERING", print degree as "(4 YRS B.E)", else print the degree as is. Also, this should print along with branch, like so. "FOOD TECHNOLOGY (4 YRS B.E)" How can I achieve this? I have tried using function in accessorFn, it worked fine outside the useMemo, but it expects me to include the function inside of useMemo. I couldn't achieve that. Please suggest ways I could achieve this.

0

There are 0 best solutions below