|
1 |
| -# react-datatable |
| 1 | +# React Datatable |
| 2 | + |
2 | 3 | ReactDatatable is a component which provide ability to create multifunctional table using single component like jQuery Datatable. It's fully customizable and easy to integrate in any react component. Bootstrap compatible.
|
| 4 | + |
| 5 | +## Features |
| 6 | +* Lightweight |
| 7 | +* Fully customizable (JSX, templates, state, styles, callbacks) |
| 8 | +* Client-side & pagination |
| 9 | +* Multi-sort |
| 10 | +* Filters |
| 11 | +* Minimal design |
| 12 | +* Fully controllable via optional props and callbacks |
| 13 | + |
| 14 | +## Usage |
| 15 | + |
| 16 | +```js |
| 17 | +import React, { Component, Fragment } from 'react'; |
| 18 | +import { render} from 'react-dom'; |
| 19 | +import ReactDatatable from '@ashvin27/react-datatable'; |
| 20 | + |
| 21 | +class App extends Component { |
| 22 | + constructor(props) { |
| 23 | + super(props); |
| 24 | + this.columns = [ |
| 25 | + { |
| 26 | + key: "name", |
| 27 | + text: "Name", |
| 28 | + className: "name", |
| 29 | + align: "left", |
| 30 | + sortable: true, |
| 31 | + }, |
| 32 | + { |
| 33 | + key: "address", |
| 34 | + text: "Address", |
| 35 | + className: "address", |
| 36 | + align: "left", |
| 37 | + sortable: true |
| 38 | + }, |
| 39 | + { |
| 40 | + key: "postcode", |
| 41 | + text: "Postcode", |
| 42 | + className: "postcode", |
| 43 | + sortable: true |
| 44 | + }, |
| 45 | + { |
| 46 | + key: "rating", |
| 47 | + text: "Rating", |
| 48 | + className: "rating", |
| 49 | + align: "left", |
| 50 | + sortable: true |
| 51 | + }, |
| 52 | + { |
| 53 | + key: "type_of_food", |
| 54 | + text: "Type of Food", |
| 55 | + className: "type_of_food", |
| 56 | + sortable: true, |
| 57 | + align: "left" |
| 58 | + }, |
| 59 | + { |
| 60 | + key: "action", |
| 61 | + text: "Action", |
| 62 | + className: "action", |
| 63 | + width: 100, |
| 64 | + align: "left", |
| 65 | + sortable: false, |
| 66 | + cell: record => { |
| 67 | + return ( |
| 68 | + <Fragment> |
| 69 | + <button |
| 70 | + className="btn btn-primary btn-sm" |
| 71 | + onClick={() => this.editRecord(record)} |
| 72 | + style={{marginRight: '5px'}}> |
| 73 | + <i className="fa fa-edit"></i> |
| 74 | + </button> |
| 75 | + <button |
| 76 | + className="btn btn-danger btn-sm" |
| 77 | + onClick={() => this.deleteRecord(record)}> |
| 78 | + <i className="fa fa-trash"></i> |
| 79 | + </button> |
| 80 | + </Fragment> |
| 81 | + ); |
| 82 | + } |
| 83 | + } |
| 84 | + ]; |
| 85 | + this.config = { |
| 86 | + page_size: 10, |
| 87 | + length_menu: [ 10, 20, 50 ], |
| 88 | + button: { |
| 89 | + excel: true, |
| 90 | + print: true |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + this.state = { |
| 95 | + records: [ |
| 96 | + { |
| 97 | + "id": "55f14312c7447c3da7051b26", |
| 98 | + "address": "228 City Road", |
| 99 | + "name": ".CN Chinese", |
| 100 | + "postcode": "3JH", |
| 101 | + "rating": 5, |
| 102 | + "type_of_food": "Chinese" |
| 103 | + }, |
| 104 | + { |
| 105 | + "id": "55f14312c7447c3da7051b27", |
| 106 | + "address": "376 Rayleigh Road", |
| 107 | + "name": "@ Thai", |
| 108 | + "postcode": "5PT", |
| 109 | + "rating": 5.5, |
| 110 | + "type_of_food": "Thai" |
| 111 | + }, |
| 112 | + { |
| 113 | + "id": "55f14312c7447c3da7051b28", |
| 114 | + "address": "30 Greyhound Road Hammersmith", |
| 115 | + "name": "@ Thai Restaurant", |
| 116 | + "postcode": "8NX", |
| 117 | + "rating": 4.5, |
| 118 | + "type_of_food": "Thai" |
| 119 | + }, |
| 120 | + { |
| 121 | + "id": "55f14312c7447c3da7051b29", |
| 122 | + "address": "30 Greyhound Road Hammersmith", |
| 123 | + "name": "@ Thai Restaurant", |
| 124 | + "postcode": "8NX", |
| 125 | + "rating": 4.5, |
| 126 | + "type_of_food": "Thai" |
| 127 | + } |
| 128 | + ] |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + editRecord(record) { |
| 133 | + console.log("Edit Record", record); |
| 134 | + } |
| 135 | + |
| 136 | + deleteRecord(record) { |
| 137 | + console.log("Delete Record", record); |
| 138 | + } |
| 139 | + |
| 140 | + render() { |
| 141 | + return ( |
| 142 | + <div> |
| 143 | + <ReactDatatable |
| 144 | + config={this.config} |
| 145 | + records={this.state.records} |
| 146 | + columns={this.columns} |
| 147 | + /> |
| 148 | + </div> |
| 149 | + ) |
| 150 | + } |
| 151 | +} |
| 152 | + |
| 153 | +render(<App />, document.getElementById("app")); |
| 154 | +``` |
| 155 | + |
| 156 | +# API |
| 157 | + |
| 158 | +```js |
| 159 | +import ReactDatatable from '@ashvin27/react-datatable'; |
| 160 | +or |
| 161 | +var ReactDatatable = require('@ashvin27/react-datatable') |
| 162 | +``` |
| 163 | +## Props |
| 164 | +| Name | Type | Description |
| 165 | +| ------------- | ------------- | ------------- | |
| 166 | +| config | Object[] | This props will used to specify datatable configuration |
| 167 | +| records | Object[] | This props will used to pass records/data to datatable |
| 168 | +| columns | Object[] | This props will used to specify datatable column configuration |
| 169 | + |
| 170 | +## Options |
| 171 | +| Name | Type | default | Description |
| 172 | +| ------------- | ------------- | ------------- | ------------- | |
| 173 | +| className | String | | Datatable additional class, use to appy additional styling on table |
| 174 | +| id | String | | Identifier of datatable |
| 175 | +| page_size | Number | 10 | Specify the page length (number of rows per page) |
| 176 | +| length_menu | Array[] | [10, 25, 50, 75, 100] | Specify the options in the page length `select` list. |
| 177 | +| filename | String | "table" | Specify the export filename |
| 178 | +| button | Object[] | { excel: false, print: false } | Use to enable/disable export buttons. By default buttons are disabled. |
| 179 | +| sort | Object[] | { column: "", order: "asc" } | Initial sorting order to apply to the datatable |
| 180 | + |
| 181 | +## Columns |
| 182 | +| Name | Type | default | Description |
| 183 | +| ------------- | ------------- | ------------- | ------------- | |
| 184 | +| key | String | | Specify the key of record which value will display in table cell |
| 185 | +| text | String | | Spcify the table column text |
| 186 | +| className | String | | Table column additional class fo styling |
| 187 | +| align | String | left | Specify the content alignment |
| 188 | +| width | Number | | Specify the column width |
| 189 | +| sortable | Boolean | false | Specify the column is sortable of not |
| 190 | +| cell | Function(record):string | | Datatable |
| 191 | + |
| 192 | +## Install |
| 193 | + |
| 194 | +With [npm](https://npmjs.org/) installed, run |
| 195 | + |
| 196 | +``` |
| 197 | +npm i @ashvin27/react-datatable |
| 198 | +``` |
0 commit comments