Skip to content

Commit fcad9f4

Browse files
Pagination: add Getting Started tutorial (DevExpress#6890)
1 parent 05f5fa6 commit fcad9f4

File tree

11 files changed

+1256
-0
lines changed

11 files changed

+1256
-0
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#cards {
2+
display: flex;
3+
justify-content: center;
4+
flex-wrap: wrap;
5+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<div id="pagination"></div>
2+
<div id="cards"></div>
3+
<div id="load-panel"></div>
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
const total = 100;
2+
const hexCodes = [];
3+
const apiEndpoint = 'https://www.thecolorapi.com/id?hex=';
4+
const cache = new Map();
5+
6+
function fetchData(colorId) {
7+
return new Promise((resolve, reject) => {
8+
if (cache.has(colorId)) {
9+
resolve(cache.get(colorId));
10+
} else {
11+
$.getJSON(apiEndpoint + colorId, (data) => {
12+
const colorData = {
13+
image: data.image.bare,
14+
name: data.name.value,
15+
};
16+
cache.set(colorId, colorData);
17+
resolve(colorData);
18+
}).fail(() => {
19+
reject(new Error(`Error loading color for hex: ${colorId}`));
20+
});
21+
}
22+
});
23+
}
24+
25+
const getRandomPastelColor = () => {
26+
const hue = Math.floor(Math.random() * 360);
27+
const saturation = Math.random() * 0.4 + 0.2;
28+
const brightness = Math.random() * 0.3 + 0.7;
29+
return hsvToHex(hue, saturation, brightness);
30+
};
31+
32+
const hsvToHex = (h, s, v) => {
33+
let r = 0;
34+
let g = 0;
35+
let b = 0;
36+
const i = Math.floor(h / 60);
37+
const f = h / 60 - i;
38+
const p = v * (1 - s);
39+
const q = v * (1 - f * s);
40+
const t = v * (1 - (1 - f) * s);
41+
switch (i % 6) {
42+
case 0: r = v; g = t; b = p; break;
43+
case 1: r = q; g = v; b = p; break;
44+
case 2: r = p; g = v; b = t; break;
45+
case 3: r = p; g = q; b = v; break;
46+
case 4: r = t; g = p; b = v; break;
47+
case 5: r = v; g = p; b = q; break;
48+
}
49+
const toHex = (x) => {
50+
const hex = Math.round(x * 255).toString(16);
51+
return hex.length === 1 ? `0${hex}` : hex;
52+
};
53+
return toHex(r) + toHex(g) + toHex(b);
54+
};
55+
56+
const renderCards = async (pageSize, pageIndex) => {
57+
$('#cards').empty();
58+
const startIndex = (pageIndex - 1) * pageSize;
59+
const endIndex = pageIndex * pageSize;
60+
61+
const hexSubset = hexCodes.slice(startIndex, endIndex);
62+
const promises = hexSubset.map((hex) => fetchData(hex));
63+
try {
64+
const pageColors = await Promise.all(promises);
65+
pageColors.forEach((color) => {
66+
const image = $('<img>').attr({
67+
src: color.image,
68+
alt: color.name,
69+
});
70+
$('#cards').append(image);
71+
});
72+
} catch (error) {
73+
console.error('Error rendering cards:', error);
74+
}
75+
};
76+
77+
$(() => {
78+
for (let i = 0; i < total; i += 1) {
79+
hexCodes.push(getRandomPastelColor());
80+
}
81+
82+
const loadPanel = $('#load-panel')
83+
.dxLoadPanel({
84+
position: {
85+
my: 'top',
86+
at: 'top',
87+
of: '#cards',
88+
},
89+
visible: false,
90+
showIndicator: true,
91+
showPane: true,
92+
hideOnOutsideClick: false,
93+
})
94+
.dxLoadPanel('instance');
95+
96+
const pagination = $('#pagination')
97+
.dxPagination({
98+
showInfo: true,
99+
showNavigationButtons: true,
100+
itemCount: total,
101+
pageIndex: 1,
102+
pageSize: 5,
103+
onOptionChanged: (e) => {
104+
if (e.name === 'pageSize' || e.name === 'pageIndex') {
105+
const pageIndex = pagination.option('pageIndex');
106+
const pageSize = pagination.option('pageSize');
107+
loadPanel.show();
108+
renderCards(pageSize, pageIndex).finally(() => loadPanel.hide());
109+
}
110+
},
111+
})
112+
.dxPagination('instance');
113+
114+
const pageSize = pagination.option('pageSize');
115+
const pageIndex = pagination.option('pageIndex');
116+
loadPanel.show();
117+
renderCards(pageSize, pageIndex).finally(() => loadPanel.hide());
118+
});
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#include tutorials-intro-installationnote
2+
3+
Pagination is a UI component that allows users to navigate through pages and change page size at runtime.
4+
5+
This tutorial explains how to add Pagination to a page and configure the component's core settings. It also covers implementing remote pagination. Colored cards are loaded each time a user switches pages or changes page size. The final result is displayed below:
6+
7+
<div class="simulator-desktop-container" data-view="/Content/Applications/24_2/GettingStartedWith/Pagination/index.html, /Content/Applications/24_2/GettingStartedWith/Pagination/index.js, /Content/Applications/24_2/GettingStartedWith/Pagination/index.css"></div>
8+
9+
Each section in this tutorial describes a single configuration step. You can also find the full source code in the following GitHub repository:
10+
11+
#include btn-open-github with {
12+
href: "https://github.com/DevExpress-Examples/devextreme-getting-started-with-pagination"
13+
}
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
---
2+
##### jQuery
3+
[Add DevExtreme to your jQuery application](/concepts/58%20jQuery%20Components/05%20Add%20DevExtreme%20to%20a%20jQuery%20Application/00%20Add%20DevExtreme%20to%20a%20jQuery%20Application.md '/Documentation/Guide/jQuery_Components/Add_DevExtreme_to_a_jQuery_Application/') and use the following code to create a Pagination component:
4+
5+
<!-- tab: index.js -->
6+
$(function() {
7+
$("#pagination").dxPagination({ });
8+
});
9+
10+
<!-- tab: index.html -->
11+
<html>
12+
<head>
13+
<!-- ... -->
14+
<script type="text/javascript" src="https://code.jquery.com/jquery-3.5.1.min.js"></script>
15+
<link rel="stylesheet" href="https://cdn3.devexpress.com/jslib/minor_24_2/css/dx.light.css">
16+
<script type="text/javascript" src="https://cdn3.devexpress.com/jslib/minor_24_2/js/dx.all.js"></script>
17+
<script type="text/javascript" src="index.js"></script>
18+
</head>
19+
<body>
20+
<div id="pagination"></div>
21+
</body>
22+
</html>
23+
24+
25+
##### Angular
26+
27+
[Add DevExtreme to your Angular application](/concepts/40%20Angular%20Components/10%20Getting%20Started/03%20Add%20DevExtreme%20to%20an%20Angular%20CLI%20Application '/Documentation/Guide/Angular_Components/Getting_Started/Add_DevExtreme_to_an_Angular_CLI_Application/') and use the following code to create a Pagination component:
28+
29+
<!-- tab: app.component.html -->
30+
<dx-pagination></dx-pagination>
31+
32+
<!-- tab: app.component.ts -->
33+
import { Component } from '@angular/core';
34+
35+
@Component({
36+
selector: 'app-root',
37+
templateUrl: './app.component.html',
38+
styleUrls: ['./app.component.css']
39+
})
40+
export class AppComponent {
41+
42+
}
43+
44+
<!-- tab: app.module.ts -->
45+
import { BrowserModule } from '@angular/platform-browser';
46+
import { NgModule } from '@angular/core';
47+
import { AppComponent } from './app.component';
48+
49+
import { DxPaginationModule } from 'devextreme-angular';
50+
51+
@NgModule({
52+
declarations: [
53+
AppComponent
54+
],
55+
imports: [
56+
BrowserModule,
57+
DxPaginationModule
58+
],
59+
providers: [ ],
60+
bootstrap: [AppComponent]
61+
})
62+
export class AppModule { }
63+
64+
##### Vue
65+
66+
[Add DevExtreme to your Vue application](/concepts/55%20Vue%20Components/05%20Add%20DevExtreme%20to%20a%20Vue%20Application/00%20Add%20DevExtreme%20to%20a%20Vue%20Application.md '/Documentation/Guide/Vue_Components/Add_DevExtreme_to_a_Vue_Application/') and use the following code to create a Pagination component:
67+
68+
<!-- tab: App.vue -->
69+
<template>
70+
<DxPagination />
71+
</template>
72+
73+
<script setup lang="ts">
74+
import 'devextreme/dist/css/dx.light.css';
75+
import { DxPagination } from 'devextreme-vue/pagination';
76+
</script>
77+
78+
##### React
79+
80+
[Add DevExtreme to your React application](/concepts/50%20React%20Components/05%20Add%20DevExtreme%20to%20a%20React%20Application/00%20Add%20DevExtreme%20to%20a%20React%20Application.md '/Documentation/Guide/React_Components/Add_DevExtreme_to_a_React_Application/') and use the following code to create a Pagination component:
81+
82+
<!-- tab: App.tsx -->
83+
import React from 'react';
84+
import 'devextreme/dist/css/dx.light.css';
85+
import { Pagination } from 'devextreme-react/pagination';
86+
87+
function App(): JSX.Element {
88+
return (
89+
<Pagination />
90+
);
91+
}
92+
93+
export default App;
94+
95+
---
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
This tutorial step guides you through the basic Pagination setup.
2+
3+
Specify the following settings:
4+
5+
* [itemCount](/Documentation/ApiReference/UI_Components/dxPagination/Configuration/#itemCount) sets the total number of items. Pagination does not function properly without this setting.
6+
* [pageIndex](/Documentation/ApiReference/UI_Components/dxPagination/Configuration/#pageIndex) sets the initial page to display. This tutorial sets **pageIndex** to 3 (the default value is 1).
7+
* [allowedPageSizes](/Documentation/ApiReference/UI_Components/dxPagination/Configuration/#allowedPageSizes) specifies page sizes available to users. Modify this list as needed. Include `'all'` to allow users to display all items on one page. This tutorial uses the default value: `[5, 10]`.
8+
* [pageSize](/Documentation/ApiReference/UI_Components/dxPagination/Configuration/#pageSize) specifies the initial page size.
9+
10+
The following code snippet demonstrates how to apply the aforementioned settings:
11+
12+
---
13+
##### jQuery
14+
15+
<!-- tab: index.js -->
16+
const total = 100;
17+
$(() => {
18+
const pagination = $('#pagination')
19+
.dxPagination({
20+
showInfo: true,
21+
showNavigationButtons: true,
22+
itemCount: total,
23+
pageIndex: 3,
24+
pageSize: 5,
25+
})
26+
.dxPagination('instance');
27+
});
28+
29+
##### Angular
30+
31+
<!-- tab: app.component.html -->
32+
<dx-pagination
33+
[showInfo]="true"
34+
[showNavigationButtons]="true"
35+
[itemCount]="total"
36+
[pageIndex]="pageIndex"
37+
[pageSize]="pageSize"
38+
>
39+
</dx-pagination>
40+
41+
<!-- tab: app.component.ts -->
42+
import { Component } from '@angular/core';
43+
44+
@Component({
45+
selector: 'app-root',
46+
templateUrl: './app.component.html',
47+
styleUrls: ['./app.component.css']
48+
})
49+
export class AppComponent {
50+
total = 100;
51+
pageIndex = 3;
52+
pageSize = 5;
53+
}
54+
55+
##### Vue
56+
57+
<!-- tab: App.vue -->
58+
<template>
59+
<DxPagination
60+
:show-info="true"
61+
:show-navigation-buttons="true"
62+
v-model:page-index="pageIndex"
63+
v-model:page-size="pageSize"
64+
:item-count="total"
65+
/>
66+
</template>
67+
68+
<script setup lang="ts">
69+
// ...
70+
import { ref } from 'vue';
71+
72+
const total = 100;
73+
const pageSize = ref(5);
74+
const pageIndex = ref(3);
75+
</script>
76+
77+
##### React
78+
79+
<!-- tab: App.tsx -->
80+
import React, { useState } from 'react';
81+
// ...
82+
const total = 100;
83+
84+
function App(): JSX.Element {
85+
const [pageSize, setPageSize] = useState<number>(5);
86+
const [pageIndex, setPageIndex] = useState<number>(3);
87+
return (
88+
<Pagination
89+
showInfo={true}
90+
showNavigationButtons={true}
91+
pageIndex={pageIndex}
92+
pageSize={pageSize}
93+
itemCount={total}
94+
/>
95+
);
96+
}
97+
98+
export default App;
99+
100+
---

0 commit comments

Comments
 (0)