1.3.1 • Published 3 years ago

custom-react-data-table v1.3.1

Weekly downloads
28
License
MIT
Repository
github
Last release
3 years ago

React API Data Table

Minimum package size

npm

React API Data Table is a simple and custom table with the logical features. You can easily manage your data table by search operation, pagination, page size change, sorting column and render your custom column with your own style. We are made with a core style but you can change everything with your own styles even you can change the icon of the pagination, sorting, changeable position of the pagination/page size with the visible and hide feature, and text renders with different-2 languages. Overall it's flexible with any website, cms, mis etc.

Sample data table

Click below link for react simple data table

custom-simple-data-table

Installation

React data table requires ReactJS v16+ to run.

Install the dependencies

 npm install --save custom-react-data-table

Just import these components...

import DatatableWithApi from "custom-react-data-table";
import 'custom-react-data-table/datatable.corestyle.css';

Either you can import a given style or you can make your own style for this data table.

Usage & Demo code

Just copy paste this code to check all features

import React, { Component } from 'react';

import 'custom-react-data-table/datatable.corestyle.css';
import DatatableWithApi from "custom-react-data-table";

export default class App extends Component {
    state = {
        data: null,
        orgData: null,
        count: 0,
        limit: 10,
        offset: 0,
    }
    componentDidMount() {
        this.fetchMyAPI();
    }

    fetchMyAPI = async (limit = this.state.limit, offset = this.state.offset) => {
        let urlobj = { url: `https://pokeapi.co/api/v2/pokemon?limit=${limit}&offset=${offset}` }
        await fetch(urlobj.url)
            .then((res) => res.json())
            .then((data) => {
                if (data["count"]) {
                    this.setState({ data: data.results, orgData: data.results, count: data.count });
                }
            });
    }

    pageChangeCallback = (page, limit) => {
        let offset = limit * (page - 1);
        this.setState({ limit, offset });
        this.fetchMyAPI(limit, offset);
    }
    filterTableCallback = (searchText) => {
        let sortData = this.state.orgData.filter(el => {
            let searchString = el && el.name ? el.name : "";
            searchString += el && el.url ? "," + el.url : "";
            if (searchString.indexOf(searchText) !== -1) {
                return true;
            }
            return false;
        });
        this.setState({ data: sortData });
    }
    pageSizeCallback = (value) => {
        let limit = value;
        this.setState({ limit });
        this.fetchMyAPI(limit, 0);
    }
    fieldSortingCallback = (field, order) => {
        let SortData = this.state.orgData;
        if (order === "up") {
            SortData.sort((a, b) => {
                if (a[field] > b[field]) {
                    return 1;
                }
                return -1;
            });
        } else {
            SortData.sort((a, b) => {
                if (a[field] > b[field]) {
                    return -1;
                }
                return 1;
            });
        }
        this.setState({ data: SortData });
    }

    render() {

        let tableHeadArray = [
            { title: "S.no.", serial: true, thClass: "firstTH" },
            { title: "Name", field: "name", sorting: true, thClass: "secondTH" },
            {
                title: "Url",
                field: "url",
                sorting: true,
                tdStyle: (obj) => {
                    let style = {};
                    if (obj.url.indexOf("1") !== -1) {
                        style = { background: "#ffe047" };
                    }
                    return style;
                },
                return: (obj) => {
                    return <a href={`${obj.url}`} target="_blank" rel="noopener noreferrer">{obj.url}</a>;
                },
            },
            {
                title: "Action",
                return: (obj) => {
                    return (
                        <>
                            <button
                                onClick={() => console.log("edit obj =", obj)}
                            >
                                Edit
                        </button>
                            <button onClick={() => console.log("Delete obj ", obj)}>
                                Delete
                        </button>
                        </>
                    );
                },
            },
        ];

        return (
            <div>
                {<DatatableWithApi
                    header={tableHeadArray}
                    dataArr={this.state.data}
                    pageChangeCallback={this.pageChangeCallback}
                    fieldSortingCallback={this.fieldSortingCallback}
                    filterTableCallback={this.filterTableCallback}
                    pageSizeCallback={this.pageSizeCallback}
                    showPageSize={{
                        title: "पृष्ठ संख्या: ",
                        defaultValue: this.state.limit,
                        pageSizeArr: [10, 20, 50, 100, 250],
                        top: true, bottom: true
                    }}
                    showPagination={{
                        top: true, bottom: true,
                        doubleLeftImg: "", leftImg: "",
                        rightImg: "", doubleRightImg: "",
                        dleft_tooltip: "first page",
                        left_tooltip: "prev. page",
                        right_tooltip: "next page",
                        dright_tooltip: "last page"
                    }}
                    showTotalRecord={{ top: true, bottom: true, value: this.state.count }}
                    tableClass={"table"}
                    tableHeadClass={"table head"}
                    tableHeadRowClass={"table rowhead"}
                    tableBodyClass={"table body"}
                    norecordsfound={{ align: "center", title: "no records"}}
                />}
            </div>
        );
    }
}

Props

PropsObject KeysTypeDetailse.g.
dataArr (mandatory)ArrayAn array of the table data
header (mandatory)ArrayAn array of the table head object
title (mandatory)StringCustom table column title"S.No."
serial (optional)BooleanIf true show the serial number of the record
field (mandatory)StringData array object keysif data array is {name:"abc"}{name:"def"} then field is 'name'
sorting (optional)BooleanIf true show the sorting option to related field
thClass (optional)Stringfor th tag class
tdStyle (optional)Callback FunctionThis function is return special case style and record object is an attribute of this functiontdStyle: (obj) => { let style = {}; if (obj.url.indexOf("1") !== -1) { style = { background: "#ffe047" }; } return style; }
return (optional)Callback FunctionThis function is return custom design data for a cell and record object is an attribute of this functione.g.1: return: (obj) => {return <a href={${obj.url}}>{obj.url}; } e.g.2: return: (obj) => { return ( <> <button onClick={() => console.log("edit obj =", obj)}>Edit</>);}
pageChangeCallback (mandatory)FunctionPage change callback
fieldSortingCallback (optional)FunctionSorting column callback
filterTableCallback (optional)FunctionOn search filter table callback
pageSizeCallbackFunctionOn page size change callback
showPageSize (optional)ObjectPage size data object
title (optional)StringCustom page size title
defaultValue (optional)IntegerPage size default value
pageSizeArr (optional)Integer ArrayPage size array for select page size10,20,50
top (optional)Booleanpage size on top if true
bottom (optional)Booleanpage size on bottom if true
showPagination (optional)ObjectPagination data object
top (optional)BooleanPagination on top if true
bottom (optional)BooleanPagination on bottom if true
dleft_tooltip (optional)StringDouble left button tooltip title"first page"
left_tooltip (optional)StringLeft button tooltip title"previous page"
right_tooltip (optional)StringRight button tooltip title"next page"
dright_tooltip (optional)StringDouble right button tooltip title"last page"
doubleLeftImg (optional)StringFirst page button image/icon url
leftImg (optional)StringPrevious page image/icon url
rightImg (optional)StringRight image/icon url
doubleRightImg (optional)StringRight image/icon url
showTotalRecord (mandatory)ObjectTotal record of the table object
top (optional)BooleanTotal Records show on top if true
bottom (optional)BooleanTotal Records show on bottom if true
value (mandatory)String/IntegerTotal Records value
norecordsfound (optional)Objectshow the no records title and align the text{ align: "center", title: "no records"}
title (mandatory)StringTotal Records value
align (optional)Stringalign of the text - center, left and right
tableClass (optional)StringTable tag class name
tableHeadClass (optional)StringTable head tag class name
tableHeadRowClass (optional)StringTable head row class name
tableBodyClass (optional)StringTable body class name

License

MIT

1.3.1

3 years ago

1.2.6

3 years ago

1.2.5

3 years ago

1.2.4

3 years ago

1.2.3

3 years ago

1.2.2

3 years ago

1.2.1

3 years ago

1.1.12

3 years ago

1.1.11

3 years ago

1.1.10

3 years ago

1.1.9

3 years ago

1.1.8

3 years ago

1.1.7

3 years ago

1.1.6

3 years ago

1.1.5

3 years ago

1.1.1

3 years ago

1.1.0

3 years ago

1.0.9

3 years ago

1.1.4

3 years ago

1.1.3

3 years ago

1.1.2

3 years ago

1.0.11

3 years ago

1.0.10

3 years ago

1.0.13

3 years ago

1.0.12

3 years ago

1.0.8

3 years ago

1.0.7

3 years ago

1.0.6

3 years ago

1.0.5

3 years ago

1.0.4

3 years ago

1.0.3

3 years ago

1.0.2

3 years ago

1.0.1

3 years ago

1.0.0

3 years ago