jstree - Cannot set icons when using types plugin - jstree

I'm trying to display a jstree containing sections and the pages that they contain.
I've set up a type for each but I can't get the icon to change for the page type, it just keeps displaying the default folder icon.
This is my javascript:
$('#demo1').jstree({
"themes" : {
"theme" : "default",
"dots" : false,
"icons" : true
},
"types" : {
"section" : {
"max_children" : -1,
"max_depth" : -1,
"valid_children": "all",
"type_attr" : "section"
},
"page" : {
"max_children" : 0,
"max_depth" : -1,
"valid_children": "none",
"icon" : {
"image" : "http://static.jstree.com/v.1.0rc/_docs/_drive.png"
},
"type_attr" : "page"
}
},
"core" : {"html_titles" : true, "load_open" : true },
"plugins" : [ "themes", "json_data", "ui", "cookies", "crrm", "sort", "types" ],
"json_data" : {
"ajax" : {
"type": 'GET',
"url" : function (node) {
var url = ""
if (node == -1)
{
url = 'localhost/sections';
}
else
{
url = 'localhost/sections/'+node.attr("id");
}
return url;
},
"type" : "get",
"success" : function(items) {
data = []
for (i in items) {
var item = items[i]
var type;
if (JSON.stringify(items[i].root)) {
type = "section";
node = {
"data" : item.title,
"attr" : { "id" : item.id, "rel" : type },
"state" : "closed"
}
} else {
type = "page";
node = {
"data" : item.title,
"attr" : { "rel" : type },
"state" : "open"
}
}
this.set_type(type, node);
data.push(node);
}
return data;
}
}
}
});
This is the HTML generated by the AJAX call.
<div id="demo1" class="demo jstree jstree-0 jstree-focused jstree-default" style="height:100px;">
<ul class="jstree-no-dots">
<li id="1" rel="section" class="jstree-open">
<ins class="jstree-icon"> </ins><ins class="jstree-icon"> </ins>One
<ul>
<li id="3" rel="section" class="jstree-closed"><ins class="jstree-icon"> </ins><ins class="jstree-icon"> </ins>One Subsection</li>
<li rel="page" class="jstree-open jstree-last"><ins class="jstree-icon"> </ins><ins class="jstree-icon"> </ins>Page in section one</li>
</ul>
</li>
<li id="2" rel="section" class="jstree-closed jstree-last"><ins class="jstree-icon"> </ins><ins class="jstree-icon"> </ins>Two</li>
</ul>
</div>
Can anyone see what's going wrong?
Any advice appreciated.
Thanks

Not sure if this answer is still useful but I've been having similar problems with jsTree and I found your question
Aare you sure this config is right? You have:
"types" : {
"section" : {
"max_children" : -1,
"max_depth" : -1,
"valid_children": "all",
"type_attr" : "section"
},
"page" : {
"max_children" : 0,
"max_depth" : -1,
"valid_children": "none",
"icon" : {
"image" : "http://static.jstree.com/v.1.0rc/_docs/_drive.png"
},
"type_attr" : "page"
}
I think it should be
"types" : {
"type_attr" : "rel", // you can remove this. the rel attribute is the default
"types" : {
"section" : {
"max_children" : -1,
"max_depth" : -1,
"valid_children": "all"
},
"page" : {
"max_children" : 0,
"max_depth" : -1,
"valid_children": "none",
"icon" : {
"image" : "http://static.jstree.com/v.1.0rc/_docs/_drive.png"
}
}
}
Notice that the Types object contains some properties (the type_attr option) and it also contains a nested Types property which includes each type.
Based on the docs I've read, the jsTree lib looks in the type_attr and gets the node's value and compares it to the list of values in Types.Types

for those here using 3.0, its different now.
from this issue: https://github.com/vakata/jstree/issues/497
The type is not read off of the rel attribute. Try using <li data-jstree='{ "type" : "floor" }'... in your markup (and keep the single quotes outside, and the double quotes - inside for the data-jstree attribute).`
so the key is
<li data-jstree='{ "type" : "section" }'>...</li>

Related

Edited: checkbox check_node won't fire

Here is my fiddle. https://jsfiddle.net/NervousElk/6scrbkex/12/.
The tree nodes are populated just fine but I cannot find a way to detect which checkbox is then selected/deselected by the user.
<!DOCTYPE html>
<html>
<head>
<title>testver</title>
<link rel = "stylesheet" href="css/jstree/themes/default/style.min.css">
</head>
<body>
<script src = "jquery-3.3.1.min.js"></script>
<script src = "jquery.js"></script>
<script src = "jstree.min.js"></script>
<div id="staffallocatertree">
</div>
<button id = "treetestbtn" >Build tree
</button>
<script>
document.getElementById("treetestbtn").addEventListener("click", buildthetree);
function buildthetree()
{
//$('#staffallocatertree').jstree('refresh');
$('#staffallocatertree').jstree(
{
'plugins': [ "checkbox", "themes", "sort", "ui", "state" ],
'core' :
{
"check_callback": true,
/* 'data' :
{
"url" : "tl2_get_allstaff_as_tree.php",
"dataType" : "json"
}, */
'data' :
[
{ "id" : "ajson1", "parent" : "#", "text" : "Department 1" },
{ "id" : "ajson2", "parent" : "#", "text" : "Department 2" },
{ "id" : "ajson3", "parent" : "ajson2", "text" : "Dave" },
{ "id" : "ajson4", "parent" : "ajson2", "text" : "Jim" },
],
"checkbox": {
"three_state" : false, // to avoid that fact that checking a node also check others
"whole_node" : false, // to avoid checking the box just clicking the node
"tie_selection" : false // for checking without selecting and selecting without checking
},
//"plugins": ['checkbox']
},
})
.on("check_node.jstree uncheck_node.jstree", function(e, data)
{
alert(data.node.id + ' ' + data.node.text + (data.node.state.checked ? ' CHECKED': ' NOT CHECKED'))
})
};
</script>
</body>
</html>
After the tree is populated I want to interact with the checkboxes and detect the node and text for later use. To this end I am trying to get the check_node/ uncheck_node to fire.
Thanks for any pointers.
Surprisingly this seems to be an open issue .
Fortunately the provided answer works, which is to listen on select_node.jstree.
In your case, simply change
.on("check_node.jstree uncheck_node.jstree", function(e, data) {})
to
.on("select_node.jstree unselect_node.jstree", function(e, data) {})
Hope it helps !

Mongodb update and delete operations in a single query

I have documents in which I would like to update the hostUser with one of the members of the document,also have to delete the record from the member document and add the chips of the deleted member in the club chips.
Here is the sample document.
{
"_id" : "1002",
"hostUser" : "1111111111",
"clubChips" : 10000,
"requests" : {},
"profile" : {
"clubname" : "AAAAA",
"image" : "0"
},
"tables" : [
"SJCA3S0Wm"
],
"isDeleted" : false,
"members" : {
"1111111111" : {
"chips" : 0,
"id" : "1111111111"
},
"2222222222" : {
"chips" : 0,
"id" : "2222222222"
}
}
}
This is what I have tried.
db.getCollection('test').updateMany({"hostUser":"1111111111"},
{"$set":{"hostUser":"2222222222"},"$unset":{"members.1111111111":""}})
This is how you would handle unset and set in a single call to updateMany. Can you please clarify what you meant by "check if the values exist in the member field"?
db.getCollection('test').updateMany(
{"hostUser":"1111111111"},
{
'$set': {"hostUser":"2222222222"} ,
'$unset': {"members.1111111111":""}
}
)

Why does MapReduce return undefined for a field that exists in document?

I am trying to debug a strange issue I am running into while running mapreduce on a collection:
For reference, here's a single document from the collection:
{
"_id" : "ITOUXFWgvWs",
"source" : "youtube",
"insert_datetime" : ISODate("2017-04-06T22:27:43.598Z"),
"processed" : false,
"raw" : {
"id" : "ITOUXFWgvWs",
"etag" : "\"m2yskBQFythfE4irbTIeOgYYfBU/hiQtS6aptLlqxTpsYp1EJIRcoZo\"",
"snippet" : {
"publishedAt" : ISODate("2017-04-06T13:25:28Z"),
"title" : "Alarm.com: The Only Smart Home App You Need",
"channelId" : "UC_HZfoZUP36STk7SrtKYH4g",
"description" : "All these new connected devices are awesome, but wouldn’t it be great if you could use one app for the entire connected home? It can all come together with Alarm.com.",
"categoryId" : "28",
"channelTitle" : "Alarm.com",
"thumbnails" : {
"default" : {
"height" : 90,
"width" : 120,
"url" : "https://i.ytimg.com/vi/ITOUXFWgvWs/default.jpg"
},
"standard" : {
"height" : 480,
"width" : 640,
"url" : "https://i.ytimg.com/vi/ITOUXFWgvWs/sddefault.jpg"
},
"high" : {
"height" : 360,
"width" : 480,
"url" : "https://i.ytimg.com/vi/ITOUXFWgvWs/hqdefault.jpg"
},
"medium" : {
"height" : 180,
"width" : 320,
"url" : "https://i.ytimg.com/vi/ITOUXFWgvWs/mqdefault.jpg"
},
"maxres" : {
"height" : 720,
"width" : 1280,
"url" : "https://i.ytimg.com/vi/ITOUXFWgvWs/maxresdefault.jpg"
}
},
"liveBroadcastContent" : "none",
"localized" : {
"title" : "Alarm.com: The Only Smart Home App You Need",
"description" : "All these new connected devices are awesome, but wouldn’t it be great if you could use one app for the entire connected home? It can all come together with Alarm.com."
}
},
"contentDetails" : {
"duration" : "PT37S",
"dimension" : "2d",
"definition" : "hd",
"licensedContent" : false,
"projection" : "rectangular",
"caption" : "false"
},
"kind" : "youtube#video",
"statistics" : {
"likeCount" : "0",
"dislikeCount" : "0",
"favoriteCount" : "0",
"viewCount" : "32"
},
"uploaded" : ISODate("2017-04-06T13:25:28Z")
},
}
I am literally following the mapreduce debug steps from official mongo documentation.
Here's what my mapreduce script looks like:
var map = function() {
emit("1", this._id);
};
var emit = function(key, value) {
print("emit");
print("key: " + key + " value: " + tojson(value));
}
var myDoc = db.getCollection("abc").find({}).limit(1);
map.apply(myDoc);
And it always produces the result like this:
MongoDB shell version: 2.4.6
connecting to: test
emit
key: 1 value: undefined
I expect the script to emit the _id since it clearly exists in the document but it doesn't.
What might be possible cause of this?
find() always returns a cursor.
Replace it with findOne()
var myDoc = db.getCollection("abc").findOne({});
Or store the documents in an Array using toArray()
var myDoc = db.getCollection("abc").find({}).limit(1).toArray()[0];

How to add resource and specify related element?

I have a simple API for a game tip website:
/class is the endpoint for in game classes
/tip is the endpoints for the tips
/user is the endpoint for the users
Each tip has 3 relations:
(:User)-[:AUTHORED]-(:Tip)
(:Class)<-[:FOR]-(:Tip)
(:Class)<-[:AGAINST]-(:Tip)
When I create a Tip using POST, I do'nt know how to add relations at the create time.
I can do this way: Add relation to another node in SDN4 + REST after creating the resource, but I want to do it with only one query.
EDIT:
I tried to POST this:
'{"content":"TEST", "forClass":"/class/2", "againstClass":"/class/2"}'
and the item has been created, no InvalidArgument Exception raised, but if I go to my class resource's tips, I don't have any tips:
GET /class/2/tips:
{
"_embedded" : {
"tip" : [ ]
},
"_links" : {
"self" : {
"href" : "http://localhost:8080/class/2/tips"
}
}
}
GET /tip/9 (the created one):
{
"content" : "TEST",
"_links" : {
"self" : {
"href" : "http://localhost:8080/tip/9"
},
"tip" : {
"href" : "http://localhost:8080/tip/9"
},
"author" : {
"href" : "http://localhost:8080/tip/9/author"
},
"againstClass" : {
"href" : "http://localhost:8080/tip/9/againstClass"
},
"forClass" : {
"href" : "http://localhost:8080/tip/9/forClass"
}
}
}
GET /tip/9/forClass:
{
"name" : null,
"_links" : {
"self" : {
"href" : "http://localhost:8080/class/2"
},
"bnSClass" : {
"href" : "http://localhost:8080/class/2"
},
"tips" : {
"href" : "http://localhost:8080/class/2/tips"
}
}
}

In mongodb is it possible to query by sub-object?

I have an doc in mongo:
{
"_id" : ObjectId("54eb5189ad9685bbb622ca52"),
"header" : {
"title" : "Project Name 1",
"header_img" : "project_name_1.png",
"project_stats" : {
"sqFt" : 20000,
"tons" : 300,
"duration" : "6 months",
"type" : "education facility",
"summary" : "Give quick summary of problem solved."
}
},
"row_project_detail" : {
"project_logo" : "project_name_1_logo.png",
"header" : "Project Name 1 was a project where...",
"paragraph" : "blah blah blah blah"
},
"row_1" : {
"img" : "project_name_2.png"
},
"row_2" : {
"img_1" : "project_name_3.png",
"img_2" : "project_name_4.png"
},
"row_3" : {
"img" : "project_name_5.png"
},
"row_4" : {
"img" : "project_name_6.png"
}
}
I have tried to query by db.projects.find({ header: { title: "Project Name 1"} }); but it does not produce results. How can I query by the title key in the header object? Is this possible or do I simply need to duplicate the title key from the header sub-object and put that at the root of the doc?
Use dot notation:
db.projects.find({ "header.title": "Project Name 1"});