i am trying to do a serach using input fild and find the names based on firstname but i dont know howt to do that i have created a codesandbox for my code also that i will give at the end pls help me i am new to react js
{this.state.tablist == "doctor" ? (
<div
className="chat-avatar gradiantblur"
onClick={() => this.livechat(this.state.list.id)}
>
<div>
<img src={img} className="avatar" />
</div>
<div className="alignstart">
{this.state.list.firstname} {this.state.list.lastname}
<p className="margin-0">
{" "}
{this.state.list.speciality}
</p>
<p className="margin-0">
{" "}
✓ {this.state.list.speciality}
</p>
</div>
<div>152:11</div>
</div>
) : this.state.tablist == "consultant" ? (
<>
<div className="scrollerchatlist">
{console.log(this.state.consulatnt)}
{this.state.consulatnt.map((data) => (
<div
className="chat-avatar gradiantblur"
onClick={() => this.livechat(data.id)}
>
<div>
<img src={img} className="avatar" />
</div>
<div className="alignstart">
{data.firstname} {data.lastname}
<p className="margin-0"> {data.speciality}</p>
<p className="margin-0">
{" "}
✓ {data.firstname}
</p>
</div>
<div>152:11</div>
</div>
))}
</div>
</>
) : this.state.tablist == "sales" ? (
<>
{" "}
<div className="scrollerchatlist">
{this.state.sales.map((data) => (
<div
className="chat-avatar gradiantblur"
onClick={() => this.livechat(data.id)}
>
<div>
<img src={img} className="avatar" />
</div>
<div className="alignstart">
{data.firstname}
{/* <p className="margin-0"> {data.firstname}</p> */}
<p className="margin-0">
{" "}
{/* ✓ {data.firstname} */}
</p>
</div>
<div>152:11</div>
</div>
))}
</div>
</>
) : null}
<input
className="w-100 msserserch gradiantblur"
placeholder="seach messages"
onChange={(e) =>
this.setState({ serchinput: e.target.value })
}
type="Search"
/>
the codesandbox i have created is given below pls refer what i am trying to implement i would be very help full if u can edit my code and give an working aswer in codesandbox itself
https://codesandbox.io/s/intelligent-nova-v7pb1?file=/src/App.js
CodePudding user response:
You could apply a filter
function before displaying the data:
Consultant
{this.state.consulatnt
.filter((data) =>
data.firstname
.toLowerCase()
.includes(this.state.serchinput.toLowerCase())
)
.map((data) => (
...
))}
Sales
{this.state.sales.filter((data) =>
data.firstname
.toLowerCase()
.includes(this.state.serchinput.toLowerCase())
).map((data) => (
...
))}
Here is a working sandbox with filter working on the consultant and sales lists.
CodePudding user response:
I hope this this example help you out. I have applied to Consultant
{this.state.serchinput ? this.state.consulatnt.filter(item => item.firstname === this.state.serchinput).map((data) => (
<div className="chat-avatar gradiantblur" onClick={() => this.livechat(data.id)}>
...
</div>
)): this.state.consulatnt.map((data) => (
<div className="chat-avatar gradiantblur" onClick={() => this.livechat(data.id)}>
...
</div>
))}
Here is working demo https://codesandbox.io/s/filter-8i7zf