Skip to content

Commit 39ec575

Browse files
authored
Persist select when pagination=remote (#5929)
1 parent 681167a commit 39ec575

File tree

4 files changed

+265
-28
lines changed

4 files changed

+265
-28
lines changed

panel/models/tabulator.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ class SelectionEvent(ModelEvent):
5656

5757
event_name = 'selection-change'
5858

59-
def __init__(self, model, indices, selected):
59+
def __init__(self, model, indices, selected, flush):
6060
""" Selection Event
6161
6262
Parameters
@@ -67,14 +67,17 @@ def __init__(self, model, indices, selected):
6767
A list of changed indices selected/deselected rows.
6868
selected : bool
6969
If true the rows were selected, if false they were deselected.
70+
flush : bool
71+
Whether the current selection should be emptied before adding the new indices.
7072
"""
7173
self.indices = indices
7274
self.selected = selected
75+
self.flush = flush
7376
super().__init__(model=model)
7477

7578
def __repr__(self):
7679
return (
77-
f'{type(self).__name__}(indices={self.indices}, selected={self.selected})'
80+
f'{type(self).__name__}(indices={self.indices}, selected={self.selected}, flush={self.flush})'
7881
)
7982

8083

panel/models/tabulator.ts

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,12 @@ export class CellClickEvent extends ModelEvent {
4444
}
4545

4646
export class SelectionEvent extends ModelEvent {
47-
constructor(readonly indices: number[], readonly selected: boolean) {
47+
constructor(readonly indices: number[], readonly selected: boolean, readonly flush: boolean = false) {
4848
super()
4949
}
5050

5151
protected get event_values(): Attrs {
52-
return {model: this.origin, indices: this.indices, selected: this.selected}
52+
return {model: this.origin, indices: this.indices, selected: this.selected, flush: this.flush}
5353
}
5454

5555
static {
@@ -1070,6 +1070,28 @@ export class DataTabulatorView extends HTMLBoxView {
10701070
let indices: number[] = []
10711071
const selected = this.model.source.selected
10721072
const index: number = row._row.data._index
1073+
1074+
if (this.model.pagination === 'remote') {
1075+
const includes = this.model.source.selected.indices.indexOf(index) == -1
1076+
const flush = !(e.ctrlKey || e.metaKey || e.shiftKey)
1077+
if (e.shiftKey && selected.indices.length) {
1078+
const start = selected.indices[selected.indices.length-1]
1079+
if (index>start) {
1080+
for (let i = start; i<=index; i++)
1081+
indices.push(i)
1082+
} else {
1083+
for (let i = start; i>=index; i--)
1084+
indices.push(i)
1085+
}
1086+
} else {
1087+
indices.push(index)
1088+
}
1089+
this._selection_updating = true
1090+
this.model.trigger_event(new SelectionEvent(indices, includes, flush))
1091+
this._selection_updating = false
1092+
return
1093+
}
1094+
10731095
if (e.ctrlKey || e.metaKey) {
10741096
indices = [...this.model.source.selected.indices]
10751097
} else if (e.shiftKey && selected.indices.length) {
@@ -1124,11 +1146,11 @@ export class DataTabulatorView extends HTMLBoxView {
11241146
let deselected_indices = deselected.map((x: any) => x._row.data._index)
11251147
if (selected_indices.length > 0) {
11261148
this._selection_updating = true
1127-
this.model.trigger_event(new SelectionEvent(selected_indices, selected=true))
1149+
this.model.trigger_event(new SelectionEvent(selected_indices, true, false))
11281150
}
11291151
if (deselected_indices.length > 0) {
11301152
this._selection_updating = true
1131-
this.model.trigger_event(new SelectionEvent(deselected_indices, selected=false))
1153+
this.model.trigger_event(new SelectionEvent(deselected_indices, false, false))
11321154
}
11331155
} else {
11341156
const indices: number[] = data.map((row: any) => row._index)

0 commit comments

Comments
 (0)