Skip to content

Commit 8397a7f

Browse files
authored
chore: Docs for clear rows (#132)
1 parent 68a2eb9 commit 8397a7f

6 files changed

Lines changed: 12192 additions & 4 deletions

File tree

cypress/e2e/page_tests.cy.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ describe("Testing Each Documentation Page", () => {
1515
},
1616
{
1717
title: "Adding Rows",
18-
headlines: ["One row at a time", "Batch Row Adding", "Chained Row adding"]
18+
headlines: ["One row at a time", "Batch Row Adding", "Chained Row adding", "Clearing Rows"]
1919
},
2020
{
2121
title: "Row Dividers",
@@ -161,4 +161,4 @@ describe("Testing Each Documentation Page", () => {
161161
}
162162
});
163163
});
164-
});
164+
});

cypress/e2e/url_tests.cy.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ describe("Testing Each Documentation Page", () => {
2020
{
2121
url: "/docs/doc-adding-rows",
2222
title: "Adding Rows",
23-
headlines: ["One row at a time", "Batch Row Adding", "Chained Row adding"]
23+
headlines: ["One row at a time", "Batch Row Adding", "Chained Row adding", "Clearing Rows"]
2424
},
2525
{
2626
url: "/docs/doc-row-divider",
@@ -159,4 +159,4 @@ describe("Testing Each Documentation Page", () => {
159159
}
160160
});
161161
});
162-
});
162+
});

docs/api/table-methods.md

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,58 @@ table.addRows([
9898
]);
9999
```
100100

101+
### clearRows
102+
103+
### `clearRows(): Table`
104+
105+
Removes all rows from the table while keeping the table columns and configuration.
106+
107+
Use this when the table structure should stay the same, but the row data needs to be replaced or reset.
108+
109+
**Parameters:** None
110+
111+
**Returns:** `Table` instance (for method chaining)
112+
113+
**Example:**
114+
```javascript
115+
const table = new Table(["id", "status"]);
116+
117+
table.addRows([
118+
{ id: 1, status: "Queued" },
119+
{ id: 2, status: "Running" }
120+
]);
121+
122+
table.clearRows();
123+
table.addRow({ id: 3, status: "Done" });
124+
table.printTable();
125+
```
126+
127+
**Chaining:**
128+
```javascript
129+
const table = new Table(["id", "status"])
130+
.addRows([
131+
{ id: 1, status: "Queued" },
132+
{ id: 2, status: "Running" }
133+
])
134+
.clearRows()
135+
.addRows([{ id: 3, status: "Done" }]);
136+
```
137+
138+
**Clearing Constructor Rows:**
139+
```javascript
140+
const table = new Table({
141+
columns: [{ name: "task" }, { name: "state" }],
142+
rows: [
143+
{ task: "fetch", state: "pending" },
144+
{ task: "build", state: "running" }
145+
]
146+
});
147+
148+
table.clearRows().addRow({ task: "deploy", state: "done" });
149+
```
150+
151+
After rows are cleared, `render()` and `printTable()` keep the existing table headers and render an empty body until new rows are added.
152+
101153
## Column Management Methods
102154

103155
### addColumn

docs/doc-adding-rows.md

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,3 +66,43 @@ p.addRow({ Weight: "1kg", text: "red wine", value: 10.212 })
6666
.addRow({ Weight: 3, text: "rosa hemd wie immer", value: 100 });
6767
p.printTable();
6868
```
69+
70+
## Clearing Rows
71+
72+
Use `clearRows()` when you want to remove the current row data and reuse the same table instance. The table keeps its columns and configuration, so you can add fresh rows without rebuilding the table.
73+
74+
`clearRows()` returns the table instance, so it can be used in a method chain.
75+
76+
```javascript
77+
import { Table } from 'console-table-printer';
78+
79+
const p = new Table(["id", "status"])
80+
.addRows([
81+
{ id: 1, status: "Queued" },
82+
{ id: 2, status: "Running" },
83+
])
84+
.clearRows()
85+
.addRows([{ id: 3, status: "Done" }]);
86+
87+
p.printTable();
88+
```
89+
90+
Rows added during table creation can also be cleared.
91+
92+
```javascript
93+
import { Table } from 'console-table-printer';
94+
95+
const p = new Table({
96+
columns: [{ name: "task" }, { name: "state" }],
97+
rows: [
98+
{ task: "fetch", state: "pending" },
99+
{ task: "build", state: "running" },
100+
],
101+
});
102+
103+
p.clearRows().addRow({ task: "deploy", state: "done" });
104+
105+
p.printTable();
106+
```
107+
108+
After calling `clearRows()`, rendering the table shows the existing headers with an empty body until new rows are added.

docs/doc-table-instance-creation.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ new Table({
9393

9494
- `addRow(rowObjet, options)` adding single row.
9595
- `addRows(rowObjects, options)` adding multiple rows. array of row object. This case options will be applied to all the objects in row
96+
- `clearRows()` removes all rows while keeping the table columns and options. This can be chained
9697
- `addColumn(columnObject)` adding single column
9798
- `addColumns(columnObjects)` adding multiple columns
9899
- `printTable()` Prints the table on your console

0 commit comments

Comments
 (0)