1.1.0 • Published 3 years ago

custom-simple-data-table v1.1.0

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

React Simple Data Table

Minimum package size

npm

React Simple Data Table is a 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 API data table

custom-react-data-table

Installation

React data table requires ReactJS v16+ to run.

Install the dependencies

 npm install --save custom-simple-data-table

Just import these components...

import "custom-simple-data-table/datatable.corestyle.css";
import DatatableComponent from "custom-simple-data-table";

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-simple-data-table/datatable.corestyle.css";
import DatatableComponent from "custom-simple-data-table";

export default class App extends Component {
  state = {
    data: null,
    count: 0,
  };
  componentDidMount() {
    this.fetchMyAPI();
  }
  fetchMyAPI = async () => {
    let urlobj = {
      url: `https://pokeapi.co/api/v2/pokemon?limit=30&offset=0`,
    };
    await fetch(urlobj.url)
      .then((res) => res.json())
      .then((data) => {
        if (data["count"]) {
          this.setState({
            data: data.results,
          });
        }
      });
  };
  pageChangeCallback = (page) => {
    console.log("page change callback event : ", page);
  };
  filterTableCallback = (searchText) => {
    console.log("search text callback event : ", searchText);
  };
  pageSizeCallback = (value) => {
    console.log("page size callback event ", value);
  };
  fieldSortingCallback = (object) => {
    console.log("coloum sorting callback event ", object);
  };
  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>
        <DatatableComponent
          header={tableHeadArray} // mandatory
          dataArr={this.state.data} // mandatory
          pageChangeCallback={this.pageChangeCallback} // optional
          fieldSortingCallback={this.fieldSortingCallback} // optional
          filterTableCallback={this.filterTableCallback} // optional
          pageSizeCallback={this.pageSizeCallback} // optional
          showPageSize={{ // optional
            title: "पृष्ठ संख्या: ",
            defaultValue: 10,
            pageSizeArr: [10, 20, 30, 50, 100, 250],
            top: true,
            bottom: true,
          }}
          showPagination={{ // optional
            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={{ // optional
            top: true,
            bottom: true,
          }}
          norecordsfound={{ // optional
            title: "No record's found !",
            align: "center",
            trClass: "tr text",
            tdClass: "td text",
          }}
          searchShow={{ // optional
            show: true,
            clearSearch: true,
            placeholder: "filter...",
          }}
          tableClass={"table"} // optional
          tableHeadClass={"table head"} // optional
          tableHeadRowClass={"table rowhead"} // optional
          tableBodyClass={"table body"} // optional
        />
      </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 (optional)FunctionPage change callback
fieldSortingCallback (optional)FunctionSorting column callback
filterTableCallback (optional)FunctionOn search filter table callback
pageSizeCallback (optional)FunctionOn 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 (optional)ObjectTotal record of the table object
top (optional)BooleanTotal Records show on top if true
bottom (optional)BooleanTotal Records show on bottom if true
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
trClass (optional)StringRelated row class name
tdClass (optional)StringRelated cell class name
searchShow (optional)ObjectSearch any text from the table
show (mandatory)BooleanFor show the search field
clearSearch (optional)Booleanclear the input text
placeholder (optional)Stringplaceholder of the search input field
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