#reactjs #antd
Вопрос:
В своем проекте react я добавляю таблицу дизайна ANT и реализовал индивидуальный поиск по столбцам для фильтрации своих данных.
Есть ли способ отфильтровать данные в диапазоне в таблице ANT? Как и в моей колонке «посещаемость«, я хочу отфильтровать учащихся с посещаемостью от 50 до 80. Как я могу достичь этого в таблице ANT?
Спасибо.
код
import React, { useState, useRef } from 'react';
import { Table, Input, Button, Space } from 'antd';
import Highlighter from 'react-highlight-words';
import { SearchOutlined } from '@ant-design/icons';
import '../node_modules/antd/dist/antd.css';
const App = () => {
const [searchText, setSearchText] = useState('');
const [searchedColumn, setSearchedColumn] = useState('');
const searchInput = useRef(null);
let student = [
{
rollNo: 1, name: "Rohit", fatherName: "Lalit", motherName: "Abha", age: 22, marks: 54, address: "Delhi", attendance: 85, contact: 7097991361
},
{
rollNo: 2, name: "Rohan", fatherName: "Ram", motherName: "Akanksha", age: 23, marks: 60, address: "Mumbai", attendance: 56, contact: 7097775553
},
{
rollNo: 3, name: "Ishfaq", fatherName: "Maqbool", motherName: "Amala", age: 24, marks: 49, address: "Chennai", attendance: 73, contact: 7097891779
},
{
rollNo: 4, name: "Sanjay", fatherName: "Ramesh", motherName: "Aditi", age: 24, marks: 44, address: "Mohali", attendance: 55, contact: 7097683032
},
{
rollNo: 5, name: "Anuj", fatherName: "Narayan", motherName: "Amita", age: 23, marks: 56, address: "Delhi", attendance: 60, contact: 7097574082
},
]
const getColumnSearchProps = dataIndex => ({
filterDropdown: ({ setSelectedKeys, selectedKeys, confirm, clearFilters }) => (
<div style={{ padding: 8 }}>
<Input
type={dataIndex === 'age' ? 'number' : dataIndex === 'marks' ? 'number' : dataIndex === 'attendance' ? 'number' : dataIndex === 'contact' ? 'number' : 'text'}
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' }}
/>
<Space>
<Button
type="primary"
onClick={() => handleSearch(selectedKeys, confirm, dataIndex)}
icon={<SearchOutlined />}
size="small"
style={{ width: 90 }}
>
Search
</Button>
<Button onClick={() => handleReset(clearFilters)} size="small" style={{ width: 90 }}>
Reset
</Button>
<Button
type="link"
size="small"
onClick={() => {
confirm({ closeDropdown: false });
setSearchText({
searchText: selectedKeys[0],
});
setSearchedColumn({
searchedColumn: dataIndex,
});
}}
>
Filter
</Button>
</Space>
</div>
),
filterIcon: filtered => <SearchOutlined style={{ color: filtered ? '#1890ff' : undefined }} />,
onFilter: (value, record) =>
record[dataIndex]
? 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
),
});
const handleSearch = (selectedKeys, confirm, dataIndex) => {
confirm()
setSearchText({
searchText: selectedKeys[0],
});
setSearchedColumn({
searchedColumn: dataIndex,
});
};
const handleReset = clearFilters => {
clearFilters();
setSearchText({ searchText: '' });
};
const columns = [
{
title: 'Roll No.',
dataIndex: 'rollNo',
key: 'rollNo',
},
{
title: 'Name',
dataIndex: 'name',
key: 'name',
...(getColumnSearchProps)('name'),
},
{
title: 'Father Name',
dataIndex: 'fatherName',
key: 'fatherName',
...(getColumnSearchProps)('fatherName'),
},
{
title: 'Mother Name',
dataIndex: 'motherName',
key: 'motherName',
...(getColumnSearchProps)('motherName'),
},
{
title: 'Age',
dataIndex: 'age',
key: 'age',
...(getColumnSearchProps)('age'),
},
{
title: 'Marks',
dataIndex: 'marks',
key: 'marks',
...(getColumnSearchProps)('marks'),
},
{
title: 'Address',
dataIndex: 'address',
key: 'address',
...(getColumnSearchProps)('address'),
},
{
title: 'Attendance',
dataIndex: 'attendance',
key: 'attendance',
...(getColumnSearchProps)('attendance'),
},
{
title: 'Fine',
dataIndex: 'fine',
key: 'fine',
},
{
title: 'Status',
dataIndex: 'status',
key: 'status',
},
{
title: 'Contact',
dataIndex: 'contact',
key: 'contact',
...(getColumnSearchProps)('contact'),
},
];
return <Table columns={columns} dataSource={student} />;
}
export default App;