Skip to content

Commit 0c05efb

Browse files
committed
[update] guides and api of the grid modules
1 parent e91bb3b commit 0c05efb

19 files changed

+391
-243
lines changed
52.5 KB
Loading

docs/grid/api/blockselection/afterblockhandleapply_event.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ The callback of the event is called with the following parameters:
3232
- `event` - the browser event: `MouseEvent` or `TouchEvent`
3333

3434
@example:
35-
grid.block.events.on("afterBlockHandleApply", (startCell, endCell, dir) => {
35+
grid.block.events.on("afterBlockHandleApply", (startCell, endCell, dragDirection) => {
3636
console.log("Handle applied:");
3737
});
3838

docs/grid/api/blockselection/beforeblockhandleapply_event.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ The callback of the event is called with the following parameters:
3535
Return `false` to cancel the action; otherwise, `true`.
3636

3737
@example:
38-
grid.block.events.on("beforeBlockHandleApply", (startCell, endCell, dir) => {
39-
if (dir === "right" | dir === "left") {
38+
grid.block.events.on("beforeBlockHandleApply", (startCell, endCell, dragDirection) => {
39+
if (dragDirection === "right" | dragDirection === "left") {
4040
console.log("Horizontal change canceled");
4141
return false;
4242
}

docs/grid/api/blockselection/blockselectionvalidate_event.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ const grid = new dhx.Grid("grid_container", {
4343
blockSelection: true
4444
});
4545

46-
grid.block.events.on("blockSelectionValidate", (cell, handle, event) => {
46+
grid.block.events.on("blockSelectionValidate", (validateCell, handle, event) => {
4747
if (cell.column.id === "b") {
4848
console.log("Selection cannot start from column B");
4949
return false;

docs/grid/api/blockselection/disable_method.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ description: You can explore the disable method of Grid block selection in the d
66

77
# disable()
88

9-
@short: disables the BlockSelection module and resets processing of block selection
9+
@short: disables the `BlockSelection` module and resets processing of block selection
1010

1111
@signature: {'disable(): void;'}
1212

docs/grid/api/blockselection/enable_method.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ description: You can explore the enable method of Grid block selection in the do
66

77
# enable()
88

9-
@short: enables the BlockSelection module and activates the capabilities of block selection in Grid
9+
@short: enables the `BlockSelection` module and activates the capabilities of block selection in Grid
1010

1111
@signature: {'enable(): void;'}
1212

docs/grid/api/grid_blockselection_config.md

Lines changed: 92 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ The `blockSelection` property can be set in two ways:
6868
</tr>
6969
<tr>
7070
<td><b>mode</b></td>
71-
<td>the operating mode of the module:<ul><li><i>"range"</i> - managed through the Range module</li><li><i>"manual"</i> - managed through the manual control</li></ul></td>
71+
<td>the operating mode of the module:<ul><li><i>"range"</i> - managed through the <a href="../../usage_rangeselection/">`RangeSelection` module</a></li><li><i>"manual"</i> - managed through the manual control</li></ul></td>
7272
</tr>
7373
<tr>
7474
<td><b>handle</b></td>
@@ -82,12 +82,12 @@ The `blockSelection` property can be set in two ways:
8282
</table>
8383

8484
:::note
85-
By default, the `blockSelection` property is set to `false`. When `blockSelection` is set to `true` or the module is set to the "range" mode, the **Range** module is initialized.
85+
By default, the `blockSelection` property is set to `false`. When `blockSelection` is set to `true` or the module is set to the "range" mode, the [`RangeSelection`](grid/usage_rangeselection.md) module is initialized.
8686
:::
8787

88+
This example demonstrates configuring the module with the handle disabled and the "range" mode enabled:
8889

89-
~~~jsx
90-
// this example demonstrates configuring the module with the handle disabled and the "range" mode enabled
90+
~~~jsx {10-13}
9191
const grid = new dhx.Grid("grid_container", {
9292
columns: [
9393
{ id: "a", header: [{ text: "A" }] },
@@ -98,46 +98,105 @@ const grid = new dhx.Grid("grid_container", {
9898
{ id: "2", a: "A2", b: "B2" },
9999
],
100100
blockSelection: {
101-
mode: "range",
102-
handle: false,
101+
mode: "range", // setting the "range" mode
102+
handle: false // the handle is disabled
103103
}
104104
});
105105
~~~
106106

107+
This example demonstrates configuring the handle and its behavior:
108+
107109
~~~jsx
108-
// this example demonstrates configuring the handle and its behavior
110+
/* Define grid columns configuration */
111+
const columns = [
112+
{ id: "productId", header: [{ text: "Product ID" }] }, // Column for the unique product identifier
113+
{ id: "productName", header: [{ text: "Product Name" }] }, // Column for the product name
114+
{ id: "category", header: [{ text: "Category" }] }, // Column for the product category
115+
{ id: "receivedDate", header: [{ text: "Received Date" }], type: "date", dateFormat: "%d.%m.%Y" }, // Date column with the specified format
116+
{ id: "stock", header: [{ text: "Stock" }], type: "number" }, // Numeric column for the stock quantity
117+
{ id: "price", header: [{ text: "Price" }], type: "number", numberMask: { prefix: "$" } } // Numeric column for the price with the dollar prefix
118+
];
119+
120+
/* Initialize DHTMLX Grid with the specified configuration */
109121
const grid = new dhx.Grid("grid_container", {
110-
columns: [
111-
{ id: "a", header: [{ text: "A" }] },
112-
{ id: "b", header: [{ text: "B" }] },
113-
],
114-
data: [
115-
{ id: "1", a: "A1", b: "B1" },
116-
{ id: "2", a: "A2", b: "B2" },
117-
],
122+
columns,
123+
data,
124+
autoWidth: true,
125+
history: true, // Enable history tracking for undo/redo
118126
blockSelection: {
119-
mode: "range",
120127
handle: {
121-
allowAxis: "x", // the handle movement is restricted by the "x" axis
122-
handler: ({ array, range, grid, cell, index }) => {
123-
if (array.length <= 1) {
124-
return;
125-
}
126-
const firstCell = range[0];
127-
// the copied cells will have the "-copied" suffix
128-
const value = firstCell.row[firstCell.column.id] + (index ? "-copied" : "");
129-
grid.data.update(
130-
cell.row.id,
131-
{ [cell.column.id]: value },
132-
index < array.length - 1 // the silent mode for all the cells except for the last cell
133-
);
134-
}
135-
}
136-
}
128+
allowAxis: "y", // Allow selection along the y-axis (rows)
129+
handler: blockSelectionHandler, // Custom handler for block selection
130+
},
131+
},
137132
});
133+
134+
/* Set initial selection range for the grid */
135+
grid.range.setRange({
136+
xStart: "productId", // Start selection at the "productId" column
137+
yEnd: grid.data.getId(0), // End selection at the first row
138+
});
139+
140+
/* Initialize objects to store initial values and column indices */
141+
let initValues = {}; // Store initial values for each column
142+
let columnIndex = {}; // Track index increments for each column
143+
144+
/* The handler function for block selection events */
145+
function blockSelectionHandler({ cell, array, index, grid }) {
146+
// Reset tracking objects if this is the first cell in the selection
147+
if (!index) {
148+
initValues = {};
149+
columnIndex = {};
150+
}
151+
const columnId = cell.column.id;
152+
// Initialize values for a new column if not already set
153+
if (!initValues[columnId]) {
154+
initValues[columnId] = cell.row[columnId]; // Store the initial cell value
155+
columnIndex[columnId] = 0; // Initialize the index counter
156+
return { prev: initValues[columnId], current: initValues[columnId] }; // Return unchanged values
157+
}
158+
159+
// Increment column index for the current column
160+
const colIndex = columnIndex[columnId] += 1;
161+
const initValue = initValues[columnId]; // Get the initial value for a column
162+
let prev = current = cell.row[columnId]; // Set the default previous and current values
163+
164+
// Modify the current value based on the column type
165+
switch (cell.column.type) {
166+
case "number":
167+
current = initValue + colIndex * 10; // Increment the number by 10 per row
168+
break;
169+
case "date":
170+
// Parse ISO date and increment the day by colIndex
171+
const [year, month, day] = initValue.split("-");
172+
current = new Date(Number(year), Number(month) - 1, Number(day) + colIndex).toISOString();
173+
break;
174+
default:
175+
current = initValue; // Keep the default value for other types
176+
break;
177+
}
178+
179+
// Custom formatting for specific columns
180+
if (columnId === "productId") {
181+
// Generate a new product ID with the format P00N
182+
current = `P00${parseInt(initValue.replace(/\D/g, "")) + colIndex}`;
183+
}
184+
if (columnId === "category") {
185+
// Append the index in parentheses to the category
186+
current = `${current} (${colIndex})`;
187+
}
188+
189+
// Create the history object for undo/redo
190+
const history = { prev, current };
191+
// Update grid data with the new value
192+
grid.data.update(cell.row.id, { [columnId]: current },
193+
index < array.length - 1 // Continue updating if it isn't the last cell in selection
194+
);
195+
return history; // Return the history for tracking
196+
}
138197
~~~
139198

140-
**Related sample:** [Grid. BlockSelection with handle configuration](https://snippet.dhtmlx.com/8gx20g1d)
199+
**Related sample:** [Grid. BlockSelection. Work with the handle configuration](https://snippet.dhtmlx.com/8kttktiy)
141200

142201
**Related article:** [Managing block selection in Grid](grid/configuration.md/#managing-block-selection-in-grid)
143202

docs/grid/api/grid_clipboard_config.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,4 +76,6 @@ const grid = new dhx.Grid("grid_container", {
7676

7777
**Related article:** [Clipboard](grid/configuration.md/#clipboard)
7878

79+
**Related sample**: [Grid. Clipboard. Financial data with formatted copy/paste](https://snippet.dhtmlx.com/1fnkhwm0)
80+
7981
@changelog: added in v9.2

docs/grid/api/grid_dragpanel_config.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,4 +87,6 @@ const grid = new dhx.Grid("grid_container", {
8787

8888
**Related article:** [Using the DragPanel module](grid/configuration.md/#using-the-dragpanel-module)
8989

90+
**Related sample:** [Grid. DragPanel. Initialization](https://snippet.dhtmlx.com/oyk02cr6)
91+
9092
@changelog: added in v9.2

0 commit comments

Comments
 (0)