18.5.0 • Published 5 years ago

react-datatable-ext v18.5.0

Weekly downloads
2
License
MIT
Repository
-
Last release
5 years ago

react-datatable

npm version

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.

Features

  • Lightweight
  • Fully customizable (JSX, templates, state, styles, callbacks)
  • Client-side & Server Side Pagination
  • Multi-sort
  • Filters
  • Minimal design
  • Fully controllable via optional props and callbacks

Installation

With npm installed, run

npm i react-datatable-ext

Usage

import React, { Component, Fragment } from 'react';
import { render} from 'react-dom';
import ReactDatatable from 'react-datatable-ext';

class App extends Component {
    constructor(props) {
        super(props);
        this.columns = [
            {
                key: "name",
                text: "Name",
                className: "name",
                align: "left",
                sortable: true,
            },
            {
                key: "address",
                text: "Address",
                className: "address",
                align: "left",
                sortable: true
            },
            {
                key: "postcode",
                text: "Postcode",
                className: "postcode",
                sortable: true
            },
            {
                key: "rating",
                text: "Rating",
                className: "rating",
                align: "left",
                sortable: true
            },
            {
                key: "type_of_food",
                text: "Type of Food",
                className: "type_of_food",
                sortable: true,
                align: "left"
            },
            {
                key: "action",
                text: "Action",
                className: "action",
                width: 100,
                align: "left",
                sortable: false,
                cell: record => { 
                    return (
                        <Fragment>
                            <button
                                className="btn btn-primary btn-sm"
                                onClick={() => this.editRecord(record)}
                                style={{marginRight: '5px'}}>
                                <i className="fa fa-edit"></i>
                            </button>
                            <button 
                                className="btn btn-danger btn-sm" 
                                onClick={() => this.deleteRecord(record)}>
                                <i className="fa fa-trash"></i>
                            </button>
                        </Fragment>
                    );
                }
            }
        ];
        this.config = {
            page_size: 10,
            length_menu: [ 10, 20, 50 ],
            button: {
                excel: true,
                print: true
            }
        }
        
        this.state = {
            records: [
              {
                "id": "55f14312c7447c3da7051b26",
                "address": "228 City Road",
                "name": ".CN Chinese",
                "postcode": "3JH",
                "rating": 5,
                "type_of_food": "Chinese"
              },
              {
                "id": "55f14312c7447c3da7051b27",
                "address": "376 Rayleigh Road",
                "name": "@ Thai",
                "postcode": "5PT",
                "rating": 5.5,
                "type_of_food": "Thai"
              },
              {
                "id": "55f14312c7447c3da7051b28",
                "address": "30 Greyhound Road Hammersmith",
                "name": "@ Thai Restaurant",
                "postcode": "8NX",
                "rating": 4.5,
                "type_of_food": "Thai"
              },
              {
                "id": "55f14312c7447c3da7051b29",
                "address": "30 Greyhound Road Hammersmith",
                "name": "@ Thai Restaurant",
                "postcode": "8NX",
                "rating": 4.5,
                "type_of_food": "Thai"
              }
            ]
        }
    }

    editRecord(record) {
        console.log("Edit Record", record);
    }

    deleteRecord(record) {
        console.log("Delete Record", record);
    }

    render() {
        return (
            <div>
                <ReactDatatable
                    config={this.config}
                    records={this.state.records}
                    columns={this.columns}
                />
            </div>
        )
    }
}

render(<App />, document.getElementById("app"));

API

import ReactDatatable from 'react-datatable-ext';
or
var ReactDatatable = require('react-datatable-ext')

Props

NameTypeDescription
classNameStringDatatable additional class, use to appy additional styling on table
columnsObject[]This props will used to specify datatable column configuration
configObject[]This props will used to specify datatable configuration
dynamicbooleanThis props will used to specify the table data will be server side or static
idStringIdentifier of datatable
recordsObject[]This props will used to table records
total_recordNumberThis props will used to specify the total records in case of table data is server side.
onChangeFuntion(Object)This method will call on table actions(page change, sorting, filtering, page length change)
onRowClickedFuntion(Object)This method will call when user click on a row, return row object.

Options

NameTypedefaultDescription
buttonObject[]{ excel: false, print: false, csv: false }Use to enable/disable export buttons(Excel, CSV, Print). By default buttons are disabled.
filenameString"table"Specify the export filename
length_menuArray[]10, 25, 50, 75, 100Specify the options in the page length select list.
page_sizeNumber10Specify the page length (number of rows per page)
sortObject[]{ column: "", order: "asc" }Initial sorting order to apply to the datatable
show_filterbooleantrueuse to specify either show or hide filter option
show_firstbooleantrueuse to specify either show or hide pagination first button
show_infobooleantrueuse to specify either show or hide pagination info
show_lastbooleantrueuse to specify either show or hide pagination last button
show_length_menubooleantrueuse to specify either show or hide page length menu
show_paginationbooleantrueuse to specify either show or hide pagination

Columns

NameTypedefaultDescription
alignStringleftSpecify the content alignment
classNameStringTable column additional class fo styling
keyStringSpecify the key of record which value will display in table cell
sortableBooleanfalseSpecify the column is sortable of not
textStringSpcify the table column text
widthNumberSpecify the column width
cellFunction(record, index):stringYou can use any react component or JSX to display content in cells
18.5.0

5 years ago

16.5.15

5 years ago

16.5.14

5 years ago

16.5.13

5 years ago

16.5.12

5 years ago

16.5.11

5 years ago

16.5.10

5 years ago

16.5.9

5 years ago

16.5.8

5 years ago

16.5.7

5 years ago

16.5.6

5 years ago

16.5.5

5 years ago

16.5.3

5 years ago

16.5.2

5 years ago

16.5.1

5 years ago

16.5.0

5 years ago