Skip to content

Commit

Permalink
allow usage of single numbers with show and hide, fixes #373
Browse files Browse the repository at this point in the history
  • Loading branch information
johanneswilm committed Apr 10, 2024
1 parent 15971cf commit c2debea
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 27 deletions.
28 changes: 12 additions & 16 deletions docs/documentation/columns-API.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,10 @@ let columns = datatable.columns;
columns.remove(3);

// Remove the 1st and 2nd column
columns.remove([0,1]);
columns.remove([0, 1]);

// Remove the last column
columns.remove(datatable.headings.length - 1);

```

---
Expand All @@ -97,7 +96,7 @@ Hides the selected column(s). The columns will not be visible and will be omitte

```javascript
// Hide the first and second columns
columns.hide([0,1]);
columns.hide([0, 1]);
```

---
Expand All @@ -106,10 +105,9 @@ columns.hide([0,1]);

Shows the selected column(s) (if hidden). The columns will be visible and will be included in search results and exported data.


```javascript
// Show the first and second columns
columns.show([0,1]);
columns.show([0, 1]);
```

---
Expand All @@ -127,16 +125,15 @@ let columns = datatable.columns;
columns.hide(3);

// Check visiblilty
columns.visible(3) // returns false
columns.visible(3); // returns false

or
or;

columns.visible([0,1,2,3]) // returns [true, true, true, false]
columns.visible([0, 1, 2, 3]); // returns [true, true, true, false]

or

columns.visible() // returns [true, true, true, false, true]
or;

columns.visible(); // returns [true, true, true, false, true]
```

---
Expand All @@ -159,8 +156,7 @@ Swap th position of two columns. Just pass an array of 2 integers representing t
let columns = datatable.columns;

// Swap the 1st and 6th columns
columns.swap([0,5]);

columns.swap([0, 5]);
```

---
Expand All @@ -170,14 +166,14 @@ columns.swap([0,5]);
Order the columns based on the given order. Just pass an array of column indexes in the order you require:

### Original order
![Original order](http://i.imgur.com/OK5DoGs.png)

![Original order](http://i.imgur.com/OK5DoGs.png)

```javascript
// Reorder the columns
datatable.columns.order([1,3,4,2,0]);
datatable.columns.order([1, 3, 4, 2, 0]);
```


### Result

![Original order](http://i.imgur.com/kNGEgpT.png)
28 changes: 17 additions & 11 deletions src/columns.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,10 @@ export class Columns {
/**
* Hide columns
*/
hide(columns: number[]) {
hide(columns: number | number[]) {
if (!Array.isArray(columns)) {
columns = [columns]
}
if (!columns.length) {
return
}
Expand All @@ -99,7 +102,10 @@ export class Columns {
/**
* Show columns
*/
show(columns: number[]) {
show(columns: number | number[]) {
if (!Array.isArray(columns)) {
columns = [columns]
}
if (!columns.length) {
return
}
Expand Down Expand Up @@ -212,16 +218,16 @@ export class Columns {
/**
* Remove column(s)
*/
remove(columns: number[]) {
if (Array.isArray(columns)) {
this.dt.data.headings = this.dt.data.headings.filter((_heading: headerCellType, index: number) => !columns.includes(index))
this.dt.data.data.forEach(
(row: dataRowType) => (row.cells = row.cells.filter((_cell: cellType, index: number) => !columns.includes(index)))
)
this.dt.update(true)
} else {
return this.remove([columns])
remove(columns: number | number[]) {
if (!Array.isArray(columns)) {
columns = [columns]
}

this.dt.data.headings = this.dt.data.headings.filter((_heading: headerCellType, index: number) => !columns.includes(index))
this.dt.data.data.forEach(
(row: dataRowType) => (row.cells = row.cells.filter((_cell: cellType, index: number) => !columns.includes(index)))
)
this.dt.update(true)
}

/**
Expand Down

0 comments on commit c2debea

Please sign in to comment.