# example illustrating the 'dnd' and 'checkCallback' options ####
library(jsTreeR)
nodes <- list(
list(
text = "RootA",
type = "root",
children = list(
list(
text = "ChildA1",
type = "child"
),
list(
text = "ChildA2",
type = "child"
)
)
),
list(
text = "RootB",
type = "root",
children = list(
list(
text = "ChildB1",
type = "child"
),
list(
text = "ChildB2",
type = "child"
)
)
)
)
types <- list(
root = list(
icon = "glyphicon glyphicon-ok"
),
child = list(
icon = "glyphicon glyphicon-file"
)
)
checkCallback <- JS(
"function(operation, node, parent, position, more) {",
" if(operation === 'move_node') {",
" if(parent.id === '#' || parent.type === 'child') {",
" return false;", # prevent moving a child above or below the root
" }", # and moving inside a child
" }",
" return true;", # allow everything else
"}"
)
dnd <- list(
is_draggable = JS(
"function(node) {",
" return node[0].type === 'child';",
"}"
)
)
jstree(
nodes,
dragAndDrop = TRUE, dnd = dnd,
types = types,
checkCallback = checkCallback
)
# example illustrating the 'grid' option ####
library(jsTreeR)
nodes <- list(
list(
text = "Products",
children = list(
list(
text = "Fruit",
children = list(
list(
text = "Apple",
data = list(
price = 0.1,
quantity = 20
)
),
list(
text = "Banana",
data = list(
price = 0.2,
quantity = 31
)
),
list(
text = "Grapes",
data = list(
price = 1.99,
quantity = 34
)
),
list(
text = "Mango",
data = list(
price = 0.5,
quantity = 8
)
),
list(
text = "Melon",
data = list(
price = 0.8,
quantity = 4
)
),
list(
text = "Pear",
data = list(
price = 0.1,
quantity = 30
)
),
list(
text = "Strawberry",
data = list(
price = 0.15,
quantity = 32
)
)
),
state = list(
opened = TRUE
)
),
list(
text = "Vegetables",
children = list(
list(
text = "Aubergine",
data = list(
price = 0.5,
quantity = 8
)
),
list(
text = "Broccoli",
data = list(
price = 0.4,
quantity = 22
)
),
list(
text = "Carrot",
data = list(
price = 0.1,
quantity = 32
)
),
list(
text = "Cauliflower",
data = list(
price = 0.45,
quantity = 18
)
),
list(
text = "Potato",
data = list(
price = 0.2,
quantity = 38
)
)
)
)
),
state = list(
opened = TRUE
)
)
)
grid <- list(
columns = list(
list(
width = 200,
header = "Name"
),
list(
width = 150,
value = "price",
header = "Price"
),
list(
width = 150,
value = "quantity",
header = "Qty"
)
),
width = 600
)
jstree(nodes, grid = grid)
# example illustrating custom context menu ####
library(jsTreeR)
customMenu <- JS("function customMenu(node)
{
var tree = $('#mytree').jstree(true);
var items = {
'rename' : {
'label' : 'Rename',
'action' : function (obj) { tree.edit(node); },
'icon': 'glyphicon glyphicon-edit'
},
'delete' : {
'label' : 'Delete',
'action' : function (obj) { tree.delete_node(node); },
'icon' : 'glyphicon glyphicon-trash'
},
'create' : {
'label' : 'Create',
'action' : function (obj) { tree.create_node(node); },
'icon': 'glyphicon glyphicon-plus'
}
}
return items;
}")
nodes <- list(
list(
text = "RootA",
children = list(
list(
text = "ChildA1"
),
list(
text = "ChildA2"
)
)
),
list(
text = "RootB",
children = list(
list(
text = "ChildB1"
),
list(
text = "ChildB2"
)
)
)
)
jstree(
nodes, checkCallback = TRUE, elementId = "mytree",
contextMenu = list(items = customMenu)
)
Run the code above in your browser using DataLab