lara-light-indigo

DataView

DataView displays data in grid or list layout with pagination and sorting features.

Sort By Price
No results found
Import via Module

import { DataView, DataViewLayoutOptions } from 'primereact/dataview';
 
Import via CDN

<script src="https://unpkg.com/primereact/core/core.min.js"></script>
<script src="https://unpkg.com/primereact/dataview/dataview.min.js"></script>
 
Getting Started

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

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)} />
 
Properties of DataViewLayoutOptions
NameTypeDefaultDescription
idstringnullUnique identifier of the element.
layoutstringlistLayout of the items, valid values are "list" and "grid".
dataKeystringnullA property to uniquely identify an item.
styleobjectnullInline style of the element.
classNamestringnullStyle class of the element.
Events of DataViewLayoutOptions
NameParametersDescription
onChangeevent.originalEvent: browser event
event.value = layout mode e.g. "list" or "grid"
Callback to invoke when layout mode is changed.
Paginator

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>
 
Sorting

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

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.

Properties
NameTypeDefaultDescription
idstringnullUnique identifier of the element.
headerJSX or stringnullHeader content of the component.
footerJSX or stringnullFooter content of the component.
valuearraynullAn array of objects to display.
layoutstringlistLayout of the items, valid values are "list" and "grid".
rowsnumbernullNumber of rows to display per page.
firstnumber0Index of the first record to render.
totalRecordsnumbernullNumber of total records, defaults to length of value when not defined.
paginatorbooleanfalseWhen specified as true, enables the pagination.
paginatorPositionstringbottomPosition of the paginator, options are "top","bottom" or "both".
alwaysShowPaginatorbooleantrueWhether to show it even there is only one page.
paginatorClassNamestringnullStyle class of the paginator element.
paginatorTemplatestring|objectFirstPageLink PrevPageLink PageLinks NextPageLink LastPageLink RowsPerPageDropdownTemplate of the paginator. For details, refer to the template section of the paginator documentation for further options.
paginatorLeftElementnullContent for the left side of the paginator.
paginatorRightElementnullContent for the right side of the paginator.
pageLinkSizenumber5Number of page links to display.
rowsPerPageOptionsarraynullArray of integer values to display inside rows per page dropdown.
currentPageReportTemplatestring(&123;currentPage&125; of &123;totalPages&125;)Template of the current page report element.
paginatorDropdownAppendToDOM element | stringdocument.bodyDOM 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.
emptyMessagestringNo records found.Text to display when there is no data.
sortFieldstringnullName of the field to sort data by default.
sortOrdernumbernullOrder to sort the data by default.
styleobjectnullInline style of the element.
classNamestringnullStyle class of the element.
lazybooleanfalseDefines if data is loaded and interacted with in lazy manner.
gutterbooleanfalseWhether the grid structure in the container has gutter. Default value is false.
itemTemplatefunctionnullFunction that gets the option along with the layout mdoe and returns the content.
Events
NameParametersDescription
onPageevent.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.
Styling

Following is the list of structural style classes, for theming classes visit theming page.

NameElement
p-dataviewContainer element.
p-dataview-listContainer element in list layout.
p-dataview-gridContainer element in grid layout.
p-dataview-headerHeader section.
p-dataview-footerFooter section.
p-dataview-contentContainer of items.
Accessibility

This section is under development. After the necessary tests and improvements are made, it will be shared with the users as soon as possible.

Dependencies

None.

Component Scale

Input Style

Ripple Effect

Free Themes

Built-in component themes created by the PrimeReact Theme Designer.

Bootstrap
Blue
Purple
Blue
Purple
Material Design
Indigo
Deep Purple
Indigo
Deep Purple
Material Design Compact
Indigo
Deep Purple
Indigo
Deep Purple
Tailwind
Tailwind Light
Fluent UI
Blue
PrimeOne Design - 2022 NEW
Lara Indigo
Lara Blue
Lara Purple
Lara Teal
Lara Indigo
Lara Blue
Lara Purple
Lara Teal
PrimeOne Design - 2021
Saga Blue
Saga Green
Saga Orange
Saga Purple
Vela Blue
Vela Green
Vela Orange
Vela Purple
Arya Blue
Arya Green
Arya Orange
Arya Purple
Premium Themes

Premium themes are only available exclusively for PrimeReact Theme Designer subscribers and therefore not included in PrimeReact core.

Soho Light
Soho Dark
Viva Light
Viva Dark
Mira
Nano

Legacy Free Themes

Nova
Nova Alt
Nova Accent
Luna Blue
Luna Green
Luna Amber
Luna Pink
Rhea

Premium Create-React-App Templates

Beautifully crafted premium create-react-app application templates by the PrimeTek design team.

Sakai
Atlantis
Freya
Ultima
Diamond
Sapphire
Serenity
Babylon
Avalon
Apollo
Roma