dynamic-muidatatable

0.2.1 • Public • Published

Dynamic MUI Table

Its designed to handle DJango generic class pagination api with filter, search and sorting functionality. This component have following features.

Note: Requires redux-axios-jwt npm package. Please set it up before using this.

This componenet is built upon MUI Datatable and Redux. MUI Datatable

  • Pagination
  • Sorting
  • Filter
  • Search
  • Download
  • New custom columns
  • Custom Actions

Sample code for DJango generic API

Following is a sample DJango DRF generic pagination api. For ref: Pagination - DRF

class VehiclePaginationView(generics.ListAPIView):
	authentication_classes = (authentication.JWTAuthentication,)
	permission_classes = [permissions.IsAuthenticated]

	queryset = vehicle.objects.filter(de_fleeted=False).order_by('vehicle_reg_no')
	serializer_class = CustomVehicleDetialSerializer
	filter_backends = [
			rest_framework.DjangoFilterBackend,
			filters.OrderingFilter,
			filters.SearchFilter
		]
	ordering_fields = (
			'vehicle_reg_no',
			'id',
			'date_added'
		)
	filter_fields = ('hire_status', 'make', 'model')
	search_fields = (
			'vehicle_reg_no',
			'supplier_id__first_name',
			'manufacturer_id__company'
		)

Usage in React JS/Next JS

npm install

npm i dynamic-muidatatable

Following is a sample implementation code.

import DynamicMUITable from "./DynamicMUITable"

const VehicleProfileCard = (value, tableMeta, updateValue) => {
	return (
		<ProfileCard reg_number={value.vehicle_reg_no} vehicle_id={value.id} />
	);
};

const ActionButtons = (value, tableMeta, updateValue) => {
	return (
		<>
            <ServiceDialog rowId={value} method="put" />
            <DeleteServiceDialog rowId={value} />
		</>
	);
};

const columnsCustom = [
	{
        name: "vehicle",
        label: "Reg Number",
        vehicle: true,
        sort: true,
        comp: VehicleProfileCard,
        display: true,
        filter: true,
        filterOptions: [],
        filterType: "dropdown",
        value: "reg_number",
	},
	new: [
            {
            	name: "id",
                label: "Label",
                comp: ActionButtons,
            }
	]
 ]

export default function DataTable() {
	return (
		<DynamicMUITable
		title="Table Title"
		urlLink="vehicle_accessories/service/list"
		columnsCustom={columnsCustom}
		/>
	)
}

API

<DynamicMUITable />

The component accepts the following props.

Name Type Description
title array Title used to caption table
columnsCustom array Columns used to describe table. Must be either an array of simple strings or objects describing a column
urlLink array API link to make a call to get data from the server
Customize columnsCustom

On each column object, you have the ability to customize columns to your liking.

Name Type Default Description
name string Name of column (This field is required)
label string Column Header Name override
vehicle boolean false Incase, its main vehicle column
sort boolean false Enable/disable sorting on column.
comp function Custom function you want to show. function(value, tableMeta, updateValue) { return (<Component />)}
value array In case column is an object. This value defines to show specific index of the object. For e.g. vehicle: {id: 1, reg_number: "ABC-123"}. If you want to print reg_number, than {name: "vehicle", value: "reg_number"}.
display boolean or string true Display column in table. Possible values:

  • true: Column is visible and toggleable via the View Columns popover in the Toolbar.
  • false: Column is not visible but can be made visible via the View Columns popover in the Toolbar.
  • excluded: Column is not visible and not toggleable via the View Columns popover in the Toolbar.

See also: viewColumns and filter options.

filter boolean false Display column in filter list.
filterOptions object

These options affect the filter display and functionality from the filter dialog. To modify the filter chips that display after selecting filters, see customFilterListOptions

This option is an object of several options for customizing the filter display and how filtering works.

  • names: custom names for the filter fields Example
  • logic: custom filter logic Example
  • display(filterList, onChange(filterList, index, column), index, column, filterData): Custom rendering inside the filter dialog Example. filterList must be of the same type in the main column options, that is an array of arrays, where each array corresponds to the filter list for a given column.
  • renderValue: A function to customize filter choices Example. Example use case: changing empty strings to "(empty)" in a dropdown.
  • fullWidth (boolean): Will force a filter option to take up the grid's full width.
filterType string 'dropdown' Choice of filtering view. Takes priority over global filterType option.enum('checkbox', 'dropdown', 'multiselect', 'textField', 'custom') Use 'custom' if you are supplying your own rendering via filterOptions.
Cutom Columns

Insert new key in object as shown in example above.

Name Type Description
name string Name of column (This field is required)
label string Column Header Name override
comp function Custom function you want to show. function(value, tableMeta, updateValue) { return (<Component />)}

Developed and Compiled: Hamza Fayyaz (Hayan Systems)

Package Sidebar

Install

npm i dynamic-muidatatable

Weekly Downloads

1

Version

0.2.1

License

MIT

Unpacked Size

54.9 kB

Total Files

21

Last publish

Collaborators

  • hfayyaz101