Skip to content

Commit 1637ae7

Browse files
committed
Don't report value change on sort, filter, etc.
Also refactored getValue for readability.
1 parent 40f144e commit 1637ae7

File tree

1 file changed

+28
-18
lines changed

1 file changed

+28
-18
lines changed

inst/shiny/gtShiny.js

Lines changed: 28 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,20 @@ $.extend(gtShinyBinding, {
3232
if (reactableElement) {
3333
var rows = reactableElement.querySelectorAll('.rt-tr');
3434
if (rows.length > 0) {
35-
if (!reactableElement.__clickListenerAdded) {
36-
reactableElement.addEventListener('click', function() {
37-
el.__clickFlag = true;
35+
// Listener so we know it's an actual click, not another state change.
36+
body_rows = reactableElement.querySelectorAll('.rt-tbody .rt-tr');
37+
if (body_rows.length > 0) {
38+
body_rows.forEach(function(row) {
39+
if (!row.__clickListenerAdded) {
40+
row.addEventListener('click', function() {
41+
el.__clickFlag = true;
42+
});
43+
row.__clickListenerAdded = true;
44+
}
3845
});
39-
reactableElement.__clickListenerAdded = true;
4046
}
4147

48+
// State change listener, so we fire getValue *after* the state updates.
4249
if (!reactableElement.__reactableStateChangeListener) {
4350
reactableElement.__reactableStateChangeListener = function() {
4451
$(el).trigger('change.gtShiny');
@@ -90,24 +97,27 @@ $.extend(gtShinyBinding, {
9097
this.initializeListener(el);
9198
return; // Table is reloading or not fully initialized
9299
}
100+
93101
var reactableElement = this.getReactable(el);
94-
if (reactableElement) {
95-
var selectedRows = Reactable.getState(reactableElement.id).selected;
96-
if (selectedRows === undefined) {
97-
return null;
98-
} else if (selectedRows.length === 0) {
99-
if (el.__clickFlag) {
100-
el.__clickFlag = false;
101-
return [0]; // [0] if nothing is selected due to user click
102-
} else {
103-
return null; // null if table is initializing or reloading
104-
}
105-
} else {
102+
if (!reactableElement) {
103+
return; // Initialization is finishing, state will report when finished.
104+
}
105+
106+
var selectedRows = Reactable.getState(reactableElement.id).selected;
107+
if (selectedRows === undefined) {
108+
return; // Initialization is finishing, state will report when finished.
109+
}
110+
111+
if (selectedRows.length === 0) {
112+
if (el.__clickFlag) {
106113
el.__clickFlag = false;
107-
return selectedRows.map(function(row) { return row + 1; });
114+
return [0]; // [0] if nothing is selected due to user click
108115
}
116+
return null; // null if table is initializing or reloading
109117
}
110-
return null;
118+
119+
el.__clickFlag = false;
120+
return selectedRows.map(function(row) { return row + 1; });
111121
},
112122
/**
113123
* Sets the value of the selected rows in the gtShiny element.

0 commit comments

Comments
 (0)