score:1
Accepted answer
Here you go the working code snippet, i made as less as possible code modification to make the search and the sort working together at your App.js
.
import React from 'react';
import InquiresList from './InquiryList.json';
class Inquiry extends React.Component {
constructor(props) {
super(props);
this.state = {
searchInquiries: null,
answerStatus: 'all',
data: InquiresList,
};
}
handleSearch = (event) => {
this.setState({ searchInquiries: event.target.value });
};
handleAnswer = (event) => {
this.setState({ answerStatus: event.target.value });
};
render() {
const padding = {
padding: '20px',
};
const marginBottom = {
marginBottom: '0px',
};
const row = {
margin: '0px',
};
let inquiries = InquiresList.filter((data) => {
if (this.state.searchInquiries !== null) {
return data.user_code
.toLowerCase()
.includes(this.state.searchInquiries.toLowerCase());
}
return data;
});
if (this.state.answerStatus === 'answered') {
inquiries.sort((a, b) => {
if (a.answered < b.answered) return -1;
if (a.answered > b.answered) return 1;
return 0;
});
} else if (this.state.answerStatus === 'unanswered') {
inquiries.sort((a, b) => {
if (a.answered > b.answered) return -1;
if (a.answered < b.answered) return 1;
return 0;
});
}
return (
<div className='col-md-12 bg-gray' style={{ padding: '30px' }}>
<div className='row' style={row}>
<h3 className='roboto paragraph mgb-30'>Inquiries List</h3>
</div>
<div className='row border-radius-10 default-shadow' style={row}>
<div className='col-md-12 bg-white border-radius-10' style={padding}>
<div className='row'>
<div className='col-md-6 flex all-center'>
<i className='i fa fa-search table-search-icon' />
<input
type='text'
onChange={this.handleSearch}
className='form-control'
style={{ borderRadius: '30px' }}
placeholder='Search'
/>
</div>
<div className='col-md-6 flex flex-end' style={row}>
<select
name='inquiry-filter'
onChange={this.handleAnswer}
className='inquiry-filter'
>
<option value='all'>All</option>
<option value='answered'>Answered</option>
<option value='unanswered'>Unanswered</option>
</select>
</div>
</div>
</div>
<div className='col-md-12 bg-white' style={{ padding: '0px' }}>
<table className='table table-striped table-hover table-bordered'>
<thead>
<tr>
<td>
<p className='paragraph' style={marginBottom}>
Created
</p>
</td>
<td>
<p className='paragraph' style={marginBottom}>
User Code
</p>
</td>
<td>
<p className='paragraph' style={marginBottom}>
Email
</p>
</td>
<td>
<p className='paragraph' style={marginBottom}>
Subject
</p>
</td>
<td>
<p className='paragraph' style={marginBottom}>
Answer
</p>
</td>
</tr>
</thead>
<tbody>
{inquiries.map((inquiry) => (
<tr>
<td>
<h3 className='paragraph'>{inquiry.created_date}</h3>
</td>
<td>
<h3 className='paragraph' style={marginBottom}>
{inquiry.user_code}
</h3>
</td>
<td>
<p className='paragraph' style={marginBottom}>
{inquiry.email}
</p>
</td>
<td>
<p className='paragraph' style={marginBottom}>
{inquiry.subject}
</p>
</td>
<td>
<div className='custom-control custom-checkbox'>
{inquiry.answered ? (
<input
type='checkbox'
checked
className='custom-control-input'
id={'answer-status-' + inquiry.id}
name={'answer-status-' + inquiry.id}
/>
) : (
<input
type='checkbox'
className='custom-control-input'
id={'answer-status-' + inquiry.id}
name={'answer-status-' + inquiry.id}
/>
)}
<label
className='custom-control-label'
for={'answer-status-' + inquiry.id}
/>
</div>
</td>
</tr>
))}
</tbody>
</table>
</div>
<div className='col-md-12 bg-white flex flex-end' style={padding}>
<ul
className='list list-inline pagination-list'
style={marginBottom}
>
<li className='list-inline-item'>
<button
type='button'
className='color-golden bg-white'
style={{
border: 'none',
boxShadow: '0px 0px 0px #ffff',
borderRight: '1px solid #d3d3d3',
}}
>
Previous
</button>
</li>
<li className='list-inline-item active bg-golden number-container'>
1
</li>
<li className='list-inline-item number-container'>2</li>
<li className='list-inline-item number-container'>3</li>
<li className='list-inline-item number-container'>4</li>
<li className='list-inline-item'>
<button
type='button'
className='color-golden bg-white'
style={{
border: 'none',
boxShadow: '0px 0px 0px #ffff',
borderLeft: '1px solid #d3d3d3',
}}
>
Next
</button>
</li>
</ul>
</div>
</div>
</div>
);
}
}
export default Inquiry;
score:0
you can try in this way.
import InquiresList from '../dashboard/inquiries/InquiriesList.json';
export default class index extends Component {
constructor(props) {
super(props);
this.state = {
searchInquiries: null,
answerStatus: "all",
inquiries: InquiresList,
copyInquiries: InquiresList,
};
}
onSearchHandler = (value) => {
const searchResult = this.state.copyInquiries
.filter(function (item) {
const itemData = item.user_code.toLowerCase();
const textData = value.toLowerCase();
return itemData.indexOf(textData) > -1;
})
.sort((a, b) => a.answerStatus - b.answerStatus);
this.setState({ searchInquiries: value, inquiries: searchResult });
};
render() {
return (
<div className="col-md-12 bg-gray" style={{ padding: "30px" }}>
<div className="row" style={row}>
<h3 className="roboto paragraph mgb-30">Inquiries List</h3>
</div>
<div className="row border-radius-10 default-shadow" style={row}>
<div className="col-md-12 bg-white border-radius-10" style={padding}>
<div className="row">
<div className="col-md-6 flex all-center">
<i className="i fa fa-search table-search-icon"></i>
<input
type="text"
onChange={this.handleSearch}
className="form-control"
style={{ borderRadius: "30px" }}
placeholder="Search"
/>
</div>
<div className="col-md-6 flex flex-end" style={row}>
<select
name="inquiry-filter"
onChange={this.handleAnswer}
className="inquiry-filter"
>
<option value="all">All</option>
<option value="answered">Answered</option>
<option value="unanswered">Unanswered</option>
</select>
</div>
</div>
</div>
<div className="col-md-12 bg-white" style={{ padding: "0px" }}>
<table className="table table-striped table-hover table-bordered">
<thead>
<tr>
<td>
<p className="paragraph" style={marginBottom}>
Created
</p>
</td>
<td>
<p className="paragraph" style={marginBottom}>
User Code
</p>
</td>
<td>
<p className="paragraph" style={marginBottom}>
Email
</p>
</td>
<td>
<p className="paragraph" style={marginBottom}>
Subject
</p>
</td>
<td>
<p className="paragraph" style={marginBottom}>
Answer
</p>
</td>
</tr>
</thead>
<tbody>
{inquiries.map((inquiry) => (
<tr>
<td>
<h3 className="paragraph">{inquiry.created_date}</h3>
</td>
<td>
<h3 className="paragraph" style={marginBottom}>
{inquiry.user_code}
</h3>
</td>
<td>
<p className="paragraph" style={marginBottom}>
{inquiry.email}
</p>
</td>
<td>
<p className="paragraph" style={marginBottom}>
{inquiry.subject}
</p>
</td>
<td>
<div className="custom-control custom-checkbox">
{inquiry.answered ? (
<input
type="checkbox"
checked
className="custom-control-input"
id={"answer-status-" + inquiry.id}
name={"answer-status-" + inquiry.id}
/>
) : (
<input
type="checkbox"
className="custom-control-input"
id={"answer-status-" + inquiry.id}
name={"answer-status-" + inquiry.id}
/>
)}
<label
className="custom-control-label"
for={"answer-status-" + inquiry.id}
></label>
</div>
</td>
</tr>
))}
</tbody>
</table>
</div>
</div>
</div>
);
}
}
Source: stackoverflow.com
Related Query
- Searching and sorting in react table
- Handle Pagination Sorting and Filtering Separately in Antd Table React
- Sorting React table by clicking table headers for columns with numerical and string values
- Separate filter and sorting button for react table
- React table with sorting and pagination doesn't update
- React antd nested table and drag & drop sorting
- How do I wrap a React component that returns multiple table rows and avoid the "<tr> cannot appear as a child of <div>" error?
- How to make some columns align left and some column align center in React Table - React
- How to use drag and drop functionality both columns and rows in my React Table - ReactJS
- react redux architecting table actions and reducers
- React table dynamic page size but with size limit and pagination
- Dynamic column and values in react table
- TypeError: Object(...) is not a function with React Table and moment.js
- React Table not sorting numbers correctly
- How to make the API call and display it in React JS using React Table and Axios?
- How to edit react bootstrap table and save data after editing
- React hooks useState/setState not working after sorting the array and passing it
- How to add sorting to table with react virtualized?
- Removing a header name in react table and making it empty
- Filtering and sorting React
- Sorting and Filtering React - Context
- React Table using hooks expand and collapse rows
- Auto expandable rows and subrows react table using hooks
- Semantic UI React - Table - Make whole row selectable and link somewhere
- How to change default sorting icon on table header column on React Material-Table?
- Creating a table that can drag and drop both columns and rows on React
- React Convert Div and Table Component Into PDF With Multiple Pages and CSS
- How to make react data grid table cell editable and focused on click of a button without performing a double click action
- React rendering html table and reading from html table
- How to fix my React Context to render useEffect and reload table data
More Query from same tag
- Difference between testSaga and expectSaga in redux-saga-test-plan
- Capture div containing inline svg and download it as image
- Passing a filtered list to a sibling using a React.Component style
- .this reference error in Reactjs
- React Next.js: getServerProps is not returning array from prisma
- reactjs error : If you meant to render a collection of children,use an array instead
- How to send base64 pdf code as attachment through nodemailer/mailgun in nodejs?
- Another es2016 estrange code that runs
- why getting 'WSGIRequest' object has no attribute 'data' error?
- Adding CSS file in React
- How can I have my content fill in the remainder of the page when I'm using Material UI AppBar?
- ReactJS: Logged in with Google, but callback not coming to application
- Why do we use ES6 modules for import in frontend frameworks instead of CommonJS
- Each child in an array or iterator should have a unique "key" prop in reactjs
- Custom navigation with Swiper React and typescript
- Fetch api response in chrome and firefox is different
- dynamic Progress bar in React
- Get the height of a Component in React
- Material-UI : DataGrid/XGrid How to detect density change?
- Contentful with react expected parameter accessToken error
- useState not working when receiving new values from Redux Store in Function Component
- Disable multiple column filter in Ag Grid
- Webpack 3 image src issue for React ( html-Loader & transform-react-jsx-img-import )
- React Promise will not return a value
- How do i filter a value of key in an object based on matching value?
- Open a modal (or dialogue) from a dropdown asking the user if they would like to take an action
- Remove the selected seats when the function is called ReactJs
- How to display text on both axis in react chart?
- Can I add a key prop to a React fragment?
- How can i solve FirebaseError: Expected first argument to collection() to be a CollectionReference, a DocumentReference or FirebaseFirestore