how to Restrict the number of children of parent selected in jstree? [closed] - jstree

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
how i can Restrict the number of children selected of parent?
please help me
function buildTree(data) {
var dataJson = JSON.parse(data);
$("#onflycheckboxes").jstree({
"json_data": {
data: dataJson,
},
checkbox: {
real_checkboxes: true,
checked_parent_open: true,
three_state: false
},
"search": {
"case_insensitive": true,
"show_only_matches": true
},
"plugins": ["themes", "json_data", "ui", "checkbox", "search"]
});
}

for check nodes you must use $("#onflycheckboxes").jstree('get_checked')in jstree
function buildTree(data) {
var dataJson = JSON.parse(data);
$("#onflycheckboxes").jstree({
"json_data": {
data: dataJson,
},
checkbox: {
real_checkboxes: true,
checked_parent_open: true,
three_state: false
},
"search": {
"case_insensitive": true,
"show_only_matches": true
},
"plugins": ["themes", "json_data", "ui", "checkbox", "search"]
}).bind("before.jstree", function (e, data) {
if (data.func === "check_node") {
if ($("#onflycheckboxes").jstree('get_checked').length >= numberOfCompanies) {
e.preventDefault();
toastMessage();//your message for show to user can't select any more
return false;
}
}
});;

Related

In jsTree make last child selected after click on in with link open

I can't make my last child node selected after click on it.
Click open this node (href url) but on reloaded page it only select parent node.
Maybe there is also an option to prevent selection of parent node? I don't need it at all.
My tree have only 3 levels and only 3rd/last is used to open links.
jstree config:
$(() => {
$('#jstree').jstree({
core: {
multiple: false,
themes: {
dots: false,
},
data: {
url: '/jsonCats',
dataType: 'json',
},
},
state: {
key: 'navTree',
tree_state: false,
},
types: {
firstfloor: {
icon: false,
selected: false,
},
secfloor: {
icon: false,
},
hidden: {
icon: false,
},
element: {
icon: 'bi bi-file-earmark-text',
},
},
search: {
case_sensitive: false,
show_only_matches: true,
},
plugins: ['search', 'types', 'state', 'changed'],
});
$(document).ready(() => {
$('.search-input').keyup(function () {
const searchString = $(this).val();
$('#jstree').jstree('search', searchString);
});
});
$('#jstree').on('changed.jstree', (e, data) => {
if (data.event === undefined) {
return false;
}
if (data.node.a_attr.href !== '#' && data.event.bubbles) {
location.assign(data.node.a_attr.href);
}
});
});
example json last child:
{
"id": 1001,
"parent": "16",
"text": "Text",
"type": "element",
"li_attr": {
"elem": "element"
},
"a_attr": {
"href": "/KnowledgeBase/1001"
}
localStorage only select parent node:
{"state":{"core":{"open":["1","16"],"scroll":{"left":0,"top":0},"selected":["1"]}},"ttl":false,"sec":1673943166005}

Mongoose: findByIdAndUpdate() How To Conditionally Update Based On Existing Data?

When using findByIdAndUpdate() or findOneAndUpdate() (that is, updating the database atomically to avoid race conditions), is there a way to reference the existing data to conditionally update a field?
PersonModel.findByIdAndUpdate(
req.user.id,
{
$set: {
// set this to true
// if person.color === blue for example
hasFavoriteColor: true,
}
},
{ new: true },
(err, updatedDoc => {
if (err) return;
return updatedDoc;
});
);
I'm not sure you can do that, I see nowhere in the document. However, for your query particularly, you can add the condition about the color in the filter part, something like this :
PersonModel.findOneAndUpdate(
{_id : req.user.id, color : "blue"},
{
$set: {
// set this to true
// if person.color === blue for example
hasFavoriteColor: true,
}
},
{ new: true },
(err, updatedDoc => {
if (err) return;
return updatedDoc;
});
);

$inc has no effect on the document (does not work at all) [duplicate]

This question already has answers here:
Update field in exact element array in MongoDB
(5 answers)
Closed 4 years ago.
I've got a simple poll trying to update the vote counter every time someone upvte it.
I tried using $inc but it has not effect. therefore it does return which supposed to be the updated poll/ poll after the vote counter is updated, but it just returns the same one without increasing anything at all.
What am i doing wrong?
app.patch('/voting/:id', (req, res) => {
let userVote = req.body.votes;
Poll_Schema_Model.findByIdAndUpdate({ "_id": '5b070f512a28d70eb0abaa51' }, { $inc: { "poll[0].votes":userVote } }, { new: true }, (err, newPoll) => {
res.status(200).send(newPoll);
})
.catch(() => {
res.status(400).send();
});
});
the newPoll results in :- (note that votes is defaulted to 0)
{
"_id": "5b070f512a28d70eb0abaa51",
"_creator": "5b04aba0ee81bb26182b2267",
"poll": [
{
"votes": 0,
"_id": "5b070f512a28d70eb0abaa52",
"option1": "FIRST OPTIONNN",
"option2": "SECOND OPTIONN"
}
],
"__v": 0
}
My schema :-
const Poll_Schema = new mongoose.Schema({
_creator: {
type: mongoose.Schema.Types.ObjectId
},
poll: [{
option1: {
type: String,
maxlength: 20,
minlength: 3
},
option2: {
type: String,
maxlength: 20,
minlength: 3
},
votes: {
type: Number,
minlength: 1,
default:0
}
}]
});
The syntax for referencing array item is different, you should specify position after dot like poll.0.votes instead of [0]. So your code should look like this:
app.patch('/voting/:id', (req, res) => {
let userVote = req.body.votes;
Poll_Schema_Model.findByIdAndUpdate({ "_id": '5b070f512a28d70eb0abaa51' }, { $inc: { "poll.0.votes":userVote } }, { new: true }, (err, newPoll) => {
res.status(200).send(newPoll);
})
.catch(() => {
res.status(400).send();
});
});

How to update a field using its previous value in MongoDB/Mongoose [duplicate]

This question already has answers here:
Update MongoDB field using value of another field
(12 answers)
Closed 5 years ago.
I know I can do an update using $set:
Contact.update({
_id: request.id
}, {
$set: { name: newNameValue }
}, {
upsert: false
}, function(err) { ... });
But in this case, instead of passing newNameValue, I'd like to use the previous name value to compute the new one. Let's say I want to capitalize the old name, something like:
Contact.update({
_id: request.id
}, {
$set: { name: $old.name.toUpperCase() }
}, {
upsert: false
}, function(err) { ... });
I think this has already been answered here: How to add new data to current string in MongoDB?, so please, check that for a more detailed answer, but anyway, in short, you can't do that with a single query.
The way to do it using Mongoose, as shown in this official Mongoose example:
Contact.findById(request.id, (err, contract) => {
if (err) return handleError(err);
contract.name = contract.name.toUpperCase();
contract.save((err, contractContract) => {
if (err) return handleError(err);
...
});
});
as far as I know it's not possible. You would need to find and then update
Contact
.find({
_id: request.id
})
.exec(function(err, data) {
if (err) {
return ...;
}
Contact.findByIdAndUpdate(request.id, {
$set: {
name: data.name.toUpperCase()
}
}, {
new: true
}, function(err, doc) {
if (err) return ...;
console.log(doc)
});
}

jsTree state plugin reloads page continually

I'm using jsTree as a navigation menu, and after clicking any of the links the page reloads over and over... I've searched all day for an answer to this and would very much appreciate a hand.
$('#NavTreeContainer').jstree({
'core': {
'themes': {
'name': 'default',
'responsive': true,
'stripes': true,
'dots': true,
'icons': false
}
},
'state' : { 'key' : 'navTree' },
'plugins' : [ 'state' ]
}).on('select_node.jstree', function(e, data) {
document.location = data.instance.get_node(data.node, true).children('a').attr('href');
});
$('#ExpandAllNavTreeButton').click(function() {
$('#NavTreeContainer').jstree('open_all');
});
$('#CollapseAllNavTreeButton').click(function() {
$('#NavTreeContainer').jstree('close_all');
});
I was able to get this functionality working by only binding the select node event after the tree state has been restored;
$('#jstree_id').jstree({
"state": { "key": "jstree_id" },
"plugins": ["state"]
}).on('restore_state.jstree', function () {
$('#jstree_id').on('select_node.jstree', function (e, data) {
var href = data.node.a_attr.href;
document.location.href = href;
});
});
If you already solved this I hope it helps someone else.