1

我从 api(api1) 获取数据并发送到表并渲染它,然后我从另一个 api(api2) 获取一些数据所以......

如果它从 api2 接收数据(如果它在表中但不知道如何),我想默认被选中?

//*this get data from api1 and render in table*
const [permissionList, setPermissionList] = useState([]);
async function getPermission() {
try {
    const response = await Fetch({
       body: null,
       type: "GET",
       auth: token,
       api: "listPermission"
     });
    const rows = [];
    response.permissions.map((row, i) =>
        rows.push([row.id, row.method, row.description])
     );
    setPermissionList(rows);
    } catch (err) {
        console.log(err);
    }
useEffect(() => {
    getPermission();
}, []);

//*this code for config selectableRows option dataTable*
const newOption = { ...options }; //options includes other configurations
newOption.selectableRows = "multiple";

<MUIDataTable
    className={classes.table}
    data={permissionList}
    columns={columns}
    options={newOption}
/>
//*with this code can get some information from row selected and can access to data render in the row*
newOption.customToolbarSelect = function CustomHeader(
     selectedRows, // when selected row information from row in this variable
     displayData // when selected row this variable includes all data in table
 ) {
    //*this below code : permissions includes selected row*
    let permissions = selectedRows.data.map(
        index =>
            displayData.filter(i => i.dataIndex === index.dataIndex)[0].data[0]
    );

In the following with below code : I have both the selected(from api2) and the new ones selected but problem this function runs when one of the rows is selected

    // if (data) { //data from another api2
    //   const ddd = data.permission.map(row =>
    //     displayData.filter(i => i.data[0] === row.id)
    //   );
    //   ddd.map((row, i) => {
    //     selectedRows = {
    //       data: [
    //         ...selectedRows.data,
    //         { index: row[0].dataIndex, dataIndex: row[0].dataIndex }
    //       ],
    //       lookup: {
    //         ...selectedRows.lookup,
    //         [row[0].dataIndex]: true
    //       }
    //     };
    //   });
    // }
    //
    // console.log("selectedRows", selectedRows);
    // console.log("displayData", displayData);
  };
4

0 回答 0