DataTable with API ( Material-Table ) - in React js

 

mtable


Data- table / Material- Table

material-table is a simple and powerful Data table for React based on Material-UI Table with some additional features.

Material- table, installation
-> installation

Options property could be given to component as options property. You can change behaviour of grid.

FieldTypeDefaultDescription
actionsCellStyleobjectActions cell style
actionsColumnIndexnumber0Order of actions column
addRowPositionfirst or lastlastPosition of add row
columnsButtonbooleanfalseFlag for columns button that controls which column could be rendered
cspNoncestringcspNonce for react-beautiful-dnd context
debounceIntervalnumber200debounce interval for search and filter
detailPanelColumnAlignmentleft or rightleftDetail panel column alignment.
detailPanelTypesingle or multiplemultipleDetail panel visibility type.
doubleHorizontalScrollbooleanfalseFlag for double scroll bar for long tables
emptyRowsWhenPagingbooleantrueFlag for rendering rows to complete page size
exportAllDatabooleanfalseFlag for export all data instead of rendered data
exportButtonbooleanfalseFlag for export button that render export buttons
exportDelimiterstring,Delimiter to use in exported CSV file
exportFileNamestringTitleExported file name
exportCsvfuncFunction to create a custom CSV file
filteringbooleanfalseFlag for filtering row
filterCellStyleobjectFilter cell style for all filter cells
fixedColumnsobjectPlease check fixed columns examples
groupingbooleantrueFlag for groupbar visibility
headerbooleantrueFlag for header visibility
headerStyleobjectHeader cell style for all headers
loadingTypeoverlay or linearoverlayLoading animation type
maxBodyHeightnumber or stringMax body height
minBodyHeightnumber or stringMin body height
initialPagenumber0Initial Page Number
paddingdefault or densedefaultPadding of cells and rows
pagingbooleantrueFlag for paging feature
pageSizenumeric5Number of rows that would be rendered on every page
pageSizeOptionsarray[5, 10, 20]Page size options that could be selected by user
paginationTypenormal or steppednormalFlag for pagination type
rowStyleobject or funcCss style to be applied rows
showEmptyDataSourceMessagebooleantrueFlag for showing message if there is no data in table
showFirstLastPageButtonsbooleantrueFlag for showing first and last page buttons on pagination component
showSelectAllCheckboxbooleantrueFlag for showing select all checkbox
showTextRowsSelectedbooleantrueFlag for showing selected rows text on toolbar
searchbooleantrueFlag for search feature
searchAutoFocusbooleanfalseInitialize search field focused
searchFieldAlignmentstring'right'Alignment for search text field in toolbar
searchFieldStyleobject{}Search field css style
searchFieldVariantstring'standard'Search field variant
searchTextstringInitialize search field before table was render
selectionbooleanfalseFlag for selection feature
selectionPropsobject or func{}Selection checkbox props
sortingbooleantrueFlag to activate or disable sorting feature of table
tableLayoutauto or fixedautoTo make columns width algorithm auto or fixed
toolbarbooleantrueFlag for toolbar
showTitlebooleantrueFlag for title
toolbarButtonAlignmentleft or right'right'Alignment for buttons in toolbar
draggablebooleantrueFlag for drag and drop headers
thirdSortClickbooleantrueFlag to allow unsorted state on third header click

Get Started ...


   
  import React from 'react';
  import MaterialTable from 'material-table'

  function App() {
    return (
    <div>
    <div style={{ maxWidth: '90%' }}>
    <MaterialTable
     columns={[
     { title: 'Adı', field: 'name' },
     { title: 'Soyadı', field: 'surname' },
     { title: 'Doğum Yılı', field: 'birthYear',
     type: 'numeric' },
     { title: 'Doğum Yeri', field: 'birthCity',
     lookup: { 34: 'İstanbul', 63: 'Şanlıurfa' } }
      ]}
    data={[{ name: 'Mehmet', surname: 'Baran',birthYear: 1987,birthCity:63}]}
    title="Demo Title"
    />
    </div>
    </div>
    );
  }

  export default App;

Table data from an External API in Material Table
 || Material UI

   
    
  import React, { useState, useEffect } from 'react';
  import './App.css';
  import MaterialTable from 'material-table'


  function App() {

    const [data, setData] = useState([])
    const columns = [
      { title: "ID", field: "id" },
      { title: "Username", field: "username" },
      { title: "Name", field: "name" },
      { title: "Email", field: "email" },
      { title: "Phone", field: "phone" },
      { title: "Web Link", field: 'website' }
    ]
    useEffect(() => {
      fetch("https://jsonplaceholder.typicode.com/users")
        .then(resp => resp.json())
        .then(resp => {
          setData(resp)
        })
    }, [])

    return (
      <div className="App">
        <h1 align="center">React-App</h1>
        <h4 align='center'>Material Table</h4>
        <MaterialTable
          title="Employee Data"
          data={data}
          columns={columns}
        />
      </div>
    );
  }

  export default App;