vendredi 3 septembre 2021

ReactJS - Validate Nested-IF/ELSE Statement

In my react app I'm using IF statement inside another IF statement to validate a condition that I want to get data filtered only from a specific column not from all.

Basically I'm using AntD table in which I'm displaying students data. In a table I do have few columns namely Age, Marks and Attendance. On each column(Age, Marks and Attendance) I'm applying a range filter with aim to fetch student(s) when applied search on a specific column i.e. either Age, Marks or on Attendance.

The problem I'm getting that my Nested-IF condition is not validating to true. Rest of code is working fine. I also tried to run code without Nested-IF(I mean with the simple IF/ELSE statement). That is also working fine.

I don't know what did I wrong or what I'm missing/skipping in my attempt.

Help me to validate my Nested-IF/ELSE statement(s).

What I'm trying is:

const handleSearch = (firstInput, secondInput, dataIndex) => {
  setSearchStu(
    student.filter((obj) => {
      if (dataIndex === "age") {
        if (obj.age >= firstInput && obj.age <= secondInput) {
          return true;
        } else {
          return false;
        }
      } else {
        if (dataIndex === "marks") {
          if (obj.marks >= firstInput && obj.marks <= secondInput) {
            return true;
          } else {
            return false;
          }
        } else {
          if (dataIndex === "attendance") {
            if (obj.attendance >= firstInput && obj.attendance <= secondInput) {
              return true;
            } else {
              return false;
            }
          } else {
            return false;
          }
        }
      }
    })
  );
};

Where

firstInput is the 1st input field in a range filter

secondInput is the 2nd input field in a range filter

dataIndex is like a key to validate my 1st IF statement in Nested-IF case

Values "age", "marks" and "attendance" are the defined values which are uniquely correspond to the each columns(Age, Marks and Attendance) in the table

and so on..

Thank you in advance for your assistance.

Aucun commentaire:

Enregistrer un commentaire