亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

在自定義表中搜索 - React

在自定義表中搜索 - React

慕碼人2483693 2023-11-02 16:57:57
我正在嘗試制作一個包含 api 數據可搜索的表。我正在路上,但不確定如何進一步進行。代碼如下所示:const [searchTerm, setSearchTerm] = useState("");    const [searchResults, setSearchResults] = useState([]);        const handleChange = event => {        setSearchTerm(event.target.value);    };    useEffect(() => {        const results = apiData.filter(person =>             person.toLowerCase().includes(searchTerm)        );        setSearchResults(results);    }, [searchTerm]);    const renderPerson = (contact, index) => {        return (            <tr key={index}>                <td>{contact.ID}</td>                <td>{contact.Info.Name}</td>                <td>{contact.InfoID}</td>                <td>{contact.Info.DefaultEmail.EmailAddress}</td>                <td>{contact.Info.DefaultPhone.Number}</td>                <td>{contact.Info.InvoiceAddress.AddressLine1}</td>            </tr>        )    } return (        <Fragment>            <input type="text" value={searchTerm} onChange={handleChange} placeholder="S?k.."></input>            <table id="myTable">                <thead>                    <tr className="header">                        <th>ID</th>                        <th>Navn</th>                        <th>Info Id</th>                        <th>E-post</th>                        <th>Telefon</th>                        <th>Adresse</th>                    </tr>                </thead>                <tbody>                    {apiData.map(renderPerson)}                </tbody>            </table>        </Fragment>    )https://dev.to/asimdahall/simple-search-form-in-react-using-hooks-42pg 我已經遵循了本指南,但是由于我有 renderPerson 函數,我有點不確定如何處理這。問題:有什么方法可以讓它發揮作用,還是我的方法不對?我知道我需要以某種方式將 searchResult 放入 tbody 中,但隨后表將不會被填充。
查看完整描述

2 回答

?
白衣染霜花

TA貢獻1796條經驗 獲得超10個贊

更改apiData為searchResults渲染時


<tbody>

   {searchResults.map(renderPerson)}

</tbody>

更改您的過濾方式結果(已更新)


const results = apiData.filter(person => 

   person.Info.Name.toLowerCase().includes(searchTerm)

);


....


const renderPerson = (item, index) => {

        return (

            <tr key={index}>

                <td>{item.ID}</td>

                <td>{item.Info.Name}</td>

                <td>{item.InfoID}</td>

                <td>{item.Info.DefaultEmail.EmailAddress}</td>

                <td>{item.Info.DefaultPhone.Number}</td>

                <td>{item.Info.InvoiceAddress.AddressLine1}</td>

            </tr>

        )

    }


查看完整回答
反對 回復 2023-11-02
?
慕斯709654

TA貢獻1840條經驗 獲得超5個贊

嘗試這個


export default function () {

 

const [searchTerm, setSearchTerm] = useState("");

const [searchResults, setSearchResults] = useState([]);


const apiData=[

        {

            ID:'1222',

            Info:{

                ID:'1222',

                EmailAddress:'[email protected]',

                Number:'2222222222',

                AddressLine1:'test'

            }

        },

        {

            ID:'2333',

            Info:{

                ID:'2333',

                EmailAddress:'[email protected]',

                Number:'1111111111',

                AddressLine1:'test2'

            }

        }

    ]

    

    const handleChange = event => {

        setSearchTerm(event.target.value);

        if(event.target.value){

            const results = apiData.filter(person => 

                person.ID.toLowerCase().includes(event.target.value)

            );

            setSearchResults(results);

        }else{

            setSearchResults([]);

        }

        

    };



    const renderPerson = (contact, index) => {

        return (

            <tr key={index}>

                <td>{contact.ID}</td>

                <td>{contact.Info.Name}</td>

                <td>{contact.Info.ID}</td>

                <td>{contact.Info.EmailAddress}</td>

                <td>{contact.Info.Number}</td>

                <td>{contact.Info.AddressLine1}</td>

            </tr>

        )

    }


 return (

        <Fragment>

            <input type="text" value={searchTerm} onChange={handleChange} placeholder="S?k.."></input>

            <table id="myTable">

                <thead>

                    <tr className="header">

                        <th>ID</th>

                        <th>Navn</th>

                        <th>Info Id</th>

                        <th>E-post</th>

                        <th>Telefon</th>

                        <th>Adresse</th>

                    </tr>

                </thead>

                <tbody>

                    {searchResults.map(renderPerson)}

                </tbody>

            </table>

        </Fragment>

    )


 }


查看完整回答
反對 回復 2023-11-02
  • 2 回答
  • 0 關注
  • 144 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號