项目作者: AsemAlalami

项目描述 :
react table (client and server-side) based on bootstrap.
高级语言: JavaScript
项目地址: git://github.com/AsemAlalami/react-strap-table.git
创建时间: 2018-08-09T07:22:12Z
项目社区:https://github.com/AsemAlalami/react-strap-table

开源协议:

下载


react-strap-table

react table (client and server side) based on bootstrap style.

You can customize any thing (headings, contents …) not only by text but you can putting your component or html tags,
and will find css classes for all headings and contents of columns,
and you can sorting, pagination, limit of per page and filtering of server-side data.

Installation

  1. npm install react-strap-table

components

  • ServerTable (server-side data)
    1. import ServerTable from 'react-strap-table';
    • ClientTable (client-side data):

      Coming soon…

      Demo (server-side)

  • Simple (Demo without options )
  • Advance

    Features

  • Customize headings of columns not only text but you can put your component or html tags.
  • Customize contents of columns (td cell) by any component and the row of data with passed for it.
  • Server-side sorting, pagination, limit of per page and filtering.
  • Customize response of your url request.
  • Every columns have css class for customize it table-{colmun}-th and table-{colmun}-td.
  • Customize texts and icons.

Documentation:

Example

  1. const url = 'https://react-strap-table.com/users';
  2. const columns = ['id', 'name', 'email', 'created_at'];
  3. const options = {
  4. headings: {id: '#', created_at: 'Created At'},
  5. sortable: ['name', 'email']
  6. };
  7. return (
  8. <ServerTable columns={columns}
  9. url={url}
  10. options={options}
  11. bordered hover></ServerTable>
  12. );

Props

  • url (required, server-side): base url
  • columns (required, array): contains names of columns, the headings of columns will be Upper case of first letter but you can overwrite it by headings in options props.
  • hover (boolean): bootstrap style,
  • bordered (boolean): bootstrap style,
  • condensed (boolean): bootstrap style,
  • striped (boolean): bootstrap style,
  • options (object): details.
  • perPage (boolean): show “PrePage” select input
  • search (boolean): show “search” input
  • pagination (boolean): show “pagination” section
  • updateUrl (boolean): if you want to update(replace) url page

    Children

    The children must be Function of two parameters (row, column), and implementation of function must be switch of case columns that you need to customize contents.
    The row parameter with return current row of data.
    1. <ServerTable columns={columns} url={url} bordered>
    2. {
    3. function (row, column) {
    4. switch (column) {
    5. case 'id':
    6. return (
    7. <input key={row.id}
    8. type="checkbox" value={row.id}
    9. onChange={self.handleCheckboxTableChange} />
    10. );
    11. case 'avatar':
    12. return (<img src={row.avatar} className="table-image"/>);
    13. default:
    14. return (row[column]);
    15. }
    16. }
    17. }
    18. </ServerTable>

    Note: You can get the index of the current row using row.index

    Don’t forget default case for other columns.

    Options

    Option|Description | Default
    ———|———————————|———
    headings|object of Headings columns {id:'#', created_at: 'Created At'}|{}
    sortable|array of columns that can sorted ['name', 'email']|[]
    columnsWidth|object of width columns by percentage(%) and you can fix it by pixel(px){id: 5, name:30, created_at:'30px'}|{}
    columnsAlign|object of align heading columns (not td) {id: 'center'}|{}
    perPage|integer limit rows of page|10
    perPageValues|array contain values of per page select|[10, 20, 25, 100]
    icons|object contains icons in table|{sortBase: 'fa fa-sort',sortUp: 'fa fa-sort-amount-up',sortDown: 'fa fa-sort-amount-down',search: 'fa fa-search'}
    texts|object contains texts in table|{show: 'Show', entries: 'entries', showing: 'Showing', to: 'to', of: 'of', search: 'Search'}
    requestParametersNames|object contains names of parameters request|{query: 'query',limit: 'limit',page: 'page',orderBy: 'orderBy',direction: 'direction'}
    orderDirectionValues|object contains names of order direction|{ascending: 'asc',descending: 'desc'}
    loading|text\|html tag\|component for loading|(<div style={{fontSize: 18, display: "initial"}}><span className="fa fa-spinner fa-spin"></span> Loading...</div>)
    responseAdapter (server-side)|function if you want to mapping response. function take parameter of data response and must return object contains data and total properties.|function (resp_data) {return {data: resp_data.data, total: resp_data.total}}

Options Examples

  1. let checkAllInput = (<input type="checkbox" ref={this.check_all}
  2. onChange={this.handleCheckboxTableAllChange}/>);
  3. const options = {
  4. perPage: 5,
  5. headings: {id: checkAllInput, created_at: 'Created At'},
  6. sortable: ['name', 'email', 'created_at'],
  7. columnsWidth: {name: 30, email: 30, id: 5},
  8. columnsAlign: {id: 'center', avatar: 'center'},
  9. requestParametersNames: {query: 'search', direction: 'order'},
  10. responseAdapter: function (resp_data) {
  11. return {data: resp_data.data, total: resp_data.meta.total}
  12. },
  13. texts: {
  14. show: 'عرض'
  15. },
  16. icons: {
  17. sortUp: 'fa fa-sort-up',
  18. sortDown: 'fa fa-sort-down'
  19. }
  20. };

Request parameters (server-side)

Get Request with following parameters:

  • query: search input value.
  • limit: rows per page.
  • page: current page.
  • orderBy: column to sort.
  • direction: direction order? asc or desc.

    You can rename the names by using requestParametersNames property in options prop

    You can change the direction order values by using orderDirectionValues property in options prop

Css Classes

The component based on card bootstrap component.

  • react-strap-table: root class of component.
  • card-header: contains per page and search input.
  • card-body: contains the table.
  • card-footer: contains pagination and data info.
  • table-{column-name}-th: table column header for every columns.
  • table-{column-name}-td: table column content for every columns.
  • table-sort-icon: span icon of table column header for columns that be sortable.

Refresh Data

You can refresh data in table by using refreshData function

  1. class YouComponent extends Component {
  2. constructor(props) {
  3. ...
  4. this.serverTable = React.createRef();
  5. }
  6. refreshTableData() {
  7. ...
  8. this.serverTable.current.refreshData();
  9. ...
  10. }
  11. render() {
  12. return (
  13. ...
  14. <ServerTable ref={this.serverTable}></ServerTable>
  15. ...
  16. );
  17. }
  18. }