React CardView - Getting Started
DevExtreme includes multiple data management components. You can use them to display tabular, hierarchical, multidimensional data, or present information as cards. For more information about these components and their features, see How to Choose a Data Management Component.
CardView is a UI component that displays data in a card-based format. You can use it for employee directories, contact lists, product catalogs, and task boards.
This tutorial explains how to add a CardView to a page and configure the component's core settings.
Each section in this tutorial covers a single configuration step. You can find the complete source code in the following GitHub repository:
Create a CardView
Add DevExtreme to your React application and use the following code to create a CardView:
- import React from 'react';
- import 'devextreme/dist/css/dx.fluent.blue.light.css';
- import CardView from 'devextreme-react/card-view';
- function App() {
- return (
- <CardView id="card-view">
- {/* Configuration goes here */}
- </CardView>
- );
- }
- export default App;
Bind CardView to Data
The CardView component can load and update data from different data source types. To use a local array, assign it to the dataSource property and specify the key field in the keyExpr property:
- import React from 'react';
- import 'devextreme/dist/css/dx.fluent.blue.light.css';
- import CardView from 'devextreme-react/card-view';
- import { employees } from './data.ts';
- function App() {
- return (
- <CardView
- dataSource={employees}
- keyExpr="ID">
- </CardView>
- );
- }
- export default App;
- const employees = [
- {
- ID: 1,
- FullName: "John Heart",
- Position: "CEO",
- Picture: "images/employees/01.png",
- Department: "Management",
- Phone: "+1 (213) 555-9392",
- Email: "jheart@dx-email.com"
- },
- {
- ID: 2,
- FullName: "Samantha Bright",
- Position: "COO",
- Picture: "images/employees/30.png",
- Department: "Management",
- Phone: "+1 (213) 555-2858",
- Email: "samanthab@dx-email.com"
- },
- {
- ID: 3,
- FullName: "Arthur Miller",
- Position: "CTO",
- Picture: "images/employees/10.png",
- Department: "Management",
- Phone: "+1 (310) 555-8583",
- Email: "arthurm@dx-email.com"
- },
- {
- ID: 4,
- FullName: "Robert Reagan",
- Position: "CMO",
- Picture: "images/employees/03.png",
- Department: "Management",
- Phone: "+1 (818) 555-2387",
- Email: "robertr@dx-email.com"
- },
- {
- ID: 5,
- FullName: "Greta Sims",
- Position: "HR Manager",
- Picture: "images/employees/04.png",
- Department: "Human Resources",
- Phone: "+1 (818) 555-6546",
- Email: "gretas@dx-email.com"
- },
- {
- ID: 6,
- FullName: "Brett Wade",
- Position: "IT Manager",
- Picture: "images/employees/05.png",
- Department: "IT",
- Phone: "+1 (626) 555-0358",
- Email: "brettw@dx-email.com"
- },
- ];
Configure Columns (Fields)
CardView uses the concept of columns from DataGrid. While it has rows and columns, the columns appear as fields on each card. The header shows the column names, and each field on a card includes a caption that usually matches the column dataField, and a unique value specific to the card.
The following code snippet restricts the CardView columns (fields) to FullName, Position, and Email and allows column reordering:
- import React from 'react';
- import 'devextreme/dist/css/dx.fluent.blue.light.css';
- import CardView, { Column } from 'devextreme-react/card-view';
- import { employees } from './data.ts';
- function App() {
- return (
- <CardView ...
- allowColumnReordering={true}
- >
- <Column dataField="FullName" allowHiding={false} />
- <Column dataField="Position" />
- <Column dataField="Email" />
- </CardView>
- );
- }
- export default App;
Filter and Search Data
The CardView includes the following UI elements used to search and filter data:
In this tutorial, the header filter and the search panel are displayed:
- import React from 'react';
- import 'devextreme/dist/css/dx.fluent.blue.light.css';
- import CardView, { HeaderFilter, SearchPanel } from 'devextreme-react/card-view';
- import { employees } from './data.ts';
- function App() {
- return (
- <CardView ... >
- <HeaderFilter visible={true} />
- <SearchPanel visible={true} />
- </CardView>
- );
- }
- export default App;
Select Cards
CardView supports single and multiple selection modes. Use the selection.mode property to specify the mode.
- import React from 'react';
- import 'devextreme/dist/css/dx.fluent.blue.light.css';
- import CardView, { Selection } from 'devextreme-react/card-view';
- import { employees } from './data.ts';
- function App() {
- return (
- <CardView ... >
- <Selection mode="multiple" />
- </CardView>
- );
- }
- export default App;
Add Pagination
To add pagination to CardView, configure the following settings:
- import React from 'react';
- import 'devextreme/dist/css/dx.fluent.blue.light.css';
- import CardView, { Paging, Pager } from 'devextreme-react/card-view';
- import { employees } from './data.ts';
- function App() {
- return (
- <CardView ... >
- <Paging
- pageSize={3}
- />
- <Pager
- showInfo={true}
- showNavigationButtons={true}
- showPageSizeSelector={true}
- />
- </CardView>
- );
- }
- export default App;
Configure Column Chooser
CardView displays all columns from the columns array. To hide a column, set its visible property to false. Hidden columns appear in the columnChooser. Users can restore hidden columns from it. To enable the column chooser, set the columnChooser.enabled property to true. If a column should not be visible in the column chooser, do not declare it in the columns array.
You can also specify other column chooser settings, such as mode, position, and selection:
- import React from 'react';
- import 'devextreme/dist/css/dx.fluent.blue.light.css';
- import CardView, { ColumnChooser, Position, Selection } from 'devextreme-react/card-view';
- import { employees } from './data.ts';
- function App() {
- return (
- <CardView ... >
- <ColumnChooser
- enabled={true}
- height={340}
- mode="select"
- >
- <Position
- my="right top"
- at="right bottom"
- of=".dx-cardview-column-chooser-button"
- />
- <Selection selectByClick={true} />
- </ColumnChooser>
- </CardView>
- );
- }
- export default App;