I have a nested JSON object where I am trying to search for an attribute value. If there is a match, I would like to print the entire JSONPath just in the form of XPath.
Below is my JSON:
{
"_declaration": {
"_attributes": {
"version": "1.0",
"encoding": "UTF-8",
"standalone": "yes"
}
},
"hierarchy": {
"_attributes": {
"index": "0",
"class": "hierarchy",
"rotation": "0",
"width": "1440",
"height": "3036"
},
"android.widget.FrameLayout": {
"_attributes": {
"index": "0",
"package": "com.google.android.permissioncontroller",
"class": "android.widget.FrameLayout",
"text": "",
"checkable": "false",
"checked": "false",
"clickable": "false",
"enabled": "true",
"focusable": "false",
"focused": "false",
"long-clickable": "false",
"password": "false",
"scrollable": "false",
"selected": "false",
"bounds": "[37,1017][1403,2102]",
"displayed": "true"
},
"android.widget.FrameLayout": {
"_attributes": {
"index": "0",
"package": "com.google.android.permissioncontroller",
"class": "android.widget.FrameLayout",
"text": "",
"checkable": "false",
"checked": "false",
"clickable": "false",
"enabled": "true",
"focusable": "false",
"focused": "false",
"long-clickable": "false",
"password": "false",
"scrollable": "false",
"selected": "false",
"bounds": "[37,1017][1403,2102]",
"displayed": "true"
},
"android.widget.FrameLayout": {
"_attributes": {
"index": "0",
"package": "com.google.android.permissioncontroller",
"class": "android.widget.FrameLayout",
"text": "",
"resource-id": "android:id/content",
"checkable": "false",
"checked": "false",
"clickable": "false",
"enabled": "true",
"focusable": "false",
"focused": "false",
"long-clickable": "false",
"password": "false",
"scrollable": "false",
"selected": "false",
"bounds": "[37,1017][1403,2102]",
"displayed": "true"
},
"android.widget.ScrollView": {
"_attributes": {
"index": "0",
"package": "com.google.android.permissioncontroller",
"class": "android.widget.ScrollView",
"text": "",
"checkable": "false",
"checked": "false",
"clickable": "false",
"enabled": "true",
"focusable": "true",
"focused": "false",
"long-clickable": "false",
"password": "false",
"scrollable": "false",
"selected": "false",
"bounds": "[37,1017][1403,2102]",
"displayed": "true"
},
"android.widget.LinearLayout": {
"_attributes": {
"index": "0",
"package": "com.google.android.permissioncontroller",
"class": "android.widget.LinearLayout",
"text": "",
"resource-id": "com.android.permissioncontroller:id/grant_singleton",
"checkable": "false",
"checked": "false",
"clickable": "true",
"enabled": "true",
"focusable": "false",
"focused": "false",
"long-clickable": "false",
"password": "false",
"scrollable": "false",
"selected": "false",
"bounds": "[37,1017][1403,2102]",
"displayed": "true"
},
"android.widget.LinearLayout": {
"_attributes": {
"index": "0",
"package": "com.google.android.permissioncontroller",
"class": "android.widget.LinearLayout",
"text": "",
"resource-id": "com.android.permissioncontroller:id/grant_dialog",
"checkable": "false",
"checked": "false",
"clickable": "true",
"enabled": "true",
"focusable": "true",
"focused": "false",
"long-clickable": "false",
"password": "false",
"scrollable": "false",
"selected": "false",
"bounds": "[37,1017][1403,2102]",
"displayed": "true"
},
"android.widget.LinearLayout": [
{
"_attributes": {
"index": "0",
"package": "com.google.android.permissioncontroller",
"class": "android.widget.LinearLayout",
"text": "",
"resource-id": "com.android.permissioncontroller:id/content_container",
"checkable": "false",
"checked": "false",
"clickable": "false",
"enabled": "true",
"focusable": "false",
"focused": "false",
"long-clickable": "false",
"password": "false",
"scrollable": "false",
"selected": "false",
"bounds": "[93,1073][1347,1549]",
"displayed": "true"
},
"android.widget.LinearLayout": {
"_attributes": {
"index": "0",
"package": "com.google.android.permissioncontroller",
"class": "android.widget.LinearLayout",
"text": "",
"checkable": "false",
"checked": "false",
"clickable": "false",
"enabled": "true",
"focusable": "false",
"focused": "false",
"long-clickable": "false",
"password": "false",
"scrollable": "false",
"selected": "false",
"bounds": "[177,1136][1263,1465]",
"displayed": "true"
},
"android.widget.ImageView": {
"_attributes": {
"index": "0",
"package": "com.google.android.permissioncontroller",
"class": "android.widget.ImageView",
"text": "",
"resource-id": "com.android.permissioncontroller:id/permission_icon",
"checkable": "false",
"checked": "false",
"clickable": "false",
"enabled": "true",
"focusable": "false",
"focused": "false",
"long-clickable": "false",
"password": "false",
"scrollable": "false",
"selected": "false",
"bounds": "[664,1136][776,1248]",
"displayed": "true"
}
},
"android.widget.TextView": {
"_attributes": {
"index": "1",
"package": "com.google.android.permissioncontroller",
"class": "android.widget.TextView",
"text": "Allow Oscar to send you notifications?",
"resource-id": "com.android.permissioncontroller:id/permission_message",
"checkable": "false",
"checked": "false",
"clickable": "false",
"enabled": "true",
"focusable": "false",
"focused": "false",
"long-clickable": "false",
"password": "false",
"scrollable": "false",
"selected": "false",
"bounds": "[177,1290][1263,1465]",
"displayed": "true"
}
}
}
},
{
"_attributes": {
"index": "1",
"package": "com.google.android.permissioncontroller",
"class": "android.widget.LinearLayout",
"text": "",
"checkable": "false",
"checked": "false",
"clickable": "false",
"enabled": "true",
"focusable": "false",
"focused": "false",
"long-clickable": "false",
"password": "false",
"scrollable": "false",
"selected": "false",
"bounds": "[93,1549][1347,2046]",
"displayed": "true"
},
"android.widget.Button": [
{
"_attributes": {
"index": "0",
"package": "com.google.android.permissioncontroller",
"class": "android.widget.Button",
"text": "Allow",
"resource-id": "com.android.permissioncontroller:id/permission_allow_button",
"checkable": "false",
"checked": "false",
"clickable": "true",
"enabled": "true",
"focusable": "true",
"focused": "false",
"long-clickable": "false",
"password": "false",
"scrollable": "false",
"selected": "false",
"bounds": "[177,1556][1263,1752]",
"displayed": "true"
}
},
{
"_attributes": {
"index": "1",
"package": "com.google.android.permissioncontroller",
"class": "android.widget.Button",
"text": "Don’t allow",
"resource-id": "com.android.permissioncontroller:id/permission_deny_button",
"checkable": "false",
"checked": "false",
"clickable": "true",
"enabled": "true",
"focusable": "true",
"focused": "false",
"long-clickable": "false",
"password": "false",
"scrollable": "false",
"selected": "false",
"bounds": "[177,1766][1263,1962]",
"displayed": "true"
}
}
]
}
]
}
}
}
}
}
}
}
}
I would like to find the path to the element, which has property "text: "Don't allow" for its _attributes key.
So, my expected output will be:
/hierarchy/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.ScrollView/android.widget.LinearLayout/android.widget.LinearLayout/android.widget.LinearLayout[2]/android.widget.Button[2]
I am using Javascript.
I am receiving a dynamic data string from the python kernel, (however, it's an object but the python kernel can only send it as a sting due to the limitation of the python backend that cant be changed due to a third-party package), below is the console.log output.
[{"model_class": "DataToolParameter", "name": "input_file", "argument": null, "type": "data", "label": "VCF/BCF Data", "help": "", "refresh_on_change": true, "optional": false, "hidden": false, "is_dynamic": false, "value": null, "extensions": ["vcf", "vcf_bgzip", "bcf"], "edam": {"edam_formats": ["format_3016", "format_3016", "format_3020"], "edam_data": ["data_0006", "data_0006", "data_3498"]}, "multiple": false, "options": {"hda": [], "hdca": []}, "default_value": null, "text_value": "No dataset."}, {"model_class": "Section", "name": "sec_required", "type": "section", "title": "Required References", "help": null, "expanded": true, "inputs": [{"model_class": "Conditional", "name": "reference_source", "type": "conditional", "cases": [{"model_class": "ConditionalWhen", "value": "cached", "inputs": [{"model_class": "SelectToolParameter", "name": "fasta_ref", "argument": null, "type": "select", "label": "Reference genome", "help": "", "refresh_on_change": false, "optional": false, "hidden": false, "is_dynamic": true, "value": null, "options": [], "display": null, "multiple": false, "textable": false, "default_value": null, "text_value": "None"}]}, {"model_class": "ConditionalWhen", "value": "history", "inputs": [{"model_class": "DataToolParameter", "name": "fasta_ref", "argument": null, "type": "data", "label": "Reference genome", "help": "", "refresh_on_change": true, "optional": false, "hidden": false, "is_dynamic": false, "value": {"values": [{"id": "0b378429c2c91c4f", "src": "hda"}]}, "extensions": ["fasta"], "edam": {"edam_formats": ["format_1929"], "edam_data": ["data_2044"]}, "multiple": false, "options": {"hda": [{"id": "0b378429c2c91c4f", "hid": 11, "name": "UCSC Main on Human: multiz100way (chrX:15,578,261-15,621,068) (as fasta)", "tags": [], "src": "hda", "keep": false}, {"id": "33d7a2c94fd36485", "hid": 9, "name": "non_ACPs.fasta", "tags": [], "src": "hda", "keep": false}, {"id": "47f5e6f7acd01631", "hid": 3, "name": "Extract MAF blocks on data 2 (as fasta)", "tags": [], "src": "hda", "keep": false}, {"id": "695fd3e24b533dbc", "hid": 2, "name": "UCSC Main on Human: multiz100way (chrX:15,560,138-15,602,945) (as fasta)", "tags": [], "src": "hda", "keep": false}], "hdca": []}, "default_value": {"values": [{"id": "0b378429c2c91c4f", "src": "hda"}]}, "text_value": "No dataset."}]}], "test_param": {"model_class": "SelectToolParameter", "name": "reference_source_selector", "argument": null, "type": "select", "label": "Choose the source for the reference genome", "help": "", "refresh_on_change": true, "optional": false, "hidden": false, "is_dynamic": false, "value": "cached", "options": [["Use a built-in genome", "cached", false], ["Use a genome from the history", "history", false]], "display": null, "multiple": false, "textable": false, "text_value": "Use a built-in genome"}}, {"model_class": "DataToolParameter", "name": "gff_annot", "argument": null, "type": "data", "label": "GFF3 annotation file", "help": "From Ensembl: ftp://ftp.ensembl.org/pub/current_gff3/", "refresh_on_change": true, "optional": false, "hidden": false, "is_dynamic": false, "value": null, "extensions": ["gff3"], "edam": {"edam_formats": ["format_1975"], "edam_data": ["data_1255"]}, "multiple": false, "options": {"hda": [], "hdca": []}, "default_value": null, "text_value": "No dataset."}]}, {"model_class": "Section", "name": "sec_csq_opts", "type": "section", "title": "CSQ Options", "help": null, "expanded": true, "inputs": [{"model_class": "IntegerToolParameter", "name": "ncsq", "argument": null, "type": "integer", "label": "maximum number of consequences to consider per site", "help": "-ncsq 16", "refresh_on_change": false, "min": 1, "max": 50, "optional": false, "hidden": false, "is_dynamic": false, "value": "16", "area": false, "datalist": [], "default_value": "16", "text_value": "16"}, {"model_class": "BooleanToolParameter", "name": "local_csq", "argument": null, "type": "boolean", "label": "run localized predictions considering only one VCF record at a time", "help": "--local-csq switch off haplotype-aware calling, run localized predictions considering only one VCF record at a time", "refresh_on_change": false, "optional": false, "hidden": false, "is_dynamic": false, "value": "false", "truevalue": "--local-csq", "falsevalue": "", "default_value": "false", "text_value": "false"}, {"model_class": "SelectToolParameter", "name": "phase", "argument": null, "type": "select", "label": "phase", "help": "how to construct haplotypes and how to deal with unphased data", "refresh_on_change": false, "optional": true, "hidden": false, "is_dynamic": false, "value": null, "options": [["take GTs as is, create haplotypes regardless of phase (0/1 -> 0|1)", "a", false], ["merge *all* GTs into a single haplotype (0/1 -> 1, 1/2 -> 1)", "m", false], ["require phased GTs, throw an error on unphased het GTs", "r", false], ["create non-reference haplotypes if possible (0/1 -> 1|1, 1/2 -> 1|2)", "R", false], ["skip unphased GTs", "s", false]], "display": null, "multiple": false, "textable": false, "default_value": null, "text_value": "Nothing selected."}, {"model_class": "TextToolParameter", "name": "custom_tag", "argument": null, "type": "text", "label": "use this custom tag to store consequences rather than the default BCSQ tag", "help": "", "refresh_on_change": false, "optional": true, "hidden": false, "is_dynamic": false, "value": "", "area": false, "datalist": [], "default_value": "", "text_value": "Empty."}]}, {"model_class": "Section", "name": "sec_restrict", "type": "section", "title": "Restrict to", "help": null, "expanded": false, "inputs": [{"model_class": "Conditional", "name": "regions", "type": "conditional", "cases": [{"model_class": "ConditionalWhen", "value": "__none__", "inputs": []}, {"model_class": "ConditionalWhen", "value": "regions", "inputs": [{"model_class": "Repeat", "name": "regions", "type": "repeat", "title": "Region Filter", "help": null, "default": 1, "min": 1, "max": "__Infinity__", "inputs": [{"model_class": "TextToolParameter", "name": "chrom", "argument": null, "type": "text", "label": "Region chromosome", "help": "", "refresh_on_change": false, "optional": false, "hidden": false, "is_dynamic": false, "value": "", "area": false, "datalist": []}, {"model_class": "TextToolParameter", "name": "start", "argument": null, "type": "text", "label": "Region start position", "help": "", "refresh_on_change": false, "optional": false, "hidden": false, "is_dynamic": false, "value": "", "area": false, "datalist": []}, {"model_class": "TextToolParameter", "name": "stop", "argument": null, "type": "text", "label": "Region end position", "help": "", "refresh_on_change": false, "optional": false, "hidden": false, "is_dynamic": false, "value": "", "area": false, "datalist": []}], "cache": {}}]}, {"model_class": "ConditionalWhen", "value": "regions_file", "inputs": [{"model_class": "DataToolParameter", "name": "regions_file", "argument": null, "type": "data", "label": "Regions File", "help": "restrict to Regions listed in a file", "refresh_on_change": true, "optional": false, "hidden": false, "is_dynamic": false, "value": {"values": [{"id": "0b378429c2c91c4f", "src": "hda"}]}, "extensions": ["tabular"], "edam": {"edam_formats": ["format_3475"], "edam_data": ["data_0006"]}, "multiple": false, "options": {"hda": [{"id": "0b378429c2c91c4f", "hid": 11, "name": "UCSC Main on Human: multiz100way (chrX:15,578,261-15,621,068) (as interval)", "tags": [], "src": "hda", "keep": false}, {"id": "955b0e088867ea92", "hid": 10, "name": "UCSC Main on Human: knownGene (genome)", "tags": [], "src": "hda", "keep": false}, {"id": "33d7a2c94fd36485", "hid": 9, "name": "non_ACPs.fasta (as tabular)", "tags": [], "src": "hda", "keep": false}, {"id": "3ba2ccc043967969", "hid": 3, "name": "Extract MAF blocks on data 2 (as interval)", "tags": [], "src": "hda", "keep": false}, {"id": "57c61e66a8a42935", "hid": 2, "name": "UCSC Main on Human: multiz100way (chrX:15,560,138-15,602,945) (as interval)", "tags": [], "src": "hda", "keep": false}, {"id": "1d6404f66ed3d72e", "hid": 1, "name": "UCSC Main on Human: knownGene (genome)", "tags": [], "src": "hda", "keep": false}], "hdca": []}, "default_value": {"values": [{"id": "0b378429c2c91c4f", "src": "hda"}]}, "text_value": "No dataset."}]}], "test_param": {"model_class": "SelectToolParameter", "name": "regions_src", "argument": null, "type": "select", "label": "Regions", "help": "", "refresh_on_change": true, "optional": false, "hidden": false, "is_dynamic": false, "value": "__none__", "options": [["Do not restrict to Regions", "__none__", false], ["Specify one or more Region(s) directly", "regions", false], ["Operate on Regions specified in a history dataset", "regions_file", false]], "display": null, "multiple": false, "textable": false, "text_value": "Do not restrict to Regions"}}, {"model_class": "Conditional", "name": "targets", "type": "conditional", "cases": [{"model_class": "ConditionalWhen", "value": "__none__", "inputs": []}, {"model_class": "ConditionalWhen", "value": "targets", "inputs": [{"model_class": "Repeat", "name": "targets", "type": "repeat", "title": "Target Filter", "help": null, "default": 1, "min": 1, "max": "__Infinity__", "inputs": [{"model_class": "TextToolParameter", "name": "chrom", "argument": null, "type": "text", "label": "Target chromosome", "help": "", "refresh_on_change": false, "optional": false, "hidden": false, "is_dynamic": false, "value": "", "area": false, "datalist": []}, {"model_class": "TextToolParameter", "name": "start", "argument": null, "type": "text", "label": "Target start position", "help": "", "refresh_on_change": false, "optional": false, "hidden": false, "is_dynamic": false, "value": "", "area": false, "datalist": []}, {"model_class": "TextToolParameter", "name": "stop", "argument": null, "type": "text", "label": "Target end position", "help": "", "refresh_on_change": false, "optional": false, "hidden": false, "is_dynamic": false, "value": "", "area": false, "datalist": []}], "cache": {}}]}, {"model_class": "ConditionalWhen", "value": "targets_file", "inputs": [{"model_class": "DataToolParameter", "name": "targets_file", "argument": null, "type": "data", "label": "Targets File", "help": "restrict to Targets listed in a file", "refresh_on_change": true, "optional": false, "hidden": false, "is_dynamic": false, "value": {"values": [{"id": "0b378429c2c91c4f", "src": "hda"}]}, "extensions": ["tabular"], "edam": {"edam_formats": ["format_3475"], "edam_data": ["data_0006"]}, "multiple": false, "options": {"hda": [{"id": "0b378429c2c91c4f", "hid": 11, "name": "UCSC Main on Human: multiz100way (chrX:15,578,261-15,621,068) (as interval)", "tags": [], "src": "hda", "keep": false}, {"id": "955b0e088867ea92", "hid": 10, "name": "UCSC Main on Human: knownGene (genome)", "tags": [], "src": "hda", "keep": false}, {"id": "33d7a2c94fd36485", "hid": 9, "name": "non_ACPs.fasta (as tabular)", "tags": [], "src": "hda", "keep": false}, {"id": "3ba2ccc043967969", "hid": 3, "name": "Extract MAF blocks on data 2 (as interval)", "tags": [], "src": "hda", "keep": false}, {"id": "57c61e66a8a42935", "hid": 2, "name": "UCSC Main on Human: multiz100way (chrX:15,560,138-15,602,945) (as interval)", "tags": [], "src": "hda", "keep": false}, {"id": "1d6404f66ed3d72e", "hid": 1, "name": "UCSC Main on Human: knownGene (genome)", "tags": [], "src": "hda", "keep": false}], "hdca": []}, "default_value": {"values": [{"id": "0b378429c2c91c4f", "src": "hda"}]}, "text_value": "No dataset."}]}], "test_param": {"model_class": "SelectToolParameter", "name": "targets_src", "argument": null, "type": "select", "label": "Targets", "help": "", "refresh_on_change": true, "optional": false, "hidden": false, "is_dynamic": false, "value": "__none__", "options": [["Do not restrict to Targets", "__none__", false], ["Specify one or more Target(s) directly", "targets", false], ["Operate on Targets specified in a history dataset", "targets_file", false]], "display": null, "multiple": false, "textable": false, "text_value": "Do not restrict to Targets"}}, {"model_class": "TextToolParameter", "name": "include", "argument": "--include", "type": "text", "label": "Include", "help": "Select sites for which the expression is true", "refresh_on_change": false, "optional": true, "hidden": false, "is_dynamic": false, "value": null, "area": false, "datalist": [], "default_value": null, "text_value": "Not available."}, {"model_class": "TextToolParameter", "name": "exclude", "argument": "--exclude", "type": "text", "label": "Exclude", "help": "Exclude sites for which the expression is true", "refresh_on_change": false, "optional": true, "hidden": false, "is_dynamic": false, "value": null, "area": false, "datalist": [], "default_value": null, "text_value": "Not available."}]}, {"model_class": "Section", "name": "sec_subset", "type": "section", "title": "Subset Options", "help": null, "expanded": false, "inputs": [{"model_class": "TextToolParameter", "name": "samples", "argument": "--samples", "type": "text", "label": "Samples", "help": "Comma separated list of samples to annotate (or exclude)", "refresh_on_change": false, "optional": true, "hidden": false, "is_dynamic": false, "value": "", "area": false, "datalist": [], "default_value": "", "text_value": "Empty."}, {"model_class": "BooleanToolParameter", "name": "invert_samples", "argument": null, "type": "boolean", "label": "Invert Samples", "help": "Inverts the query/filtering applied by Samples (adds \\"^\\" prefix to exclude)", "refresh_on_change": false, "optional": false, "hidden": false, "is_dynamic": false, "value": "false", "truevalue": "^", "falsevalue": "", "default_value": "false", "text_value": "false"}, {"model_class": "DataToolParameter", "name": "samples_file", "argument": "--samples_file", "type": "data", "label": "Samples file", "help": "File of samples to include", "refresh_on_change": true, "optional": true, "hidden": false, "is_dynamic": false, "value": null, "extensions": ["tabular"], "edam": {"edam_formats": ["format_3475"], "edam_data": ["data_0006"]}, "multiple": false, "options": {"hda": [{"id": "0b378429c2c91c4f", "hid": 11, "name": "UCSC Main on Human: multiz100way (chrX:15,578,261-15,621,068) (as interval)", "tags": [], "src": "hda", "keep": false}, {"id": "955b0e088867ea92", "hid": 10, "name": "UCSC Main on Human: knownGene (genome)", "tags": [], "src": "hda", "keep": false}, {"id": "33d7a2c94fd36485", "hid": 9, "name": "non_ACPs.fasta (as tabular)", "tags": [], "src": "hda", "keep": false}, {"id": "3ba2ccc043967969", "hid": 3, "name": "Extract MAF blocks on data 2 (as interval)", "tags": [], "src": "hda", "keep": false}, {"id": "57c61e66a8a42935", "hid": 2, "name": "UCSC Main on Human: multiz100way (chrX:15,560,138-15,602,945) (as interval)", "tags": [], "src": "hda", "keep": false}, {"id": "1d6404f66ed3d72e", "hid": 1, "name": "UCSC Main on Human: knownGene (genome)", "tags": [], "src": "hda", "keep": false}], "hdca": []}, "default_value": null, "text_value": "No dataset."}, {"model_class": "BooleanToolParameter", "name": "invert_samples_file", "argument": null, "type": "boolean", "label": "Invert Samples file", "help": "inverts the query/filtering applied by Samples file", "refresh_on_change": false, "optional": false, "hidden": false, "is_dynamic": false, "value": "false", "truevalue": "^", "falsevalue": "", "default_value": "false", "text_value": "false"}]}, {"model_class": "SelectToolParameter", "name": "output_type", "argument": null, "type": "select", "label": "", "help": "", "refresh_on_change": false, "optional": false, "hidden": false, "is_dynamic": false, "value": "b", "options": [["compressed BCF", "b", false], ["uncompressed VCF", "v", false]], "display": null, "multiple": false, "textable": false, "default_value": "b", "text_value": "compressed BCF"}]
I am trying to convert this dynamic string to an object using
JSON.parse(Kernel_inputs_as_string)
It works perfectly for all the cases except a few in which string contains "^" char. It throws an error.
SyntaxError: Unexpected token ^ in JSON at position 13477 error
On the other hand, when I copy and save the console.log string as input and trying JSON.parse() it work perfectly as described below.
const json = '[{"model_class": "DataToolParameter", "name": "input_file".....}]'
const obj = JSON.parse(json);
console.log(obj[0]);
I'm not able to understand why this error is coming and how to solve it for similar cases where the string has "^" char.
Please help me to solve this error.
Thank you
I am trying to convert a working JavaScript/Jquery feature to AngularJS (1.0) so I can add it to a fairly large AngularJS application. I am having some issues getting data to the view using ng-repeat and not sure where my issue is. I was reading through $index documentation and not sure if this is the source of my issue or what I need to do. Also not getting anything in the console for the response.data.deviceList object.
Possible I have a few issues going on here.
HTML
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Building Layout - Drag and Drop</title>
<link rel="stylesheet" type="text/css" href="main.css">
<script src="interact.js"></script>
<script src="angular.min.js"></script>
<script src="mainScripts.js"></script>
</head>
<body>
<div ng-app="mainApp">
<div class="tile draggable" ng-controller="mainScripts" ng-repeat="stuff in titles">
<ul>
<li>{{stuff.location}}</li>
<li>{{stuff.description}}</li>
</ul>
</div>
</div>
</body>
</html>
JavaScript
var mainApp = angular.module('mainApp',[]);
mainApp.controller('mainScripts', function($scope, $http) {
$http.get('devices.json').then(function successCallback(response){
console.log("API call works!");
console.dir(response.data.deviceList);
var titles = response.data.deviceList;
});
}, function errorCallback(response) {
console.log("API call doesn't work");
});
JSON
{
"deviceTypes": [{
"description": "Air Handler",
"typeName": "AirSource"
}, {
"description": "VAV Terminal",
"typeName": "AirTerminal"
}, {
"description": "Fan Coil",
"typeName": "ChilledWaterTerminal"
}, {
"description": "Chiller",
"typeName": "ChilledWaterSource"
}, {
"description": "Generic Unit",
"typeName": "NoResources"
}, {
"description": "Other Source",
"typeName": "OtherSource"
}, {
"description": "Other Terminal",
"typeName": "OtherTerminal"
}, {
"description": "Water Manager",
"typeName": "WaterSource"
}, {
"description": "WSHP Terminal",
"typeName": "WaterTerminal"
}],
"deviceList": [{
"href": "../MISRest/devices/3101117",
"location": "Loc Desk 3 VAV",
"description": "VAV 117",
"objectName": "VAV 117",
"instance": "3101117",
"occupancy": "Occupied",
"schedule": "Standard Schedule",
"ignore": "False",
"commStatus": "None",
"alarmStatus": "None",
"macaddress": "117",
"directSchedule": "True",
"rogueZone": "False",
"parentID": {
"air": "0"
},
"deviceType": ["AirTerminal"]
}, {
"href": "../MISRest/devices/3101121",
"location": "Loc Desk 4 with temp VAV",
"description": "VAV 121",
"objectName": "VAV Actuator Series 2 Bacnet ASC Controller",
"instance": "3101121",
"occupancy": "Error",
"schedule": "Standard Schedule",
"ignore": "False",
"commStatus": "Fault",
"alarmStatus": "Active",
"macaddress": "121",
"directSchedule": "True",
"rogueZone": "False",
"parentID": {
"air": "0"
},
"deviceType": ["AirTerminal"]
}, {
"href": "../MISRest/devices/3101004",
"location": "New Paris",
"description": "KMC Device",
"objectName": "BAC-8205_001635",
"instance": "3101004",
"occupancy": "Error",
"schedule": "Standard Schedule",
"ignore": "False",
"commStatus": "None",
"alarmStatus": "None",
"macaddress": "4",
"directSchedule": "True",
"rogueZone": "False",
"deviceType": ["NoResources"]
}, {
"href": "../MISRest/devices/3101013",
"location": "Default Location",
"description": "VAV-013",
"objectName": "DEFAULT",
"instance": "3101013",
"occupancy": "Occupied",
"schedule": "None",
"ignore": "False",
"commStatus": "None",
"alarmStatus": "None",
"macaddress": "13",
"directSchedule": "True",
"rogueZone": "False",
"parentID": {
"air": "0"
},
"deviceType": ["AirTerminal"]
}, {
"href": "../MISRest/devices/3101066",
"location": "Loc Desk AHU (1st)",
"description": "Desk AHU 066 (2nd)",
"objectName": "POL904_015413",
"instance": "3101066",
"occupancy": "Occupied",
"schedule": "None",
"ignore": "False",
"commStatus": "None",
"alarmStatus": "Active",
"macaddress": "66",
"directSchedule": "False",
"rogueZone": "False",
"deviceType": ["AirSource"]
}]
}
You are declaring the controller inside the ng-repeat (which is incorrect, you need only one controller instance for handling the entire collection, and not one (controller) instance for every element in the collection), so change
this <div ng-app="mainApp">
to <div ng-app="mainApp" ng-controller="mainScripts">
and this <div class="tile draggable" ng-controller="mainScripts" ng-repeat="stuff in titles">
to this <div class="tile draggable" ng-repeat="stuff in titles">
Also, you misplaced the error callback...see below working sample (mocked data)
var mainApp = angular.module('mainApp', []);
mainApp.controller('mainScripts', function($scope, $http) {
var data = {
"deviceTypes": [{
"description": "Air Handler",
"typeName": "AirSource"
}, {
"description": "VAV Terminal",
"typeName": "AirTerminal"
}, {
"description": "Fan Coil",
"typeName": "ChilledWaterTerminal"
}, {
"description": "Chiller",
"typeName": "ChilledWaterSource"
}, {
"description": "Generic Unit",
"typeName": "NoResources"
}, {
"description": "Other Source",
"typeName": "OtherSource"
}, {
"description": "Other Terminal",
"typeName": "OtherTerminal"
}, {
"description": "Water Manager",
"typeName": "WaterSource"
}, {
"description": "WSHP Terminal",
"typeName": "WaterTerminal"
}],
"deviceList": [{
"href": "../MISRest/devices/3101117",
"location": "Loc Desk 3 VAV",
"description": "VAV 117",
"objectName": "VAV 117",
"instance": "3101117",
"occupancy": "Occupied",
"schedule": "Standard Schedule",
"ignore": "False",
"commStatus": "None",
"alarmStatus": "None",
"macaddress": "117",
"directSchedule": "True",
"rogueZone": "False",
"parentID": {
"air": "0"
},
"deviceType": ["AirTerminal"]
}, {
"href": "../MISRest/devices/3101121",
"location": "Loc Desk 4 with temp VAV",
"description": "VAV 121",
"objectName": "VAV Actuator Series 2 Bacnet ASC Controller",
"instance": "3101121",
"occupancy": "Error",
"schedule": "Standard Schedule",
"ignore": "False",
"commStatus": "Fault",
"alarmStatus": "Active",
"macaddress": "121",
"directSchedule": "True",
"rogueZone": "False",
"parentID": {
"air": "0"
},
"deviceType": ["AirTerminal"]
}, {
"href": "../MISRest/devices/3101004",
"location": "New Paris",
"description": "KMC Device",
"objectName": "BAC-8205_001635",
"instance": "3101004",
"occupancy": "Error",
"schedule": "Standard Schedule",
"ignore": "False",
"commStatus": "None",
"alarmStatus": "None",
"macaddress": "4",
"directSchedule": "True",
"rogueZone": "False",
"deviceType": ["NoResources"]
}, {
"href": "../MISRest/devices/3101013",
"location": "Default Location",
"description": "VAV-013",
"objectName": "DEFAULT",
"instance": "3101013",
"occupancy": "Occupied",
"schedule": "None",
"ignore": "False",
"commStatus": "None",
"alarmStatus": "None",
"macaddress": "13",
"directSchedule": "True",
"rogueZone": "False",
"parentID": {
"air": "0"
},
"deviceType": ["AirTerminal"]
}, {
"href": "../MISRest/devices/3101066",
"location": "Loc Desk AHU (1st)",
"description": "Desk AHU 066 (2nd)",
"objectName": "POL904_015413",
"instance": "3101066",
"occupancy": "Occupied",
"schedule": "None",
"ignore": "False",
"commStatus": "None",
"alarmStatus": "Active",
"macaddress": "66",
"directSchedule": "False",
"rogueZone": "False",
"deviceType": ["AirSource"]
}]
};
$scope.titles = data.deviceList; // mocked, replace with real $http.get
/*
// this is the correct way
$http.get('devices.json').then(function successCallback(response) {
console.log("API call works!");
console.dir(response.data.deviceList);
$scope.titles = response.data.deviceList;
}, function errorCallback(response) { // error callback goes here
console.log("API call doesn't work");
});
*/
}); // <-- removed error call back from here
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="mainApp" ng-controller="mainScripts"> <!-- moved controller here! -->
<div class="tile draggable" ng-repeat="stuff in titles"> <!-- removed controller from here -->
<ul>
<li>{{stuff.location}}</li>
<li>{{stuff.description}}</li>
</ul>
</div>
</div>
I'm new in Angularjs,i succeed to display every value from the key "type" from my json array ,but i want only to display "type" if the value is equal to "service" how can i do that.
I tried to do with custom filter but i don t understand how it works.
Thanks for advance for your help !
Here is my controller :
.controller('CategorieCtrl', function ($scope, $stateParams,$state,factocategories) {
var mytype = {};
$scope.displaysales = function(){
mytype = "sales";
console.log(mytype);
}
$scope.displaybuys = function(){
mytype = "buys";
console.log(mytype);
}
$scope.displaycharges = function(){
mytype = "charges";
console.log(mytype);
}
$scope.displayperso = function(){
mytype = "personals";
console.log(mytype);
}
console.log(mytype);
var mytoken = sessionStorage.getItem('token');
factocategories.send(mytoken).then(function(conf){
console.log(conf);
$scope.datab = conf.data;
})
})
here is my view :
<ion-view view-title="Catégories">
<ion-content>
<div class="item item-divider submenu">
<p class="submenu col-25" ng-click="displaysales()">Ventes</p><p class="submenu col-25" ng-click="displaybuys()">achats</p><p class="submenu col-25" ng-click="displaycharges()">Charges</p><p class="submenu col-25" ng-click="displayperso()">Perso</p>
</div>
<ul class="list" ng-repeat="item in datab track by $index">
<li class="item" ng-model="item.type">{{item.type}}</li>
</ul>
</ion-content>
</ion-view>
and here is my json :
{
"die": false,
"erreur": "",
"data": [
{
"id": 913,
"name": "Justine / Ma first facture",
"type": "services",
"invoice": true
},
{
"id": 914,
"name": "Justine / Facture 2",
"type": "services",
"invoice": true
},
{
"id": 917,
"name": "Abus / Fact",
"type": "services",
"invoice": true
},
{
"id": 930,
"name": "Abus / F - 00004",
"type": "",
"invoice": true
},
{
"id": 931,
"name": "Test client / F - 00005",
"type": "",
"invoice": true
},
{
"id": 8868,
"name": "Achat de marchandises",
"type": "buys",
"invoice": false
},
{
"id": 10223,
"name": "adidas",
"type": "sales",
"invoice": false
},
{
"id": 8870,
"name": "Apport personnel",
"type": "personals",
"invoice": false
},
{
"id": 8867,
"name": "Carburant, entretien du véhicule",
"type": "charges",
"invoice": false
},
{
"id": 8865,
"name": "Cotisation sociale",
"type": "charges",
"invoice": false
},
{
"id": 8863,
"name": "Déplacement",
"type": "charges",
"invoice": false
},
{
"id": 8872,
"name": "Emprunt",
"type": "charges",
"invoice": false
},
{
"id": 8861,
"name": "Energie",
"type": "charges",
"invoice": false
},
{
"id": 8871,
"name": "Fourniture de bureau",
"type": "charges",
"invoice": false
},
{
"id": 8864,
"name": "Frais banque, assurance, juridique",
"type": "charges",
"invoice": false
},
{
"id": 8877,
"name": "Logiciel, service web",
"type": "charges",
"invoice": false
},
{
"id": 8866,
"name": "Loyer",
"type": "charges",
"invoice": false
},
{
"id": 8874,
"name": "Mobilier",
"type": "charges",
"invoice": false
},
{
"id": 8869,
"name": "Mon salaire, prélèvement personnel",
"type": "personals",
"invoice": false
},
{
"id": 10779,
"name": "New law",
"type": "charges",
"invoice": false
},
{
"id": 10222,
"name": "nike",
"type": "services",
"invoice": false
},
{
"id": 10778,
"name": "Pakpak",
"type": "sales",
"invoice": false
},
{
"id": 8862,
"name": "Restauration",
"type": "charges",
"invoice": false
},
{
"id": 8875,
"name": "Site web",
"type": "charges",
"invoice": false
},
{
"id": 8873,
"name": "Sous-traitance",
"type": "charges",
"invoice": false
},
{
"id": 8860,
"name": "Télécom",
"type": "charges",
"invoice": false
},
{
"id": 8876,
"name": "Véhicule",
"type": "charges",
"invoice": false
}
]
}
<ul class="list" ng-repeat="item in datab track by $index">
<li class="item" ng-if="item.type === 'services'">{{item.type}}</li>
</ul>
ng-model is not required! is is meant for inputs and two data binding.
for the condinonallity use ng-if
Or you can use a filter like this :
<ul class="list" ng-repeat="item in datab track by $index | filter:{type:'services'}">
<li class="item">{{item.type}}</li>
</ul>
use ng-if="item.type=='service'"
<ul class="list" ng-repeat="item in datab track by $index" ng-if="item.type=='service'">
<li class="item" ng-model="item.type">{{item.type}}</li>
</ul>