오늘은 React 5번째 시간입니다.
Today I Learned
- 검색 기능
- 페이저 기능
constructor(props){
super(props);
this.state={
list:[
{"id":92032472,"title":"eff","writerId":"newlec","content":"af"}
],
count:0
};
this.queryInput = React.createRef();
this.fieldInput = React.createRef();
this.page=1;
this.field = "title";
this.query = "";
}
dataBind(){
fetch(`/api/notice/list?p=${this.page}&f=${this.field}&q=${this.query}`)
.then((response)=>{
return response.json();
})
.then((data)=>{
this.setState(data);
});
}
<form className="table-form">
<fieldset>
<legend className="hidden">공지사항 검색 필드</legend>
<label className="hidden">검색분류</label>
<select ref={this.fieldInput} name="f">
<option value="title">제목</option>
<option value="writerId">작성자</option>
</select>
<label className="hidden">검색어</label>
<input type="text" name="q" ref={this.queryInput}/>
<input className="btn btn-search" type="submit" value="검색" onClick={(e)=>{this.search(e);}}/>
</fieldset>
</form>
search(e){
e.preventDefault();
this.query = this.queryInput.current.value;
this.field = this.fieldInput.current.value;
this.dataBind();
}
constructor(props){
super(props);
this.state={
list:[
{"id":92032472,"title":"eff","writerId":"newlec","content":"af"}
],
count:0
};
this.queryInput = React.createRef();
this.fieldInput = React.createRef();
this.page=1;
this.field = "title";
this.query = "";
let offset = (this.page -1)%5;
this.startNum = this.page-offset;
}
<ul className="-list- center" onClick={(e)=>{this.pageClickHandler(e);}}>
{
[0,1,2,3,4].map((i)=><li key={i+this.startNum}><a className="-text- " href="?p={i}&f=&q=">{i+this.startNum}</a></li>)
}
</ul>
<div>
{
this.startNum==1?
<span className="btn btn-prev" onClick={(e)=>{alert('이전 페이지가 없습니다.')}}>이전</span>
:<a className="btn btn-prev" href="?p=&f=&q=">이전</a>
}
</div>