Problems populating a kendo treeview dialog: only root nodes are populated - kendo-treeview

I'm facing some difficulties when trying to populate a kendo treeview with JSON data. My kendo treeview only displays the root elements and not it's children.
In short I have the following in js:
var featuremodel = getMetaContentByName("filterJson");
var featuremodeljson = jQuery.parseJSON(featuremodel);
console.log(featuremodeljson);
var local = new kendo.data.HierarchicalDataSource({
data: featuremodeljson,
schema:{
model: {
id: "id",
children: "items"
}
}
});
$("#treeview").kendoTreeView({
dataSource: featuremodeljson,
dataTextField: "FullName",
checkboxes: true,
checkboxes: {
checkChildren: true
},
loadOnDemand: false,
});
I can see in the console,
console.log(featuremodeljson)
that this is a valid Json. I can see the element hierarchy. However, the kendo view only populates the elements in the first hierarchy level - and not their children.
The Json is something like this:
[{
"id": "No Feature",
"FullName": "No Feature",
"expanded": "true",
"hasChildren": "true",
"items": [ {
"id": "Feature0",
"FullName": "Feature0",
"expanded": "true",
"hasChildren": "true"
}]
},
{
"id": "Sensors",
"FullName": "Sensors",
"expanded": "true",
"hasChildren": "true",
"items": [{
"id": "Feature1",
"FullName": "Feature1",
"expanded": "true",
"hasChildren": "true"
}]
},
{
"id": "Warnings",
"FullName": "Warnings",
"expanded": "true",
"hasChildren": "true",
"items": [ {
"id": "Feature2",
"FullName": "Feature2",
"expanded": "true",
"hasChildren": "true"
}]
},
{
"id": "Languages",
"FullName": "Languages",
"expanded": "true",
"hasChildren": "true",
"items": [ {
"id": "Feature3",
"FullName": "Feature3",
"expanded": "true",
"hasChildren": "true"
}]
}]
Any ideas of what I'm doing wrong?
Thanks

I found the problem - The JSON is not correct. Both "expanded" and "hasChildren" require boolean attributes. Hence, the values are not "true" but true.
The Json should be something like this:
[{
"id": "No Feature",
"FullName": "No Feature",
"expanded": true,
"hasChildren": true,
"items": [ {
"id": "Feature0",
"FullName": "Feature0",
"expanded": true,
"hasChildren": true
}]
},
{
"id": "Sensors",
"FullName": "Sensors",
"expanded": true,
"hasChildren": true,
"items": [{
"id": "Feature1",
"FullName": "Feature1",
"expanded": true,
"hasChildren": true
}]
}]

Related

get object with the matching objects in sub array in mongoose

data store in the DB:
{
"_id": "61c1c0efc204bb170e280d2f",
"title": "Searching Relevant Cases",
"question": "Your Industry",
"vendor_id": "61b8324040fb21d80f3e6702",
"answer_type": "is_radio",
"optional_answer": false,
"sub_question_type": "none",
"view": "list_view",
"is_active": true,
"is_delete": false,
"sub_question": [],
"draggable_list": [],
"__v": 0
},
{
"_id": "61c1c142c204bb170e280d45",
"title": "Searching Relevant Cases",
"question": "Employee size",
"vendor_id": "61b8324040fb21d80f3e6702",
"answer_type": "is_radio",
"optional_answer": false,
"sub_question_type": "none",
"view": "list_view",
"is_active": true,
"is_delete": false,
"sub_question": [],
"draggable_list": [],
"__v": 0
},
{
"_id": "61c1c24ac204bb170e280d5e",
"title": "Dependent",
"question": "On previous Chosen option",
"vendor_id": "61b8324040fb21d80f3e6702",
"answer_type": "is_radio",
"optional_answer": false,
"sub_question_type": "has_parent",
"sub_question": [
{
"question_id": "61c1c0efc204bb170e280d2f",
"_id": "61c1c24ac204bb170e280d5f",
"answer": "Bank"
},
{
"question_id": "61c1c142c204bb170e280d45",
"_id": "61c1c24ac204bb170e280d60",
"answer": "SMB (<100)"
}
],
"view": "list_view",
"is_active": true,
"is_delete": false,
"draggable_list": [],
"__v": 0
},
{
"_id": "61c1c2d7c204bb170e280d7e",
"title": "Dependent 1",
"question": "On previous Chosen option 1",
"vendor_id": "61b8324040fb21d80f3e6702",
"answer_type": "is_radio",
"optional_answer": false,
"sub_question_type": "has_parent",
"sub_question": [
{
"question_id": "61c1c142c204bb170e280d45",
"_id": "61c1c2d7c204bb170e280d7f",
"answer": "Mid (100 - 1000)"
},
{
"question_id": "61c1c0efc204bb170e280d2f",
"_id": "61c1c2d7c204bb170e280d80",
"answer": "Tech"
}
],
"view": "list_view",
"is_active": true,
"is_delete": false,
"draggable_list": [],
"__v": 0
}
Query parameters are:
obj: {
"question_id": "61c1c0efc204bb170e280d2f",
"answer": "Bank"
},
obj1: {
"question_id": "61c1c142c204bb170e280d45",
"answer": "SMB (<100)"
}
The result I'm expecting is:
{
"_id": "61c1c24ac204bb170e280d5e",
"title": "Dependent",
"question": "On previous Chosen option",
"vendor_id": "61b8324040fb21d80f3e6702",
"answer_type": "is_radio",
"optional_answer": false,
"sub_question_type": "has_parent",
"sub_question": [
{
"question_id": "61c1c0efc204bb170e280d2f",
"_id": "61c1c24ac204bb170e280d5f",
"answer": "Bank"
},
{
"question_id": "61c1c142c204bb170e280d45",
"_id": "61c1c24ac204bb170e280d60",
"answer": "SMB (<100)"
}
],
"view": "list_view",
"is_active": true,
"is_delete": false,
"draggable_list": [],
"__v": 0
}
The answer what I get from the user (user:14732669) on StackOverflow is:
https://mongoplayground.net/p/pAV76ctwhfC
this query is working but not returning the resulted object instead it returns two objects.
I have to match the question_id as well as the answer of (obj and obj1) for both the objects which are in the sub_question array
$match
db.collection.aggregate([
{
"$match": {
"$and": [
{
"sub_question.question_id": "61c1c0efc204bb170e280d2f",
"sub_question.answer": "Bank"
},
{
"sub_question.question_id": "61c1c142c204bb170e280d45",
"sub_question.answer": "SMB (<100)"
}
]
}
}
])
mongoplayground

Parsing Nested JSON and Manipulating It in Ruby

This is my first attempt at parsing nested JSON with Ruby. I need to go through the JSON to pull out specific values for "_id", "name", and "type" for instance. I then need to create a reference table so that I can refer to each "_id" and associated information. I also need to combine information from multiple JSON responses. I've been able to get basic information and have tried a few things I've found online. I just need a little assistance with a starting point. If anyone has any ideas of where to start with this I'd really appreciate it.
Devices JSON response hash. Each device starts with _id.
{
"api": "1.0",
"error": null,
"id": "60b5d4c3077862123cfa4443",
"result": {
"devices": [
{
"_id": "123456787786211fd31f3dd",
"batteryPowered": true,
"category": "door_lock",
"deviceTypeId": "144_1_1",
"firmware": [
{
"id": "us.144.1_1.0",
"version": "2.6"
}
],
"gatewayId": "1234567807786214fbc6bd4e",
"info": {
"firmware.stack": "3.28",
"hardware": "0",
"manufacturer": "Kwikset",
"model": "912",
"protocol": "zwave",
"zwave.node": "2",
"zwave.smartstart": "no"
},
"name": "Garage Door",
"parentDeviceId": "",
"persistent": false,
"reachable": false,
"ready": true,
"roomId": "1234567807786211fd31f3eb",
"security": "middle",
"status": "idle",
"subcategory": "",
"type": "doorlock"
},
{
"_id": "1234567897786211fd31f3ed",
"batteryPowered": true,
"category": "door_lock",
"deviceTypeId": "59_1_1129",
"firmware": [
{
"id": "us.59.18064.0",
"version": "3.3"
},
{
"id": "us.59.18065.1",
"version": "11.0"
}
],
"gatewayId": "1234567897786214fbc6bd4e",
"info": {
"firmware.stack": "6.3",
"hardware": "3",
"manufacturer": "Schlage",
"model": "BE469ZP",
"protocol": "zwave",
"zwave.node": "3",
"zwave.smartstart": "no"
},
"name": "Front Door",
"parentDeviceId": "",
"persistent": false,
"reachable": true,
"ready": true,
"roomId": "1234567807786211fd31f3ec",
"security": "high",
"status": "idle",
"subcategory": "",
"type": "doorlock"
},
{
"_id": "1234567897786211fd31f40a",
"batteryPowered": false,
"category": "switch",
"deviceTypeId": "57_20562_12344",
"firmware": [
{
"id": "us.57.29240.0",
"version": "5.25"
}
],
"gatewayId": "1234567807786214fbc6bd4e",
"info": {
"firmware.stack": "4.54",
"hardware": "255",
"manufacturer": "Honeywell",
"model": "ZW4103/39337",
"protocol": "zwave",
"zwave.node": "4",
"zwave.smartstart": "no"
},
"name": "Lamp Switch",
"parentDeviceId": "",
"persistent": false,
"reachable": true,
"ready": true,
"roomId": "1234567807786211fd31f416",
"security": "no",
"status": "idle",
"subcategory": "interior_plugin",
"type": "switch.outlet"
},
{
"_id": "1234567b07786211fd31f40e",
"batteryPowered": false,
"category": "dimmable_light",
"deviceTypeId": "57_20548_12339",
"firmware": [
{
"id": "us.57.29747.0",
"version": "5.21"
}
],
"gatewayId": "1234567d07786214fbc6bd4e",
"info": {
"firmware.stack": "4.34",
"hardware": "255",
"manufacturer": "Honeywell",
"model": "39339/ZW3107",
"protocol": "zwave",
"zwave.node": "5",
"zwave.smartstart": "no"
},
"name": "Lamp Dimmer",
"parentDeviceId": "",
"persistent": false,
"reachable": true,
"ready": true,
"roomId": "1234567807786211fd31f416",
"security": "no",
"status": "idle",
"subcategory": "dimmable_plugged",
"type": "dimmer.outlet"
}
]
}
}
There is then also a JSON response that lists the functions for each device in the same format above. However instead of "devices"=> it is "items"=> and the beach function is the _id key again.
I'd like to combine function _id tags and descriptions with the device JSON, so I can create a way to send my script "unlock door lock 1" and it subs the number with the _id of the device and the function _id.
You can start with a very rough navigator function like this:
def find_device(data, name, index)
# Filter through the device list...
data['result']['devices'].select do |device|
# ...for matching names.
device.name == name
end[index] # Take indexed entry
end
Where now you can do find_device(data, 'door_lock', 0) to dig up that entry.
Converting "door lock 1" to [ 'door_lock', 0 ] should be pretty trivial:
def to_location(str)
# Split off the name component(s) and index number
*name, index = str.split(/\s+/)
# Reassemble with underscores and -1 to account for 0-index
[ name.join('_'), index.to_i - 1 ]
end

Azure Search Normalized Lowercase Field

I am unable to add a normalized copy of the "Title" field to our search index. Ultimately, I'm trying to use this field for case-insensitive order by. Currently, titles are returned in the following order (with $orderBy=TitleCaseInsensitive):
Abc
Bbc
abc
And instead I want: Abc->abc->Bbc. I have forked the "Title" field out into two fields via a Field Mapping and am then applying a Custom Analyzer with the "lowercase" tokenFilter, to the normalized field. Can someone explain why I am not getting the desired results? Here is the relevant portion of the index definition:
"index":{
"name": "current-local-inventory",
"fields": [
{"name": "TitleCaseInsensitive","indexAnalyzer":"caseInsensitiveAnalyzer","searchAnalyzer":"keyword", "type": "Edm.String","filterable": false, "sortable": true, "facetable": false, "searchable": true},
{"name": "Title", "type": "Edm.String","filterable": true, "sortable": true, "facetable": false, "searchable": true},
],
"analyzers": [
{
"#odata.type":"#Microsoft.Azure.Search.CustomAnalyzer",
"name":"caseInsensitiveAnalyzer",
"charFilters":[],
"tokenizer":"keyword_v2",
"tokenFilters":["lowercase"]
}
]
},
"indexers":[{
"fieldMappings" : [
{"sourceFieldName" : "Title", "targetFieldName" : "Title" },
{"sourceFieldName" : "Title", "targetFieldName" : "TitleCaseInsensitive" }
]
}]
See my answer in the related post Azure Search - Accent insensitive analyzer not working when sorting. When you include the lowercase token filter it only affects search and not sorting. See Azure Search User Voice entry Case-insensitive sorting for string fields
My suggested workaround as I explain in the related post is to create a forked/shadow property. However, using an analyzer with a lowercase token filter won't help. The only way I could get your example working was to include a copy of your Title property that was already lowercased. Notice that I don't use fieldMapping and I don't use different analyzers for indexing and search like you have in your example.
CREATE INDEX
Create the index. Replace variables wrapped in angle brackets as suitable for your env.
{
"#odata.context": "https://{{SEARCH_SVC}}.{{DNS_SUFFIX}}/$metadata#indexes/$entity",
"#odata.etag": "\"0x8D8761DCBBCCD00\"",
"name": "{{INDEX_NAME}}",
"defaultScoringProfile": null,
"fields": [
{"name": "Id", "type": "Edm.String", "searchable": false, "filterable": true, "retrievable": true, "sortable": true, "facetable": false, "key": true, "indexAnalyzer": null, "searchAnalyzer": null, "analyzer": null, "synonymMaps": [] },
{"name": "TitleCaseInsensitive","indexAnalyzer": null, "searchAnalyzer": null, "analyzer": "caseInsensitiveAnalyzer", "type": "Edm.String","filterable": false, "sortable": true, "facetable": false, "searchable": true},
{"name": "Title", "type": "Edm.String","filterable": true, "sortable": true, "facetable": false, "searchable": true}
],
"scoringProfiles": [],
"corsOptions": null,
"suggesters": [],
"analyzers": [ {
"#odata.type":"#Microsoft.Azure.Search.CustomAnalyzer",
"name":"caseInsensitiveAnalyzer",
"charFilters":[],
"tokenizer":"keyword_v2",
"tokenFilters":["lowercase"]
}],
"tokenizers": [],
"tokenFilters": [],
"charFilters": [],
"encryptionKey": null
}
UPLOAD
Upload three sample documents.
{
"value": [
{
"#search.action": "mergeOrUpload",
"Id": "1",
"Title": "Abc",
"TitleCaseInsensitive": "abc"
},
{
"#search.action": "mergeOrUpload",
"Id": "2",
"Title": "abc",
"TitleCaseInsensitive": "abc"
},
{
"#search.action": "mergeOrUpload",
"Id": "3",
"Title": "Bbc",
"TitleCaseInsensitive": "bbc"
}
]
}
QUERY
Then, query with $orderby on your lowercased (normalized) property.
https://{{SEARCH_SVC}}.{{DNS_SUFFIX}}/indexes/{{INDEX_NAME}}/docs?search=*&$count=true&$select=Id,Title,TitleCaseInsensitive&searchMode=all&queryType=full&api-version={{API-VERSION}}&$orderby=TitleCaseInsensitive asc
And you'll get the expected results where Title is sorted in a case-insensitive way.
{
"#odata.context": "https://<your-search-service>.search.windows.net/indexes('dg-test-65526118')/$metadata#docs(*)",
"#odata.count": 3,
"value": [
{
"#search.score": 1.0,
"Id": "2",
"TitleCaseInsensitive": "abc",
"Title": "abc"
},
{
"#search.score": 1.0,
"Id": "1",
"TitleCaseInsensitive": "abc",
"Title": "Abc"
},
{
"#search.score": 1.0,
"Id": "3",
"TitleCaseInsensitive": "bbc",
"Title": "Bbc"
}
]
}
I would love to be corrected with a simple way to accomplish this.
Please check out the Text normalization for case-insensitive filtering, faceting and sorting feature that's in Preview.
You can update your index to use this "normalizer" feature for the fields in which you'd like case-insensitive order-by operations.
You don't need a separate field TitleCaseInsensitive anymore. You can add "normalizer": "lowercase" to the Title field, and $orderBy=Title will sort in the order you'd like, ignoring casing.
The "lowercase" normalizer is pre-defined. If you'd like other filters to be applied, please look at predefined and custom normalizers
"index": {
"name": "current-local-inventory",
"fields": [
{"name": "Title", "type": "Edm.String", "filterable": true, "sortable": true, "facetable": false, "searchable": true, "normalizer":"lowercase"}
]
},
"indexers":[{
"fieldMappings" : [
{"sourceFieldName" : "Title", "targetFieldName" : "Title" }
]
}]

How can I ensure alphabetical order with json schema of json objects in json arrays?

I would like to ensure that json objects within a json array are ordered correctly by a specific property with a json schema.
Is that possible? And if so, how can I create such a json schema?
Schema:
{
"type": "object",
"properties": {
"cities": {
"type": "array",
"items": {
"type": "object",
"properties": {
"shortName": {
"type": "string"
},
"name": {
"type": "string"
},
"showInMap": {
"type": "boolean"
},
"active": {
"type": "boolean"
}
},
"??ORDERBY??": "shortName",
"??ORDER??": "ASC",
"required": [
"shortName"
]
}
}
}
}
I would like to filter out json files which are not correctly ordered.
Example: (invalid)
{
"cities": [
{
"shortName": "NY",
"name": "New York",
"showInMap": true,
"active": true
},
{
"shortName": "LD",
"name": "London",
"showInMap": true,
"active": false
},
{
"shortName": "MO",
"name": "Moscow",
"showInMap": false,
"active": false
}
]
}
And accept json files which are ordered correctly.
Example: (valid)
{
"cities": [
{
"shortName": "LD",
"name": "London",
"showInMap": true,
"active": false
},
{
"shortName": "MO",
"name": "Moscow",
"showInMap": false,
"active": false
},{
"shortName": "NY",
"name": "New York",
"showInMap": true,
"active": true
}
]
}
EDIT: This answer makes no use of json schema.
Here is a minimal solution working on your sample data. It is not safe for production, as it is missing all sorts of checks for undefined, but I think you can enhance it at will.
function isSorted(array, sortKey) {
return array.reduce((ordered, item, index) => {
return index > array.length - 2 ? ordered : ordered && item[sortKey] < array[index + 1][sortKey];
}, true);
}
const incorrectCase = [{
"shortName": "NY",
"name": "New York",
"showInMap": true,
"active": true
},
{
"shortName": "LD",
"name": "London",
"showInMap": true,
"active": false
},
{
"shortName": "MO",
"name": "Moscow",
"showInMap": false,
"active": false
}
]
const correctCase = [{
"shortName": "LD",
"name": "London",
"showInMap": true,
"active": false
},
{
"shortName": "MO",
"name": "Moscow",
"showInMap": false,
"active": false
}, {
"shortName": "NY",
"name": "New York",
"showInMap": true,
"active": true
}
];
console.log('incorrect case: ', isSorted(incorrectCase, "shortName"));
console.log('correct case: ', isSorted(correctCase, "shortName"));

Join 2 arrays by key and value (AngularJS)

I have 2 objects
{
"_id": "58b7f36b3354c24630f6f3b0",
"name": "refcode",
"caption": "Reference",
"type": "string",
"search": false,
"required": false,
"table": true,
"expansion": true
},
and
{
"_id": "58b7f36b3354c24630f6f3c8",
"vacancyid": "0",
"refcode": "THIS IS MY REF",
"position": "Test",
"jobtype": "Temp",
"department": "Industrial",
"branch": "Office",
"startdate": "02/12/2013",
"contactname": "Person Name",
"contactemail": "person#domain",
"Q_V_TYP": "Daily",
"score": 0
},
Object one defines what a field should be and what it is called
The second object is a job description.
What i need is to match a field to each key (this even sounds confusing i my head, so here is an example)
{
"_id": "58b7f36b3354c24630f6f3c8",
"vacancyid": "0",
"refcode": {
"_id": "58b7f36b3354c24630f6f3b0",
"name": "refcode",
"caption": "Reference",
"type": "string",
"search": false,
"required": false,
"table": true,
"expansion": true,
"value": "THIS IS MY REF"
}
},
"position": "Test",
"jobtype": "Temp",
"department": "Industrial",
"branch": "Office",
"startdate": "02/12/2013",
"contactname": "Person Name",
"contactemail": "person#domain",
"Q_V_TYP": "Daily",
"score": 0
},
Here you go:
var def = {
"_id": "58b7f36b3354c24630f6f3b0",
"name": "refcode",
"caption": "Reference",
"type": "string",
"search": false,
"required": false,
"table": true,
"expansion": true
};
var jobDesc = {
"_id": "58b7f36b3354c24630f6f3c8",
"vacancyid": "0",
"refcode": "THIS IS MY REF",
"position": "Test",
"jobtype": "Temp",
"department": "Industrial",
"branch": "Office",
"startdate": "02/12/2013",
"contactname": "Person Name",
"contactemail": "person#domain",
"Q_V_TYP": "Daily",
"score": 0
};
var jobDescKeysArr = Object.keys(jobDesc);
if (jobDescKeysArr.indexOf(def.name) !== -1) {
// A match.
def.value = jobDesc[def.name];
jobDesc[def.name] = Object.assign({}, def);
console.log(jobDesc)
}

Resources