在React中如何筛选多个表格列

huangapple go评论78阅读模式
英文:

How to filter multiple table columns in reactjs

问题

在下面的React组件返回语句中,我有两个搜索框。按姓名搜索正常工作,但如何添加另一个搜索条件来按地块号搜索呢?
是否可以在同一过滤语句中添加另一个三元运算符?还是我需要为地块号列编写单独的语句?

return (
    <>
        <div className="input-group mb-3">
            <form>
                <input type='text' className="form-control searchBar" placeholder='按姓名搜索' onChange={(e) => setSearch(e.target.value)}></input>
            </form>

            <form>
                <input type='text' className="form-control searchBar" placeholder='按地块号搜索' onChange={(e) => setSearch(e.target.value)}></input>
            </form>
        </div>
        {isLoading ?
            <div className='circle'>
                <Box sx={{ display: 'flex' }}>
                    <CircularProgress />
                    <h1>Loading</h1>
                </Box>
            </div> :

            <div>
                <TableContainer component={Paper}>
                    <Table sx={{ minWidth: 650 }} aria-label="简单表格">
                        <TableHead>
                            <TableRow>
                                <TableCell align="left">区域</TableCell>
                                <TableCell align="left">地块号</TableCell>
                                <TableCell align="left">街道</TableCell>
                                <TableCell align="left">大小</TableCell>
                                <TableCell align="left">姓名</TableCell>
                                <TableCell align="left">联系电话</TableCell>
                                <TableCell align="left">地址</TableCell>
                                <TableCell align="left">操作</TableCell>
                            </TableRow>
                        </TableHead>
                        <TableBody>
                            {properties.filter((row) => {
                                return search.toLowerCase() === '' ? row : row.Name.toLowerCase().includes(search)
                            }).map((row) => (
                                <TableRow
                                    key={row._id}
                                    sx={{ '&:last-child td, &:last-child th': { border: 0 } }}
                                >
                                    <TableCell align="left">{row.Sector}</TableCell>
                                    <TableCell align="left">{row["Plot No"]}</TableCell>
                                    <TableCell align="left">{row.Street}</TableCell>
                                    <TableCell align="left">{row.Size}</TableCell>
                                    <TableCell align="left">{row.Name}</TableCell>
                                    <TableCell align="left">{row["Contact No"]}</TableCell>
                                    <TableCell align="left">{row.Address}</TableCell>
                                    <TableCell align="left" >
                                        <Link to={`/property/${row._id}`}>
                                            <Button variant='contained'>详情</Button>
                                        </Link>
                                    </TableCell>
                                </TableRow>
                            ))}
                        </TableBody>
                    </Table>
                </TableContainer>
            </div>}
    </>
)}
英文:

In below react component return statement I have two search boxes. Search by name is working fine but how can i add another search criteria to search by Plot No.
Is it possible to add another turnery operator in same filter statement? or do I need to write separate statement for Plot No column?

**return (
&lt;&gt;
&lt;div class=&quot;input-group mb-3&quot;&gt;
&lt;form&gt;
&lt;input type=&#39;text&#39; className=&quot;form-control searchBar&quot; placeholder=&#39;Search by Name&#39; onChange={(e)=&gt; setSearch(e.target.value)}&gt;&lt;/input&gt;
&lt;/form&gt;
&lt;form&gt;
&lt;input type=&#39;text&#39; className=&quot;form-control searchBar&quot; placeholder=&#39;Search by Plot No&#39; onChange={(e)=&gt; setSearch(e.target.value)}&gt;&lt;/input&gt;
&lt;/form&gt;
&lt;/div&gt;
{isLoading ? 
&lt;div className=&#39;circle&#39;&gt;
&lt;Box sx={{ display: &#39;flex&#39; }}&gt;
&lt;CircularProgress /&gt;
&lt;h1&gt;Loading&lt;/h1&gt;
&lt;/Box&gt;
&lt;/div&gt;:
&lt;div&gt;
&lt;TableContainer component={Paper}&gt;
&lt;Table sx={{ minWidth: 650 }} aria-label=&quot;simple table&quot;&gt;
&lt;TableHead&gt;
&lt;TableRow&gt;
&lt;TableCell align=&quot;left&quot;&gt;Sector&lt;/TableCell&gt;
&lt;TableCell align=&quot;left&quot;&gt;Plot No&lt;/TableCell&gt;
&lt;TableCell align=&quot;left&quot;&gt;Street&lt;/TableCell&gt;
&lt;TableCell align=&quot;left&quot;&gt;Size&lt;/TableCell&gt;
&lt;TableCell align=&quot;left&quot;&gt;Name&lt;/TableCell&gt;
&lt;TableCell align=&quot;left&quot;&gt;Contact No&lt;/TableCell&gt;
&lt;TableCell align=&quot;left&quot;&gt;Address&lt;/TableCell&gt;
&lt;TableCell align=&quot;left&quot;&gt;Action&lt;/TableCell&gt;
&lt;/TableRow&gt;
&lt;/TableHead&gt;
&lt;TableBody&gt;
{properties.filter((row) =&gt;{
return search.toLowerCase() === &#39;&#39; ? row : row.Name.toLowerCase().includes(search)}).map((row) =&gt; (
&lt;TableRow
key={row._id
}
sx={{ &#39;&amp;:last-child td, &amp;:last-child th&#39;: { border: 0 } }}
&gt; 
&lt;TableCell align=&quot;left&quot;&gt;{row.Sector}&lt;/TableCell&gt;
&lt;TableCell align=&quot;left&quot;&gt;{row[&quot;Plot No&quot;]}&lt;/TableCell&gt;
&lt;TableCell align=&quot;left&quot;&gt;{row.Street}&lt;/TableCell&gt;
&lt;TableCell align=&quot;left&quot;&gt;{row.Size}&lt;/TableCell&gt;
&lt;TableCell align=&quot;left&quot;&gt;{row.Name}&lt;/TableCell&gt;
&lt;TableCell align=&quot;left&quot;&gt;{row[&quot;Contact No&quot;]}&lt;/TableCell&gt;
&lt;TableCell align=&quot;left&quot;&gt;{row.Address}&lt;/TableCell&gt;
&lt;TableCell align=&quot;left&quot; &gt;
&lt;Link to ={`/property/${row._id}` }&gt;
&lt;Button variant=&#39;contained&#39;&gt;Details&lt;/Button&gt;
&lt;/Link&gt;
&lt;/TableCell&gt;             
&lt;/TableRow&gt;            
))}
&lt;/TableBody&gt;
&lt;/Table&gt;
&lt;/TableContainer&gt;
&lt;/div&gt;}
&lt;/&gt;
)}**

答案1

得分: 0

你可以修改筛选函数以通过姓名和地块号进行搜索:

return (
    <>
        <div className="input-group mb-3">
            <form>
                <input
                    type='text'
                    className="form-control searchBar"
                    placeholder='按姓名搜索'
                    onChange={(e) => setSearch(e.target.value)}
                />
            </form>

            <form>
                <input
                    type='text'
                    className="form-control searchBar"
                    placeholder='按地块号搜索'
                    onChange={(e) => setSearchPlotNo(e.target.value)} {/* 需要为地块号搜索创建新状态 */}
                />
            </form>
        </div>

        {isLoading ? (
            <div className='circle'>
                <Box sx={{ display: 'flex' }}>
                    <CircularProgress />
                    <h1>Loading</h1>
                </Box>
            </div>
        ) : (
            <div>
                <TableContainer component={Paper}>
                    <Table sx={{ minWidth: 650 }} aria-label="简单表格">
                        <TableHead>
                            <TableRow>
                                <TableCell align="left">区域</TableCell>
                                <TableCell align="left">地块号</TableCell>
                                <TableCell align="left">街道</TableCell>
                                <TableCell align="left">大小</TableCell>
                                <TableCell align="left">姓名</TableCell>
                                <TableCell align="left">联系方式</TableCell>
                                <TableCell align="left">地址</TableCell>
                                <TableCell align="left">操作</TableCell>
                            </TableRow>
                        </TableHead>
                        <TableBody>
                            {properties
                                .filter((row) =>
                                    search.toLowerCase() === '' ||
                                    row.Name.toLowerCase().includes(search) ||
                                    row["Plot No"].toLowerCase().includes(searchPlotNo) // 使用新的搜索状态
                                )
                                .map((row) => (
                                    <TableRow
                                        key={row._id}
                                        sx={{ '&:last-child td, &:last-child th': { border: 0 } }}
                                    >
                                        <TableCell align="left">{row.Sector}</TableCell>
                                        <TableCell align="left">{row["Plot No"]}</TableCell>
                                        <TableCell align="left">{row.Street}</TableCell>
                                        <TableCell align="left">{row.Size}</TableCell>
                                        <TableCell align="left">{row.Name}</TableCell>
                                        <TableCell align="left">{row["Contact No"]}</TableCell>
                                        <TableCell align="left">{row.Address}</TableCell>
                                        <TableCell align="left">
                                            <Link to={`/property/${row._id}`}>
                                                <Button variant='contained'>详情</Button>
                                            </Link>
                                        </TableCell>
                                    </TableRow>
                                ))}
                        </TableBody>
                    </Table>
                </TableContainer>
            </div>
        )}
    </>
)}
英文:

You can modify the filter function to search by both Name and Plot No:

return (
&lt;&gt;
&lt;div className=&quot;input-group mb-3&quot;&gt;
&lt;form&gt;
&lt;input
type=&#39;text&#39;
className=&quot;form-control searchBar&quot;
placeholder=&#39;Search by Name&#39;
onChange={(e) =&gt; setSearch(e.target.value)}
/&gt;
&lt;/form&gt;
&lt;form&gt;
&lt;input
type=&#39;text&#39;
className=&quot;form-control searchBar&quot;
placeholder=&#39;Search by Plot No&#39;
onChange={(e) =&gt; setSearchPlotNo(e.target.value)} {/* Cần tạo state mới cho việc t&#236;m kiếm theo Plot No */}
/&gt;
&lt;/form&gt;
&lt;/div&gt;
{isLoading ? (
&lt;div className=&#39;circle&#39;&gt;
&lt;Box sx={{ display: &#39;flex&#39; }}&gt;
&lt;CircularProgress /&gt;
&lt;h1&gt;Loading&lt;/h1&gt;
&lt;/Box&gt;
&lt;/div&gt;
) : (
&lt;div&gt;
&lt;TableContainer component={Paper}&gt;
&lt;Table sx={{ minWidth: 650 }} aria-label=&quot;simple table&quot;&gt;
&lt;TableHead&gt;
&lt;TableRow&gt;
&lt;TableCell align=&quot;left&quot;&gt;Sector&lt;/TableCell&gt;
&lt;TableCell align=&quot;left&quot;&gt;Plot No&lt;/TableCell&gt;
&lt;TableCell align=&quot;left&quot;&gt;Street&lt;/TableCell&gt;
&lt;TableCell align=&quot;left&quot;&gt;Size&lt;/TableCell&gt;
&lt;TableCell align=&quot;left&quot;&gt;Name&lt;/TableCell&gt;
&lt;TableCell align=&quot;left&quot;&gt;Contact No&lt;/TableCell&gt;
&lt;TableCell align=&quot;left&quot;&gt;Address&lt;/TableCell&gt;
&lt;TableCell align=&quot;left&quot;&gt;Action&lt;/TableCell&gt;
&lt;/TableRow&gt;
&lt;/TableHead&gt;
&lt;TableBody&gt;
{properties
.filter((row) =&gt;
search.toLowerCase() === &#39;&#39; ||
row.Name.toLowerCase().includes(search) ||
row[&quot;Plot No&quot;].toLowerCase().includes(searchPlotNo) // Sử dụng state t&#236;m kiếm mới
)
.map((row) =&gt; (
&lt;TableRow
key={row._id}
sx={{ &#39;&amp;:last-child td, &amp;:last-child th&#39;: { border: 0 } }}
&gt;
&lt;TableCell align=&quot;left&quot;&gt;{row.Sector}&lt;/TableCell&gt;
&lt;TableCell align=&quot;left&quot;&gt;{row[&quot;Plot No&quot;]}&lt;/TableCell&gt;
&lt;TableCell align=&quot;left&quot;&gt;{row.Street}&lt;/TableCell&gt;
&lt;TableCell align=&quot;left&quot;&gt;{row.Size}&lt;/TableCell&gt;
&lt;TableCell align=&quot;left&quot;&gt;{row.Name}&lt;/TableCell&gt;
&lt;TableCell align=&quot;left&quot;&gt;{row[&quot;Contact No&quot;]}&lt;/TableCell&gt;
&lt;TableCell align=&quot;left&quot;&gt;{row.Address}&lt;/TableCell&gt;
&lt;TableCell align=&quot;left&quot;&gt;
&lt;Link to={`/property/${row._id}`}&gt;
&lt;Button variant=&#39;contained&#39;&gt;Details&lt;/Button&gt;
&lt;/Link&gt;
&lt;/TableCell&gt;
&lt;/TableRow&gt;
))}
&lt;/TableBody&gt;
&lt;/Table&gt;
&lt;/TableContainer&gt;
&lt;/div&gt;
)}
&lt;/&gt;
)}

答案2

得分: 0

我可以通过以下修改来解决这个问题。

{properties.filter((row) => {
    return search.toLowerCase() === '' ? row : row.Name.toLowerCase().includes(search) || row["Plot No"].includes(search) 
}).map((row) => (

但是每个文本框仍然会搜索整个表格,而我希望从筛选后的结果中过滤结果。

英文:

I am able to resolve this issue by below modification.

{properties.filter((row) =&gt;{
return search.toLowerCase() === &#39;&#39; ? row : row.Name.toLowerCase().includes(search) || row[&quot;Plot No&quot;].includes(search) }).map((row) =&gt; (

But still Each Text box searches entire table. while i want to filter results from filtered results.

huangapple
  • 本文由 发表于 2023年8月9日 12:04:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/76864492-2.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定