Adding another check property to select default rows on antd table

Viewed 17

Currently i have this code on my api call to add a check property (list) (based on api called data "resRole" and "res") which can be used inside of rowSelection to select all the default checked row.

However, now i have another table which I need to do the same thing. Just that instead of using resRole, now I will use resProject. Which i need to first add a key to, before i add a checkProject in "res".

As such, i updated the check to checkRole and intend to put in an additional checkDept (list) in the getAllUserRole's res.data.

Looking at my code, I do not know where I can implement it. It seems like I have to create it inside of the getDataUserRole() function but that seems too messy. And might cause some async issues.

Below is the code:

async function getDataProject() {
    let resProject = await getAllProject();
    if (resProject) {
      setDataSourceProject(resProject.data);
    }
  }
  
async function getDataUserRole() {

    let resRole = await getAllRoles();
    if (resRole) {
      //Add a key to every Role 
      for (var i = 0; i < resRole.data.length; i++) {
        resRole.data[i]["key"] = i;
      }
      setDataSourceRole(resRole.data);

      let res = await getAllUserRole();
      if (res) {
        console.log("getAllUserRole =", res);
        for (var i = 0; i < res.data.length; i++) {
          //add "check" to every email in the array
          res.data[i]["checkRole"] = [];
          //loop through all the roleIds array in each email
          for (var j = 0; j < res.data[i].roleIds.length; j++) {
            //if roleIds is not empty
            if (res.data[i].roleIds.length != 0) {
              //loop through all Role from getAllRoles and check if any roleIds match the Role. If match push the key of the Role into check
              for (var k = 0; k < resRole.data.length; k++) {
                if (res.data[i].roleIds[j] == resRole.data[k].id) {
                  res.data[i]["checkRole"].push(resRole.data[k].key);
                }
              }
            }
          }
        }

        //If groupChange (groupChange is state for storing value of radio button) is null, return untransformed data
        if (!(groupChange)) {
          setDataSourceUserRole(res.data);
        }
        //If groupChange has value, call the function with the state value as a parameter
        else {
          var treeData = groupData(res.data, groupChange)
          setDataSourceUserRole(treeData);
        }
      }
    }
  }
1 Answers

Instead of Using it inside getDataUserRole(). Use it inside getAllUserRole(). and once you get your result just add additional data with the role and send it back to one function. If you want to call it separately so then you to depend it one function on another because due to async it will not work properly

Related