2.0.2 • Published 3 years ago

bootstrap-react-treetable v2.0.2

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

bootstrap-react-treetable

A React component that presents a simple TreeTable, allowing the user to supply styling and rendering options. A default styling (using Bootstrap 4) is also available.

Installation

Run the following command: npm install bootstrap-react-treetable --save or yarn add bootstrap-react-treetable --save

Usage

Firstly, import the component as follows:

import BootstrapTreeTable from 'bootstrap-react-treetable';

Then include it in your components as follows:

<BootstrapTreeTable tableData={this.props.tableData} columns={columns} control={control}/>

See our Demo page to see the component in action.

BootstrapTreeTable expects data in the following format:

[
    {
        data: {
            name: "name0",
            dataType: "string0",
            example: "ex0",
            description: "desc0"
        },
        children: [
            {
                data: {
                    name: "name0-0",
                    dataType: "string0-0",
                    example: "ex0-0",
                    description: "desc0-0"
                },
                children: []
            }, {
                data: {
                    name: "name0-1",
                    dataType: "string0-1",
                    example: "ex0-1",
                    description: "desc0-1"
                },
                children: []
            }, {
                data: {
                    name: "name0-2",
                    dataType: "string0-2",
                    example: "ex0-2",
                    description: "desc0-2"
                },
                children: [
                    {
                        data: {
                            name: "name0-2-1",
                            dataType: "string0-2-1",
                            example: "ex0-2-1",
                            description: "desc0-2-1"
                        },
                        children: []
                    }
                ]
            }
        ]
    },
    {
        data: {
            name: "name1",
            dataType: "string1",
            example: "ex1",
            description: "desc1 &euro; &euro;"
        },
        children: []
    },
    {
        data: {
            name: "name2",
            dataType: "string2",
            example: "ex2",
            description: "desc2 &euro; &euro; &euro; &euro;"
        },
        children: []
    }
]

Configuration options

Obviously, the tableData prop is required. There is one other required prop:

columns is an array of objects, describing the columns to be rendered. The options within a column object are:

Attribute NameTypeDescriptionExampleRequired
dataFieldStringthe field that holds the data to be displayednameYes
headingStringthe column header to be used - if not supplied, the dataField is used instead.NameNo
fixedWidthBooleanShould the column be defined with a fixed width? - default is falsefalseNo
percentageWidthNumberThe percentage width this column will be allocated, should fixedWidth be true25No
styleClassStringA CSS class to be applied to the TD element for this fieldwhateverNo
rendererfunctionA function to be applied to the data - see further detail belowwhateverNo
sortableBooleanIs this field sortable? Default is yesfalseNo
sortUsingRendererBooleanWhen sorting, sort using the output of the rendererfalseNo
sortOrderStringIndicates that the table should be sorted by this field in this order - values are 'asc' or 'desc'ascNo
sortTypeStringIndicates the data type this field should be sorted as - options are string, date or numberstringNo
sortDateFormatStringThe format of the date to be sorted (assuming sortType is date). This uses date-fns, so the formats are specified here. Note that these are different to the formats used by Moment.js, so this is a breaking change.stringNo
filterableBooleanShould this column be included when filtering the data? - default is falsetrueNo

Further control of how the table is displayed can be provided using the control prop.

Attribute NameTypeDescriptionExampleRequired
visibleRowsNumberNumber of levels to display automatically - default is 12No
showExpandCollapseButtonBooleanShould the Expand All/Collapse All button be displayed? - default is falsefalseNo
allowSortingBooleanEnable or disable sorting on this table - default is falsefalseNo
allowFilteringBooleanEnable or disable filtering on this table - default is falsefalseNo
filterInputPlaceholderTextStringText to display as the placeholder in the filter input boxFilter...No
showPaginationBooleanPaginate the table, and provide a set of links at the bottom for navigation - default is falsefalseNo
initialRowsPerPageNumberNumber of rows to display when paginated10No

Should you wish to have multiple header rows, you can supply a topRows prop. These rows are rendered above the main header row for the table. This prop is an array of arrays of objects.

Attribute NameTypeDescriptionExampleRequired
headingStringThe text to display in the header cellWhateverNo
colspanNumberThe number of columns this header should span - default is 11No
rowspanNumberThe number of rows this header should span - default is 11No
alignmentStringThe text alignment of this header cell - one of left, center or rightcenterNo
verticalAlignmentStringThe text alignment of this header cell - one of baseline, top, middle, bottom, text-top or text-bottom - see Bootstrap docs for detailscenterNo

The colspan values should add up to the number of columns in the regular table, or odd things will happen with your display.

NOTE: this plugin doesn't do any checking on the colspan and rowspan numbers you put in - it's up to you to ensure you've got the values correct. Also, you cannot span columns or rows on the main table headers (the one which is used for sorting).

Rendering option

A function can be provided on a per-column basis to control the display of the data from that column.

This function should accept 2 parameters:

  1. dataRow - the entire row of data being operated on
  2. dataField - the name of the field within that row to be displayed

Thus the actual data for the column will be provided as dataRow.data[dataField]

The function should return a simple type (string, number etc) or HTML - see the example below:

let descriptionRenderer = function (dataRow, dataField) {
    return <span dangerouslySetInnerHTML={{__html: dataRow.data[dataField]}}></span>
};

Styling

The component is styled using Bootstrap 4 classes. Bootstrap 4 is a peer dependency, so it needs to be provided by the project using this one.

Sorting

The table can be sorted by default, simply set the sortOrder attribute of the relevant column.

Clicking the header of any column will sort by that column, in ascending order. Clicking again will sort in descending order. Appropriate icons are used to indicate the sort order.

You can prevent sorting of a specific column by simply setting the sortable attribute of the relevant column to false.

Use as a simple DataTable

To use this component as a simple datatable (i.e. no expandable capabilities), simply provide a tableData prop with no children attributes.

Release History

ReleaseDescriptionRelease date
2.0.2Added Multiple header rows04-Mar-2021
2.0.0React lifestyle methods removed, Moment.js removed, bugfixes applied21-Jan-2021
1.0.2Audit fixes01-Oct-2019
1.0.126-Jun-2019
1.0.0First release22-Feb-2019
1.0.0-rc.1First release candidate of 1.0.022-Feb-2019

Development

Use yalc instead of "npm link".