Angular.js how to search in json array for given path - javascript

In my angular , i am having one $scope variable as below.
$scope.roleList2 = [
{ "roleName" : "User", "roleId" : "role1", "children" : [
{ "roleName" : "subUser1", "roleId" : "role11", "collapsed" : true, "children" : [] },
{ "roleName" : "subUser2", "roleId" : "role12", "collapsed" : true, "children" : [
{ "roleName" : "subUser2-1", "roleId" : "role121", "children" : [
{ "roleName" : "subUser2-1-1", "roleId" : "role1211", "children" : [] },
{ "roleName" : "subUser2-1-2", "roleId" : "role1212", "children" : [] }
]}
]}
]},
{ "roleName" : "Admin", "roleId" : "role2", "children" : [
{ "roleName" : "subAdmin1", "roleId" : "role11", "collapsed" : true, "children" : [] },
{ "roleName" : "subAdmin2", "roleId" : "role12", "children" : [
{ "roleName" : "subAdmin2-1", "roleId" : "role121", "children" : [
{ "roleName" : "subAdmin2-1-1", "roleId" : "role1211", "children" : [] },
{ "roleName" : "subAdmin2-1-2", "roleId" : "role1212", "children" : [] }
]}
]}
]},
{ "roleName" : "Guest", "roleId" : "role3", "children" : [
{ "roleName" : "subGuest1", "roleId" : "role11", "children" : [] },
{ "roleName" : "subGuest2", "roleId" : "role12", "collapsed" : true, "children" : [
{ "roleName" : "Banned Area", "roleId" : "role121", "children" : [
{ "roleName" : "subGuest2-1-1", "roleId" : "role1211", "Parent":"Banned Area" ,"children" : [] },
{ "roleName" : "subGuest2-1-2", "roleId" : "role1212", "Parent":"Banned Area", "children" : [] }
]}
]}
]}
];
I want to first find array for given path
Admin>subAdmin2-1
and insert
{ "roleName" : "subAdmin2-1-3", "roleId" : "role1213", "children" : [] }
Please guide, how can achieve this.

This is quick and dirty but will work:
for (var i=0; i<$scope.roleList2.length; i++) {
if ($scope.roleList2[i].roleName == "Admin") {
var subRoles = $scope.roleList2[i].children;
for (var k=0; k<subRoles.length; k++) {
if (subRoles[k].roleName == "subAdmin2") {
var subSubRoles = subRoles[k].children;
for (var j=0; j<subSubRoles.length; j++) {
if (subSubRoles[j].roleName == "subAdmin2-1") {
subSubRoles[j].children.push({ "roleName" : "subAdmin2-1-3", "roleId" : "role1213", "children" : [] });
console.log(subSubRoles[j]);
}
}
}
}
}
}
You can try improving with a recursion. And you probably want this to be in a function, so you can call
var role = "{ "roleName" : "subAdmin2-1-3", "roleId" : "role1213", "children" : [] }";
$scope.addSubRole(role, "Admin", "subAdmin2-1")

Related

Complex mongodb data how to update

I'm trying to update show_seats.showByDate.shows.showSeats.seat_details.values.seat_status
this my code .........................................................................................................................................................
db.seats.updateOne({'show_seats.showByDate.shows.showSeats.seat_details.values._id':"62a9e044ec028e60180fe28a"},{$set:{'show_seats.showByDate.shows.showSeats.seat_details.values.seat_status' : true}})
.........................................................................................................................................................
please say what did i wrong**
{
"_id" : ObjectId("62a43ac2d7213c7233cd1dee"),
"totalShowByDay" : "2",
"totalShowDays" : 4,
"movieId" : ObjectId("62953ba3cb6ae625ec9433e6"),
"screenId" : ObjectId("6293b9638dde2d92658d5513"),
"createdAt" : 1654930114438,
"showId" : ObjectId("62a43ac2d7213c7233cd14ed"),
"show_seats" : [
{
"showByDate" : {
"ShowDate" : "2022-06-11",
"shows" : [
{
"showTime" : "2022-06-11T10:00",
"showSeats" : [
[
{
"category" : "CLASSIC",
"seat_details" : [
{
"key" : "A",
"values" : [
{
"_id" : ObjectId("62a43ac2d7213c7233cd14ee"),
"seat_number" : "1",
"tag_name" : "A",
"seat_status" : false,
"user_id" : false,
"price" : "140",
"seats_category" : "CLASSIC",
"show_time" : "2022-06-11T10:00"
},
,
{
"_id" : ObjectId("62a43ac2d7213c7233cd14ef"),
"seat_number" : "2",
"tag_name" : "A",
"seat_status" : false,
"user_id" : false,
"price" : "140",
"seats_category" : "CLASSIC",
"show_time" : "2022-06-11T10:00"
},
{
"_id" : ObjectId("62a43ac2d7213c7233cd14f0"),
"seat_number" : "3",
"tag_name" : "A",
"seat_status" : false,
"user_id" : false,
"price" : "140",
"seats_category" : "CLASSIC",
"show_time" : "2022-06-11T10:00",,
{
"_id" : ObjectId("62a43ac2d7213c7233cd14ef"),
"seat_number" : "2",
"tag_name" : "A",
"seat_status" : false,
"user_id" : false,
"price" : "140",
"seats_category" : "CLASSIC",
"show_time" : "2022-06-11T10:00"
},
{
"_id" : ObjectId("62a43ac2d7213c7233cd14f0"),
"seat_number" : "3",
"tag_name" : "A",
"seat_status" : false,
"user_id" : false,
"price" : "140",
"seats_category" : "CLASSIC",
"show_time" : "2022-06-11T10:00"
}
this is what i ended up doing, I need best solution
await db.getDb().collection(coll.seat).aggregate([
{
'$unwind': {
'path': '$show_seats'
}
}, {
'$unwind': {
'path': '$show_seats.showByDate.shows'
}
}, {
'$unwind': {
'path': '$show_seats.showByDate.shows.showSeats'
}
}, {
'$unwind': {
'path': '$show_seats.showByDate.shows.showSeats'
}
}, {
'$unwind': {
'path': '$show_seats.showByDate.shows.showSeats.seat_details'
}
}, {
'$unwind': {
'path': '$show_seats.showByDate.shows.showSeats.seat_details.values'
}
}, {
'$match': {
'show_seats.showByDate.shows.showSeats.seat_details.values._id': ObjectId("62a43ac2d7213c7233cd14ee")
}
}, {
$addFields: { "show_seats.showByDate.shows.showSeats.seat_details.values.seat_status": true }
}
])

Create Functionality in Context Menu of jsTree not working

New nodes won't be created when 'types' plugin is defined.
Please look at this fiddle.
I'm unable to create new Nodes in the tree.
http://jsfiddle.net/z8L5r9w3/1/
$('#jstree').jstree({
"core" : {
"check_callback" : true,
"data" : [
{ "text" : "Branch 1", "type" : "branch", "children" : [
{ "text" : "leaf 1.1", "type" : "leaf" },
{ "text" : "leaf 1.2", "type" : "leaf" },
{ "text" : "leaf 1.3", "type" : "leaf" }
]
},
{ "text" : "Branch 2", "type" : "branch", "children" : [
{ "text" : "leaf 2.1", "type" : "leaf" },
{ "text" : "leaf 2.2", "type" : "leaf" },
{ "text" : "leaf 2.3", "type" : "leaf" }
]
}
]
},
"types" : {
"#" : {
"valid_children" : ["branch"]
},
"branch" : {
"valid_children" : ["leaf"]
},
"leaf" : {
"valid_children" : []
}
},
"plugins" : ["types", "dnd", "contextmenu"]});
Also, you can override "contextmenu"
"contextmenu":{
"items": function () {
return {
"Create": {
"label": "Create",
"action": function (data) {
var ref = $.jstree.reference(data.reference);
sel = ref.get_selected();
if(!sel.length) { return false; }
sel = sel[0];
type = ref.get_type(sel);
if (type == "#")
type = "branch";
else if (type == "branch")
type = "leaf";
else if (type == "leaf")
type = "";
sel = ref.create_node(sel, {text: "new "+type, type: type});
if(sel) {
ref.edit(sel);
}
}
},
"Rename": {
"label": "Rename",
"action": function (data) {
var inst = $.jstree.reference(data.reference);
obj = inst.get_node(data.reference);
inst.edit(obj);
}
},
"Delete": {
"label": "Delete",
"action": function (data) {
var ref = $.jstree.reference(data.reference),
sel = ref.get_selected();
if(!sel.length) { return false; }
ref.delete_node(sel);
}
}
};
}
},
You have a problem with "types". The "Create" action in the "contextmenu" does not know about the types of "branch" and "leaf" and create new node with "type" : "default".
You can see this:
"types" : {
"#" : {
"valid_children" : ["branch", "default"]
},
"branch" : {
"valid_children" : ["leaf", "default"]
},
"leaf" : {
"valid_children" : []
}
},

Tree View with Angular JS and Kendo UI

In my angular web I am using Kendo UI for representing tree view. Here is my html file.
<!DOCTYPE html>
<html>
<head>
<title></title>
<link rel="stylesheet" href="//kendo.cdn.telerik.com/2016.1.226/styles/kendo.common-material.min.css" />
<link rel="stylesheet" href="//kendo.cdn.telerik.com/2016.1.226/styles/kendo.material.min.css" />
<script src="//cdnjs.cloudflare.com/ajax/libs/angular.js/1.2.20/angular.min.js"></script>
<script src="//kendo.cdn.telerik.com/2016.1.226/js/jquery.min.js"></script>
<script src="//kendo.cdn.telerik.com/2016.1.226/js/kendo.all.min.js"> </script>
</head>
<body>
<div ng-app="myapp">
<div ng-controller="myappCtrl">
<div ng-attr-id="{{ treeview }}" kendo-tree-view k-options="treeview"></div>
</div>
<div style="padding-top: 2em;">
<h4>Selected:</h4>
<p ng-attr-id="{{ selected }}">No interests selected.</p>
</div>
</div>
<script src="../scripts/test.js"></script>
</body>
</html>
And this is my test.js
var app = angular.module('myapp', []);
app.controller('myappCtrl', function($scope, $http, $window) {
$scope.dataModal = [ {
"key" : "Key",
"type" : "type",
"value" : 1,
"items" : [ {
"key" : "Key",
"type" : "type1",
"items" : [ {
"key" : "Key",
"type" : "type1",
"items" : [ {
"key" : "Key",
"type" : "type1",
"items" : [
]
}, {
"key" : "Key",
"type" : "type1",
"items" : [
]
}, {
"key" : "Key",
"type" : "type1",
"items" : [
]
}, {
"key" : "Key",
"type" : "type1",
"items" : [
]
}, {
"key" : "Key",
"type" : "type1",
"items" : [
]
}, {
"key" : "Key",
"type" : "type1",
"items" : [
]
}, {
"key" : "Key",
"type" : "type1",
"items" : [
]
}, {
"key" : "Key",
"type" : "type1",
"items" : [ {
"key" : "Key",
"type" : "type1",
"items" : [
]
}, {
"key" : "Key",
"type" : "type1",
"items" : [
]
} ]
}, {
"key" : "Key",
"type" : "type1",
"items" : [
]
}, {
"key" : "Key",
"type" : "type1",
"items" : [
]
}, {
"key" : "Key",
"type" : "type1",
"items" : [ {
"key" : "Key",
"type" : "type1",
"items" : [
]
} ]
}, {
"key" : "Key",
"type" : "type1",
"items" : [
]
}, {
"key" : "Key",
"type" : "type1",
"items" : [
]
}, {
"key" : "Key",
"type" : "type1",
"items" : [
]
}, {
"key" : "Key",
"type" : "type1",
"items" : [
]
}, {
"key" : "Key",
"type" : "type1",
"items" : [
]
}, {
"key" : "Key",
"type" : "type1",
"items" : [
]
}, {
"key" : "Key",
"type" : "type1",
"items" : [
]
}, {
"key" : "Key",
"type" : "type1",
"items" : [
]
}, {
"key" : "Key",
"type" : "type1",
"items" : [
]
}, {
"key" : "Key",
"type" : "type1",
"items" : [
]
}, {
"key" : "Key",
"type" : "type1",
"items" : [
]
}, {
"key" : "Key",
"type" : "type1",
"items" : [
]
}, {
"key" : "Key",
"type" : "type1",
"items" : [
]
}, {
"key" : "Key",
"type" : "type1",
"items" : [
]
}, {
"key" : "Key",
"type" : "type1",
"items" : [
]
}, {
"key" : "Key",
"type" : "type1",
"items" : [
]
} ]
}, {
"key" : "Key",
"type" : "type1",
"items" : [
]
}, {
"key" : "Key",
"type" : "type1",
"items" : [
]
}, {
"key" : "Key",
"type" : "type1",
"items" : [
]
}, {
"key" : "Key",
"type" : "type1",
"items" : [
]
}, {
"key" : "Key",
"type" : "type1",
"items" : [
]
}, {
"key" : "Key",
"type" : "type1",
"items" : [
]
} ]
}, {
"key" : "Key",
"type" : "type1",
"items" : [
]
}, {
"key" : "Key",
"type" : "type1",
"items" : [
]
}, {
"key" : "Key",
"type" : "type1",
"items" : [
]
}, {
"key" : "Key",
"type" : "type1",
"items" : [
]
}, {
"key" : "Key",
"type" : "type1",
"items" : [
]
} ]
}, {
"key" : "Key",
"type" : "type1",
"value" : 1,
"items" : [ {
"key" : "subUser1",
"type" : "role11",
"items" : [
]
} ]
}, {
"key" : "Key",
"type" : "type1",
"value" : 1,
"items" : [ {
"key" : "subUser1",
"type" : "role11",
"items" : [
]
} ]
}, {
"key" : "Key",
"type" : "type1",
"value" : 1,
"items" : [ {
"key" : "subUser1",
"type" : "role11",
"items" : [
]
} ]
}, {
"key" : "Key",
"type" : "type1",
"value" : 1,
"items" : [ {
"key" : "subUser1",
"type" : "role11",
"items" : [
]
} ]
}, {
"key" : "Key",
"type" : "type1",
"value" : 1,
"items" : [ {
"key" : "subUser1",
"type" : "role11",
"items" : [
]
} ]
} ];
$scope.treeview = {
checkboxes : {
checkChildren : true
},
//check : onCheck,
dataTextField : "key",
loadOnDemand : false,
dataSource : $scope.dataModal
};
// get keys of checked nodes
function checkedNodeIds(nodes, checkedNodes) {
for (var i = 0; i < nodes.length; i++) {
if (nodes[i].checked) {
checkedNodes.push(nodes[i].key);
}
if (nodes[i].hasChildren) {
checkedNodeIds(nodes[i].children.view(), checkedNodes);
}
}
}
// show checked nodes on data source change
function onCheck() {
var checkedNodes = [];
var treeView = $("#treeview").data("kendoTreeView");
var message = "";
checkedNodeIds(treeView.dataSource.view(), checkedNodes);
if (checkedNodes.length > 0) {
message = "Selected Interests: " + checkedNodes.join(", ");
} else {
message = "No Interests Checked.";
}
$("#selected").html(message);
}
});
My page is displaying the tree view. And I am using two functions to get the selected items of the tree. But it gives as error on console log as below.
test.js:347 Uncaught TypeError: Cannot read property 'dataSource' of null
Line 347: checkedNodeIds(treeView.dataSource.view(), checkedNodes);
Please be kind enough to show me what's wrong with my codes.
Thank You
First of all your code needs some modifications
In order to use kendo directives inside angular you need to introduce a dependency on them, this is done on your module configuration, without this configuration anything will work
var app = angular.module('myapp', ['kendo.directives']);
You should avoid using jQuery selectors on controllers, it's place is in angualr directives. In order to set the output message to the view you can simply use a variable on your scope and then bind it on the view
// show checked nodes on data source change
function onCheck(kendoEvent) {
var checkedNodes = [];
var treeView = kendoEvent.sender;
var message = "";
checkedNodeIds(treeView.dataSource.view(), checkedNodes);
if (checkedNodes.length > 0) {
message = "Selected Interests: " + checkedNodes.join(", ");
} else {
message = "No Interests Checked.";
}
$scope.$apply(function() { $scope.selectedItems = message; });
}
Note that we use $scope.$apply, to set this message, this is because jQuery events which is used by kendo happens outside the control of angular, so angular does not update the view because he doesn't know that he has to do it, using $scope.$apply we force it Angular to update the bindings.
onCheck event inside tree configuration should be uncomment, all kendo events handlers receives a parameter as an argument, which contains a pointer to the widget. documentation

underscore - locating node based in a value

This is my 1st time using underscore...I've this simple json...
"categories" : [
{
"tag" : "cat1",
"active" : true,
"order" : 10,
"title" : {
"en" : "Category 1",
"pt" : "Categoria 1"
},
"children" : [
{
"tag" : "cat11",
"active" : true,
"order" : 10,
"title" : {
"en" : "Category 1.1",
"pt" : "Categoria 1.1"
}
},
{
"tag" : "cat12",
"active" : true,
"order" : 20,
"title" : {
"en" : "Category 1.2",
"pt" : "Categoria 1.2"
}
}
]
},
{
"tag" : "cat2",
"active" : true,
"order" : 10,
"title" : {
"en" : "Category 2",
"pt" : "Categoria 2"
},
"children" : [
{
"tag" : "cat21",
"active" : true,
"order" : 10,
"title" : {
"en" : "Category 2.1",
"pt" : "Categoria 2.1"
}
},
{
"tag" : "cat22",
"active" : true,
"order" : 20,
"title" : {
"en" : "Category 2.2",
"pt" : "Categoria 2.2"
}
}
]
},
{
"tag" : "cat3",
"active" : true,
"order" : 10,
"title" : {
"en" : "Category 3",
"pt" : "Categoria 3"
},
"children" : [
{
"tag" : "cat31",
"active" : true,
"order" : 10,
"title" : {
"en" : "Category 3.1",
"pt" : "Categoria 3.1"
}
},
{
"tag" : "cat32",
"active" : true,
"order" : 20,
"title" : {
"en" : "Category 3.2",
"pt" : "Categoria 3.2"
}
}
]
},
{
"tag" : "cat4",
"active" : true,
"order" : 10,
"title" : {
"en" : "Category 4",
"pt" : "Categoria 4"
},
"children" : [
{
"tag" : "cat41",
"active" : true,
"order" : 10,
"title" : {
"en" : "Category 4.1",
"pt" : "Categoria 4.1"
}
},
{
"tag" : "cat42",
"active" : true,
"order" : 20,
"title" : {
"en" : "Category 4.2",
"pt" : "Categoria 4.2"
}
}
]
}
]
you can see I've the TAG key in several places. I need to get the entire tree based in a criteria for tag. I was using, find, filter, where and findWhere and all the time I got the same results:
var find = _.find($rootScope.webshop.categories, {tag: 'cat1'});
this works !
but if I try...
var find = _.find($rootScope.webshop.categories, {tag: 'cat11'});
No results :( Even using _.where or ._filter or ._findWhere. - The results always will be the same.
Can someone help a beginner in Underscore with something that's probably simple ?!
ty !
_.find does not recurse, you will have to build your own solution. Something like this:
_.findIn = function() {
var args = Array.prototype.slice.call(arguments, 0);
var childrenProp = args[0];
var result = _.find.apply(_, args.slice(1));
if (result !== void(8)) return result;
var arr = args[1];
for (var i = 0, l = arr.length; i < l; i++) {
args[1] = arr[i][childrenProp];
var result = _.findIn.apply(_, args);
if (result !== void(8)) return result;
}
return void(8);
}
_.findIn('children', categories, {tag: 'cat11'})
categories = [
{
"tag" : "cat1",
"active" : true,
"order" : 10,
"title" : {
"en" : "Category 1",
"pt" : "Categoria 1"
},
"children" : [
{
"tag" : "cat11",
"active" : true,
"order" : 10,
"title" : {
"en" : "Category 1.1",
"pt" : "Categoria 1.1"
}
},
{
"tag" : "cat12",
"active" : true,
"order" : 20,
"title" : {
"en" : "Category 1.2",
"pt" : "Categoria 1.2"
}
}
]
},
{
"tag" : "cat2",
"active" : true,
"order" : 10,
"title" : {
"en" : "Category 2",
"pt" : "Categoria 2"
},
"children" : [
{
"tag" : "cat21",
"active" : true,
"order" : 10,
"title" : {
"en" : "Category 2.1",
"pt" : "Categoria 2.1"
}
},
{
"tag" : "cat22",
"active" : true,
"order" : 20,
"title" : {
"en" : "Category 2.2",
"pt" : "Categoria 2.2"
}
}
]
},
{
"tag" : "cat3",
"active" : true,
"order" : 10,
"title" : {
"en" : "Category 3",
"pt" : "Categoria 3"
},
"children" : [
{
"tag" : "cat31",
"active" : true,
"order" : 10,
"title" : {
"en" : "Category 3.1",
"pt" : "Categoria 3.1"
}
},
{
"tag" : "cat32",
"active" : true,
"order" : 20,
"title" : {
"en" : "Category 3.2",
"pt" : "Categoria 3.2"
}
}
]
},
{
"tag" : "cat4",
"active" : true,
"order" : 10,
"title" : {
"en" : "Category 4",
"pt" : "Categoria 4"
},
"children" : [
{
"tag" : "cat41",
"active" : true,
"order" : 10,
"title" : {
"en" : "Category 4.1",
"pt" : "Categoria 4.1"
}
},
{
"tag" : "cat42",
"active" : true,
"order" : 20,
"title" : {
"en" : "Category 4.2",
"pt" : "Categoria 4.2"
}
}
]
}
]
_.findIn = function() {
var args = Array.prototype.slice.call(arguments, 0);
var childrenProp = args[0];
var result = _.find.apply(_, args.slice(1));
if (result !== void(8)) return result;
var arr = args[1];
for (var i = 0, l = arr.length; i < l; i++) {
args[1] = arr[i][childrenProp];
var result = _.findIn.apply(_, args);
if (result !== void(8)) return result;
}
return void(8);
}
document.write(JSON.stringify(_.findIn('children', categories, {tag: 'cat11'})));
<script src="//cdnjs.cloudflare.com/ajax/libs/underscore.js/1.7.0/underscore-min.js"></script>

RedQueryBuilder validations

I am using the RedQueryBuilder version 0.5.0
http://0-5-0.redquerybuilder.appspot.com/ for building an Expression validator.
I want to Know, how to validate the entered expressions?
The metadata of the RedQuery Builder 0.5.0 supports css and class additions for Jquery support.
Please, suggest how to add the css metadata to the existing metadata.
The Sample MetaData is given below:
meta : {
tables : [ {
"name" : "PERSON",
"label" : "Person",
"columns" : [ {
"name" : "NAME",
"label" : "Name",
"type" : "STRING",
"size" : 10
}, {
"name" : "DOB",
"label" : "Date of birth",
"type" : "DATE"
}, {
"name" : "SEX",
"label" : "Sex",
"type" : "STRING",
"editor" : "SELECT"
}, {
"name" : "CATEGORY",
"label" : "Category",
"type" : "REF",
} ],
fks : []
} ],
types : [ {
"name" : "STRING",
"editor" : "TEXT",
"operators" : [ {
"name" : "=",
"label" : "is",
"cardinality" : "ONE"
}, {
"name" : "<>",
"label" : "is not",
"cardinality" : "ONE"
}, {
"name" : "LIKE",
"label" : "like",
"cardinality" : "ONE"
}, {
"name" : "<",
"label" : "less than",
"cardinality" : "ONE"
}, {
"name" : ">",
"label" : "greater than",
"cardinality" : "ONE"
} ]
}, {
"name" : "DATE",
"editor" : "DATE",
"operators" : [ {
"name" : "=",
"label" : "is",
"cardinality" : "ONE"
}, {
"name" : "<>",
"label" : "is not",
"cardinality" : "ONE"
}, {
"name" : "<",
"label" : "before",
"cardinality" : "ONE"
}, {
"name" : ">",
"label" : "after",
"cardinality" : "ONE"
} ]
}, {
"name" : "REF",
"editor" : "SELECT",
"operators" : [ {
"name" : "IN",
"label" : "any of",
"cardinality" : "MULTI"
}]
} ]
},

Categories

Resources