Skip to content

Commit 1fa8467

Browse files
committed
Fix selected rows
1 parent d8c25a0 commit 1fa8467

File tree

2 files changed

+42
-37
lines changed

2 files changed

+42
-37
lines changed

packages/itables_anywidget/js/widget.ts

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -58,13 +58,10 @@ function render({ model, el }: RenderContext<WidgetModel>) {
5858
let selected_rows = model.get('selected_rows');
5959
let full_row_count = model.get('full_row_count');
6060
let data_row_count = model.get('data').length;
61-
if (data_row_count < full_row_count) {
62-
let bottom_half = data_row_count / 2;
63-
let top_half = full_row_count - bottom_half;
64-
selected_rows = selected_rows.filter(i => i >= 0 && i < full_row_count && (i < bottom_half || i >= top_half)).map(
65-
i => (i < bottom_half) ? i : i - full_row_count + data_row_count);
66-
}
67-
61+
let bottom_half = data_row_count / 2;
62+
let top_half = full_row_count - bottom_half;
63+
selected_rows = Array.from(selected_rows.filter(i => i >= 0 && i < full_row_count && (i < bottom_half || i >= top_half)).map(
64+
i => (i < bottom_half) ? i : i - full_row_count + data_row_count));
6865
dt.rows().deselect();
6966
dt.rows(selected_rows).select();
7067

@@ -107,11 +104,9 @@ function render({ model, el }: RenderContext<WidgetModel>) {
107104
// We convert them back to the full table
108105
let full_row_count = model.get('full_row_count');
109106
let data_row_count = model.get('data').length;
110-
if (data_row_count < full_row_count) {
111-
let bottom_half = data_row_count / 2;
112-
selected_rows = selected_rows.map(
113-
i => (i < bottom_half ? i : i + full_row_count - data_row_count));
114-
}
107+
let bottom_half = data_row_count / 2;
108+
selected_rows = Array.from(selected_rows.map(
109+
i => (i < bottom_half ? i : i + full_row_count - data_row_count)));
115110

116111
model.set('selected_rows', selected_rows);
117112
model.save_changes();

packages/itables_for_streamlit/src/index.tsx

Lines changed: 35 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,11 @@ function onRender(event: Event): void {
1313
var other_args = (event as CustomEvent<RenderData>).detail.args.other_args
1414
var dt_args = (event as CustomEvent<RenderData>).detail.args.dt_args
1515

16-
if(other_args.downsampling_warning) {
17-
dt_args["fnInfoCallback"] = function (oSettings:any, iStart:number, iEnd:number, iMax:number, iTotal:number, sPre:string) { return sPre + ' (' +
18-
other_args.downsampling_warning + ')' }
16+
if (other_args.downsampling_warning) {
17+
dt_args["fnInfoCallback"] = function (oSettings: any, iStart: number, iEnd: number, iMax: number, iTotal: number, sPre: string) {
18+
return sPre + ' (' +
19+
other_args.downsampling_warning + ')'
20+
}
1921
}
2022

2123
// As we can't pass the dt_args other than in the
@@ -29,34 +31,42 @@ function onRender(event: Event): void {
2931
table.setAttribute('style', other_args.style)
3032

3133
dt = new DataTable(table, dt_args)
32-
if(other_args.caption) {
34+
if (other_args.caption) {
3335
dt.caption(other_args.caption)
3436
}
3537

36-
function export_selected_rows() {
37-
let selected_rows:Array<number> = Array.from(dt.rows({ selected: true }).indexes());
38+
let full_row_count: number = other_args.full_row_count;
39+
let data_row_count: number = dt_args.data.length;
40+
41+
let bottom_half = data_row_count / 2;
42+
let top_half = full_row_count - bottom_half;
3843

39-
let full_row_count:number = other_args.full_row_count;
40-
let data_row_count:number = dt_args.data.length;
44+
// The model selected rows are for the full table, so
45+
// we map them to the actual data
46+
let org_selected_rows = Array.from(
47+
other_args.selected_rows
48+
.filter((i: number) => i >= 0 && i < full_row_count && (i < bottom_half || i >= top_half))
49+
.map((i: number) => (i < bottom_half) ? i : i - full_row_count + data_row_count));
50+
dt.rows(org_selected_rows).select();
51+
52+
function export_selected_rows() {
53+
let selected_rows: Array<number> = Array.from(dt.rows({ selected: true }).indexes());
4154

4255
// Here the selected rows are for the datatable.
43-
// We convert them back to the full table
44-
if (data_row_count < full_row_count) {
45-
let bottom_half = data_row_count / 2;
46-
selected_rows = selected_rows.map(
47-
(x:number, i:number) => (x < bottom_half ? x : x + full_row_count - data_row_count));
48-
}
49-
50-
Streamlit.setComponentValue({selected_rows});
51-
};
52-
53-
dt.on('select', function (e: any, dt: any, type: any, indexes: any) {
54-
export_selected_rows();
55-
});
56-
57-
dt.on('deselect', function (e: any, dt: any, type: any, indexes: any) {
58-
export_selected_rows();
59-
});
56+
// We convert them back to the full table
57+
selected_rows = Array.from(selected_rows.map(
58+
(i: number) => (i < bottom_half ? i : i + full_row_count - data_row_count)));
59+
60+
Streamlit.setComponentValue({ selected_rows });
61+
};
62+
63+
dt.on('select', function (e: any, dt: any, type: any, indexes: any) {
64+
export_selected_rows();
65+
});
66+
67+
dt.on('deselect', function (e: any, dt: any, type: any, indexes: any) {
68+
export_selected_rows();
69+
});
6070

6171
// we recalculate the height
6272
Streamlit.setFrameHeight()

0 commit comments

Comments
 (0)