javascript - ReactJS with Material-UI: How to sort an array of Material-UI's <TableRow> alphabetically? -


currently, render material-ui's <table>'s <tablerow> (http://www.material-ui.com/#/components/table) array of <tablerow>s , using .map(). each <tablerow> has <tablerowcolumn> represents first name, <tablerowcolumn>josh</tablerowcolumn>.

but, if user presses button, sort <tablerow> array alphabetically <tablerowcolumn>'s first name. example out of 10 <tablerow>s, if array[0] had first name conny , array[1] had adrian, array[1] become array[0].

what right approach this? guidance or insight appreciated it.

edit

each row rendered array rows, has objects properties firstname , favcolor:

        {           rows.map((row) => {             return(               <userrow                 firstname={row.firstname}                 favcolor={row.favcolor}               />             )           })         } 

and each row defined so:

const userrow = (props) => {   const {firstname, favcolor} = props    return (     <tablerow>       <tablerowcolumn>{firstname}</tablerowcolumn>       <tablerowcolumn>{favcolor}</tablerowcolumn>     </tablerow>   ) } 

i sort array before applying map operation create tablerows.

the react way of thinking declarative. means on visual level, should provide elements should displayed. hence sorted before passed view component.

for example (i couldn't use material-ui elements since example didn't run in stackoverflow setup. replace elements of tablecomponent material-ui alter ego.):

const data = [    {firstname: "john", lastname: "rover",  id:12},    {firstname: "bob",  lastname: "taylor", id:24},    {firstname: "lucy", lastname: "heart",  id:43}  ]    // table component unaware of data order operations  const tablecomponent = ({tabledata}) => <table><tbody>    {tabledata.map(d=> <tr key={d.id}>        <td>{d.firstname}</td>        <td>{d.lastname}</td>    </tr>)}  </tbody></table>    // parent component takes care of feeding table  // component data in correct order.    class app extends react.component {    state = { sortby: "firstname"}    handlechange = (event) => this.setstate(      {sortby: event.target.value}    );    render () {      const {data} = this.props;       const {sortby} = this.state;      const sorteddata = data.sort((a,b) => a[sortby]>b[sortby]?1:-1)      return <div>        sort          <select value={sortby} onchange={this.handlechange}>          <option value="firstname">first name</option>          <option value="lastname">last name</option>        </select>        <h2>the table: </h2>        <tablecomponent tabledata={sorteddata} />      </div>    }  }      reactdom.render(    <app data={data} />,    document.getelementbyid('root')  );
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>  <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>    <div id="root"></div>


Comments

Popular posts from this blog

sql server - Cannot query correctly (MSSQL - PHP - JSON) -

php - trouble displaying mysqli database results in correct order -

C++ Linked List -