Skip to content

Commit 7409aca

Browse files
committed
Shiny values ID_checked and ID_checked_paths
1 parent 96a97be commit 7409aca

File tree

2 files changed

+52
-3
lines changed

2 files changed

+52
-3
lines changed

inst/essais/essai_tieSelection.R

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,13 @@ server <- function(input, output, session){
3636
jstree(
3737
nodes,
3838
checkboxes = TRUE,
39-
checkWithText = FALSE,
40-
checkCallback = TRUE
39+
checkWithText = FALSE
4140
)
4241
)
4342
})
4443

4544
observe({
46-
print(input$myTree_checked_tree)
45+
print(input$myTree_checked)
4746
print("____________________")
4847
})
4948
}

inst/htmlwidgets/jstreer.js

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,38 @@ function setShinyValueSelectedNodes(instance, leavesOnly, checkboxes) {
108108
}
109109
}
110110

111+
function setShinyValueCheckedNodes(instance, leavesOnly) {
112+
var checkedNodes = instance.get_checked(true);
113+
var nodes = getNodes(checkedNodes);
114+
var leaves = [];
115+
var pathNodes = [];
116+
var leavePathNodes = [];
117+
for(var i = 0; i < nodes.length; i++) {
118+
var node = nodes[i];
119+
var path = instance.get_path(checkedNodes[i], "/");
120+
var pathNode = {
121+
path: path,
122+
data: node.data
123+
};
124+
pathNodes.push(pathNode);
125+
var nchildren = node.children.length;
126+
delete nodes[i].children;
127+
if (leavesOnly && nchildren === 0) {
128+
leaves.push(nodes[i]);
129+
leavePathNodes.push(pathNode);
130+
}
131+
}
132+
Shiny.setInputValue(
133+
instance.element.attr("id") + "_checked:jsTreeR.list",
134+
leavesOnly ? leaves : nodes
135+
);
136+
Shiny.setInputValue(
137+
instance.element.attr("id") + "_checked_paths:jsTreeR.list",
138+
leavesOnly ? leavePathNodes : pathNodes
139+
);
140+
}
141+
142+
111143
var inShiny = HTMLWidgets.shinyMode;
112144

113145
HTMLWidgets.widget({
@@ -216,13 +248,19 @@ HTMLWidgets.widget({
216248
if(inShiny) {
217249
setShinyValue(data.instance);
218250
setShinyValueSelectedNodes(data.instance, leavesOnly, checkboxes);
251+
if(checkboxes) {
252+
setShinyValueCheckedNodes(data.instance, leavesOnly);
253+
}
219254
}
220255
});
221256

222257
$el.on("refresh.jstree", function(e, data) {
223258
if(inShiny) {
224259
setShinyValue(data.instance);
225260
setShinyValueSelectedNodes(data.instance, leavesOnly, checkboxes);
261+
if(checkboxes) {
262+
setShinyValueCheckedNodes(data.instance, leavesOnly);
263+
}
226264
}
227265
});
228266

@@ -257,6 +295,9 @@ HTMLWidgets.widget({
257295
// id, getNodesWithChildren(data.instance.get_json())
258296
// );
259297
setShinyValueSelectedNodes(data.instance, leavesOnly, checkboxes);
298+
if(checkboxes) {
299+
setShinyValueCheckedNodes(data.instance, leavesOnly);
300+
}
260301
//setShinyValue(data.new_instance); // modif 9/10/2023
261302
}
262303
});
@@ -291,13 +332,19 @@ HTMLWidgets.widget({
291332
if(inShiny) {
292333
setShinyValue(data.instance);
293334
setShinyValueSelectedNodes(data.instance, leavesOnly, checkboxes);
335+
if(checkboxes) {
336+
setShinyValueCheckedNodes(data.instance, leavesOnly);
337+
}
294338
}
295339
});
296340
$el.on("uncheck_node.jstree", function(e, data) {
297341
$el.jstree(true).deselect_node(data.node);
298342
if(inShiny) {
299343
setShinyValue(data.instance);
300344
setShinyValueSelectedNodes(data.instance, leavesOnly, checkboxes);
345+
if(checkboxes) {
346+
setShinyValueCheckedNodes(data.instance, leavesOnly);
347+
}
301348
}
302349
});
303350
}
@@ -315,6 +362,9 @@ HTMLWidgets.widget({
315362
});
316363
setShinyValue(instance);
317364
setShinyValueSelectedNodes(instance, leavesOnly, checkboxes);
365+
if(checkboxes) {
366+
setShinyValueCheckedNodes(data.instance, leavesOnly);
367+
}
318368
}
319369
});
320370

0 commit comments

Comments
 (0)