Based on the below column { title: 'User Type', dataIndex: 'role', width: '20%', key:…
- User
Based on the below column { title: 'User Type', dataIndex: 'role', width: '20%', key: 'role', ...getColumnSearchProps('role'), // sorter: (a, b) => a.role < b.role, sorter: (a, b) => userRolesById[a.role] > userRolesById[b.role] ? 1 : -1, render: (_, { role }) => renderUserType(role), }, const renderUserType = (role) => { return userRolesById[role]; }; export const userRolesById = { 6789: "DATA COLLECTOR", 1456: "SUPERVISOR", 2148: "BRANCH MANAGER", 5278: "ADMIN", 4362: "STATISTICIAN", 8346: "BRANCH STATISTICIAN", 7812: "DATA CLEANER", 3420: "DIRECTORATE DIRECTOR", 9101: "SUPER ADMIN", } why is searching by usertype not working
- Assistant
Without more information on how the
getColumnSearchPropsfunction is implemented, it is difficult to determine exactly why searching by usertype is not working. However, it is possible that thegetColumnSearchPropsfunction is designed to only search within thedataIndexfield (which in this case is 'role'), and not on the result of therenderfunction (which is whatrenderUserTypereturns). If that is the case, then searching by the user roles as displayed in the table would not be possible. - User
here is getColumnSearchProps const getColumnSearchProps = (dataIndex) => ({ filterDropdown: ({ setSelectedKeys, selectedKeys, confirm, clearFilters, }) => ( <div style={{ padding: 8, }} > <Input ref={searchInput} placeholder={
Search ${dataIndex}} value={selectedKeys[0]} onChange={(e) => setSelectedKeys(e.target.value ? [e.target.value] : []) } onPressEnter={() => handleSearch(selectedKeys, confirm, dataIndex)} style={{ marginBottom: 8, display: 'block', }} /> <Button type='primary' onClick={() => handleSearch(selectedKeys, confirm, dataIndex)} icon={} size='small' style={{ width: 90, }} > Search <Button onClick={() => clearFilters && handleReset(clearFilters)} size='small' style={{ width: 90, }} > Reset <Button type='link' size='small' onClick={() => { confirm({ closeDropdown: false, }); setSearchText(selectedKeys[0]); setSearchedColumn(dataIndex); }} > Filter ), filterIcon: (filtered) => ( <SearchOutlined style={{ color: filtered ? '#1890ff' : undefined, }} /> ), onFilter: (value, record) => record[dataIndex].toString().toLowerCase().includes(value.toLowerCase()), onFilterDropdownVisibleChange: (visible) => { if (visible) { setTimeout(() => searchInput.current?.select(), 100); } }, render: (text) => searchedColumn === dataIndex ? ( <Highlighter highlightStyle={{ backgroundColor: '#ffc069', padding: 0, }} searchWords={[searchText]} autoEscape textToHighlight={text ? text.toString() : ''} /> ) : ( text ), }); - Assistant
Based on the implementation of
getColumnSearchProps, it appears that it is designed to filter based on thedataIndexfield only, and not the result of therenderfunction.In this case, the
dataIndexis 'role', and so only the values in the 'role' column will be searched. However, therenderfunction is providing a new value for each row based on theuserRolesByIdobject, and this new value is not searchable using the current implementation ofgetColumnSearchProps.To enable searching by usertype, the
onFilterlogic ingetColumnSearchPropscould be modified to filter based on the value returned byrenderUserType(role)instead ofrecord[dataIndex].Here's an example of how the
onFilterlogic could be updated:onFilter: (value, record) => { const userType = renderUserType(record[dataIndex]); return userType.toLowerCase().includes(value.toLowerCase()); }This will enable searching by usertype as displayed in the table. Note that the
renderfunction can remain the same. - User
will the above change affect searching by all these values const columns = [ { title: 'Users', dataIndex: 'fullName', key: 'fullName', width: '40%', ...getColumnSearchProps('fullName'), // sorter: (a, b) => a.fullName.length - b.fullName.length, sorter: (a, b) => a.fullName.localeCompare(b.fullName), }, { title: 'Status', dataIndex: 'disabled', key: 'disabled', width: '20%', ...getColumnSearchProps('disabled'), sorter: (a, b) => (a.disabled === b.disabled ? 0 : a.disabled ? -1 : 1), //return 0 if they’re equal, -1 if the first value is true, and 1 if the second value is true. render: (, { disabled }) => renderUserStatus(disabled), }, { title: 'User Type', dataIndex: 'role', width: '20%', key: 'role', ...getColumnSearchProps('role'), // sorter: (a, b) => a.role < b.role, sorter: (a, b) => userRolesById[a.role] > userRolesById[b.role] ? 1 : -1, render: (, { role }) => renderUserType(role), }, { title: 'User Name', dataIndex: 'userName', width: '20%', key: 'userName', ...getColumnSearchProps('userName'), sorter: (a, b) => a.userName.toLowerCase() < b.userName.toLowerCase(), }, ];
- Assistant
No, the above change will not affect filtering by
fullName,disabled, oruserName. Each column that has search enabled is using a separateonFilterfunction that filters based on the value of thedataIndexfield.Changing the
onFilterlogic forroleto filter based onrenderUserType(role)will only update the filtering behavior for that particular column. The other columns will continue to filter based on thedataIndexvalue as defined in their respectiveonFilterfunctions.So, the searching for
fullNamewill remain based onrecord['fullName'], searching fordisabledwill remain based onrecord['disabled'], and searching foruserNamewil remain based onrecord['userName'].