DataView displays data in grid or list layout with pagination and sorting features.
import { DataView, DataViewLayoutOptions } from 'primereact/dataview';
<script src="https://unpkg.com/primereact/core/core.min.js"></script>
<script src="https://unpkg.com/primereact/dataview/dataview.min.js"></script>
Layout of the DataView is managed by the PrimeFlex that can be downloaded from npm.
npm install primeflex
DataView requires a collection of items as its value and one or more templates depending on the layout mode e.g. list and grid.
DataView has two layout modes; list and grid where itemTemplate function is called by passing the item to render along with the layout mode.
const itemTemplate = (data, layout) => {
if (layout === 'list') {
return (
// List content
);
}
if (layout === 'grid') {
return (
Grid Content
);
}
}
<DataView value={products} layout={layout} itemTemplate={itemTemplate}></DataView>
DataViewLayoutOptions is a helper component to choose between layout modes. This component is used in controlled manner to manage the state of layout orientation.
<DataViewLayoutOptions layout={layout} onChange={(e) => setLayout(e.value)} />
Name | Type | Default | Description |
---|---|---|---|
id | string | null | Unique identifier of the element. |
layout | string | list | Layout of the items, valid values are "list" and "grid". |
dataKey | string | null | A property to uniquely identify an item. |
style | object | null | Inline style of the element. |
className | string | null | Style class of the element. |
Name | Parameters | Description |
---|---|---|
onChange | event.originalEvent: browser event event.value = layout mode e.g. "list" or "grid" | Callback to invoke when layout mode is changed. |
Pagination is enabled by setting paginator property to true, rows attribute defines the number of rows per page and pageLinks specify the the number of page links to display. Visit the paginator paginator component for more information about the available properties.
Pagination can either be used in Controlled or Uncontrolled manner. In controlled mode, first and onPage properties needs to be defined to control the pagination state.
<DataView value={products} layout={layout} itemTemplate={itemTemplate} paginator rows={10} first={first} onPage={(e) => setFirst(e.first)}></DataView>
In uncontrolled mode, only paginator property needs to be enabled. Initial page state can be still be provided using the first property in uncontrolled mode however it is evaluated at initial rendering and ignored in further updates. If you programmatically need to update the paginator, prefer to use the component as controlled.
<DataView value={products} layout={layout} itemTemplate={itemTemplate} paginator rows={10}></DataView>
sortField and sortOrder properties are available for sorting functionality, for flexibility there is no built-in UI available so that a custom UI can be used for the sorting element. Here is an example that uses a dropdown where simply updating the sortField-sortOrder bindings of the DataView initiates sorting.
const sortOptions = [
{label: 'Price High to Low', value: '!price'},
{label: 'Price Low to High', value: 'price'},
];
const header = (
<div className="grid">
<div className="col-12 md:col-4">
<Dropdown options={sortOptions} value={sortKey} placeholder="Sort By" onChange={onSortChange} />
</div>
</div>
);
<DataView value={products} header={header} sortOrder={sortOrder} sortField={sortField} />
const onSortChange = (event) => {
const value = event.value;
if (value.indexOf('!') === 0) {
setSortOrder(-1);
setSortField(value.substring(1, value.length));
setSortKey(value);
}
else {
setSortOrder(1);
setSortField(value);
setSortKey(value);
}
}
Lazy loading is useful to deal with huge datasets, in order to implement lazy loading use the pagination in controlled mode and utilize the onPage callback to load your data from the backend. Pagination in this case needs to display the logical number of records so bind this value to the totalRecords property so that paginator can display itself according to the total records although you'd only need to load the data of the current page. Refer to DataTable lazy loading for a sample implementation.
Name | Type | Default | Description |
---|---|---|---|
id | string | null | Unique identifier of the element. |
header | JSX or string | null | Header content of the component. |
footer | JSX or string | null | Footer content of the component. |
value | array | null | An array of objects to display. |
layout | string | list | Layout of the items, valid values are "list" and "grid". |
rows | number | null | Number of rows to display per page. |
first | number | 0 | Index of the first record to render. |
totalRecords | number | null | Number of total records, defaults to length of value when not defined. |
paginator | boolean | false | When specified as true, enables the pagination. |
paginatorPosition | string | bottom | Position of the paginator, options are "top","bottom" or "both". |
alwaysShowPaginator | boolean | true | Whether to show it even there is only one page. |
paginatorClassName | string | null | Style class of the paginator element. |
paginatorTemplate | string|object | FirstPageLink PrevPageLink PageLinks NextPageLink LastPageLink RowsPerPageDropdown | Template of the paginator. For details, refer to the template section of the paginator documentation for further options. |
paginatorLeft | Element | null | Content for the left side of the paginator. |
paginatorRight | Element | null | Content for the right side of the paginator. |
pageLinkSize | number | 5 | Number of page links to display. |
rowsPerPageOptions | array | null | Array of integer values to display inside rows per page dropdown. |
currentPageReportTemplate | string | (&123;currentPage&125; of &123;totalPages&125;) | Template of the current page report element. |
paginatorDropdownAppendTo | DOM element | string | document.body | DOM element instance where the overlay panel should be mounted. Valid values are any DOM Element and 'self'. The self value is used to render a component where it is located. |
emptyMessage | string | No records found. | Text to display when there is no data. |
sortField | string | null | Name of the field to sort data by default. |
sortOrder | number | null | Order to sort the data by default. |
style | object | null | Inline style of the element. |
className | string | null | Style class of the element. |
lazy | boolean | false | Defines if data is loaded and interacted with in lazy manner. |
gutter | boolean | false | Whether the grid structure in the container has gutter. Default value is false. |
itemTemplate | function | null | Function that gets the option along with the layout mdoe and returns the content. |
Name | Parameters | Description |
---|---|---|
onPage | event.originalEvent: Browser event event.first: Index of the first records on page. event.rows: Number of records to display per page. | Callback to invoke on pagination. |
Following is the list of structural style classes, for theming classes visit theming page.
Name | Element |
---|---|
p-dataview | Container element. |
p-dataview-list | Container element in list layout. |
p-dataview-grid | Container element in grid layout. |
p-dataview-header | Header section. |
p-dataview-footer | Footer section. |
p-dataview-content | Container of items. |
This section is under development. After the necessary tests and improvements are made, it will be shared with the users as soon as possible.
None.
Built-in component themes created by the PrimeReact Theme Designer.
Premium themes are only available exclusively for PrimeReact Theme Designer subscribers and therefore not included in PrimeReact core.
Beautifully crafted premium create-react-app application templates by the PrimeTek design team.