How to add items dynamically to a polymer paper-tree? - polymer

I am using polymer paper-tree element in my code.Its displays the tree perfectly with static data,however when I try to add an item dynamically it doesn't work. I am attaching a screenshot here.enter image description here
//Array
this.itemArray = {"name": "Project",
"icon": "theaters",
"open": true,
"children": [{
"name": "Quotes",
"icon": "theaters",
"open": true,
"children": [{
"name": "Breaking Bad",
"icon": "theaters"
}]
}, {
"name": "Templates",
"icon": "theaters",
"open": true,
"children": [{
"name": "Breaking Bad",
"icon": "theaters"
}, {
"name": "Game of Thrones",
"icon": "theaters"
}]
}]
}
//Js code which accepts value from a text box and adds it to the array.
this.$.additem.addEventListener("click", function(){
enter code herevar data = self.$.txtData.value;
self.itemArray.children[1].children.push(
{'name':data,
'icon':'theaters'});
self.addData(self.itemArray,data);
self.$.txtData.value = " ";
});
// addData function
addData:function(itemsarr){
console.log("calling items array function",this.tree);
var tempArr = [];
tempArr = itemsarr;
if(this.tree){
this.$.temp.removeChild(this.tree);
//tempArr = itemsarr; //= this.itemArray2;
}
this.tree = document.createElement('paper-tree-node');
this.tree.setAttribute('id','treemenu');
this.$.temp.appendChild(this.tree);
this.tree.data = tempArr;
console.log(tempArr);
}

Polymer paper-tree have a data property. You just need to update it.
Help link: https://github.com/vpusher/paper-tree/blob/master/paper-tree.html

Related

How to bind local JSON Model to MultiComboBox in SAPUI5?

how can I bind my local JSON model to my MultiComboBox.
The XML code for the combobox is looking as followed:
<MultiComboBox id="multiBox" selectionFinish="onBoxFinish"/>
The model looks like this:
var exampleData = {
"data": [{
"name": "Example1",
"value": "16.505406"
},
{
"name": "Example2",
"value": "6.65465"
},
{
"name": "Example3",
"value": "89.56456"
}]
};
I want to display the 3 names in the ComboBox.
Can someone help me with this?
Thanks.
Firstly, instantiate a JSONModel with your data. Secondly, set the JSONModel to your view. Thirdly, bind the model to your MultiComboBox.
var oData = {
"data": [
{
"name": "Example1",
"value": "16.505406"
},
{
"name": "Example2",
"value": "6.65465"
},
{
"name": "Example3",
"value": "89.56456"
}
]
};
var oModel = new JSONModel(oData);
this.getView().setModel(oModel);
<MultiComboBox
selectionFinish="onBoxFinish"
items="{/data}">
<core:Item
key="{name}"
text="{value}">
</core:Item>
</MultiComboBox>

How to set valueAxes title as variable from JSON in amCharts?

I would like to change the valueAxes title from a hardcoded string to a value from a JSON property via dataprovider.
thanks
You can use the init event to set your valueAxes title then call validateNow(true) (or validateData()). Here's a contrived example:
var chart = AmCharts.makeChart("chartdiv", {
"type": "serial",
"theme": "light",
"dataProvider": [{
"valueAxisTitle": "Number of visits", //can be whatever property you want
"country": "USA",
"visits": 2025
}, // ...
]
// ...
"listeners": [{
"event": "init",
"method": function(e) {
e.chart.valueAxes[0].title = e.chart.dataProvider[0].valueAxisTitle;
e.chart.validateNow(true);
}
}]
});
Demo

Select2 json data not working

I am trying to hook select2 when an element has class "select2picker" i am also customising if the source of the dropdown list is an array. My code below
$('.select2picker').each(function() {
var settings = {};
if ($(this).attr('data-json')) {
var jsonValue = JSON.parse($(this).attr('data-json')).val());
settings = {
placeholder: $(this).attr('data-placeholder'),
minimumInputLength: $(this).attr('data-minimumInputLength'),
allowClear: true,
data: jsonValue
}
}
$(this).select2(settings);
});
but the result is horrible it fails to hook all the select2 dropdownlist
but when I comment out the data property, the output shows perfect (but the data binding goes missing)
My array looks like the following
[ { "id": "2015-0152", "text": "2015-0152" }, { "id": "2015-0153", "text": "2015-0153" }, { "id": "2016-0001", "text": "2016-0001" }, { "id": "2016-0002", "text": "2016-0002" }, { "id": "2016-0003", "text": "2016-0003" }, { "id": "2016-0004", "text": "2016-0004" }, { "id": "2016-0005", "text": "2016-0005" }, { "id": "2016-0006", "text": "2016-0006" }, { "id": "2016-0007", "text": "2016-0007" }, { ... }, { "id": "2015-0100", "text": "2015-0100" }, { "id": "2015-0101", "text": "2015-0101" }, { "id": "2015-0080", "text": "2015-0080" }, { "id": "2015-0081", "text": "2015-0081" }, { "id": "2015-0090", "text": "2015-0090" }, { "id": "2015-0102", "text": "2015-0102" }, { "id": "2015-0112", "text": "2015-0112" }, { "id": "2015-0128", "text": "2015-0128" }, { "id": "2015-0136", "text": "2015-0136" } ]
I am really confused about what is going wrong. Any idea?
Select2 version: 3.4.8
This line gives an error: var jsonValue = JSON.parse($(this).attr('data-json')).val());
Should be: var jsonValue = JSON.parse($(this).attr('data-json'));.
Also this line in your question:
i am also customising if the source of the dropdown list is an array
Indicates to me that it might also not be an array. In that cause you should check if it is an array before you pass the data to select2.
EDITED: Another thing that came to my mind was the following.
If you're using data properties for the placeholder I don't think you need to pass the values of those properties to select2 a second time like you do here
placeholder: $(this).attr('data-placeholder'),
minimumInputLength: $(this).attr('data-minimumInputLength'),
Might be that you need to pick one of the two (either pass it along in your settings, or use an attribute). As select2 looks at the data attributes to get a value.
I checked if the above was correct turns out it isn't. It works fine in this fiddle: https://jsfiddle.net/wL7oxbpv/
I think there is something wrong with your array data. Please check that.

Search JSON for multiple values, not using a library

I'd like to be able to search the following JSON object for objects containing the key 'location' then get in return an array or json object with the 'name' of the person plus the value of location for that person.
Sample return:
var matchesFound = [{Tom Brady, New York}, {Donald Steven,Los Angeles}];
var fbData0 = {
"data": [
{
"id": "X999_Y999",
"location": "New York",
"from": {
"name": "Tom Brady", "id": "X12"
},
"message": "Looking forward to 2010!",
"actions": [
{
"name": "Comment",
"link": "http://www.facebook.com/X999/posts/Y999"
},
{
"name": "Like",
"link": "http://www.facebook.com/X999/posts/Y999"
}
],
"type": "status",
"created_time": "2010-08-02T21:27:44+0000",
"updated_time": "2010-08-02T21:27:44+0000"
},
{
"id": "X998_Y998",
"location": "Los Angeles",
"from": {
"name": "Donald Steven", "id": "X18"
},
"message": "Where's my contract?",
"actions": [
{
"name": "Comment",
"link": "http://www.facebook.com/X998/posts/Y998"
},
{
"name": "Like",
"link": "http://www.facebook.com/X998/posts/Y998"
}
],
"type": "status",
"created_time": "2010-08-02T21:27:44+0000",
"updated_time": "2010-08-02T21:27:44+0000"
}
]
};
#vsiege - you can use this javascript lib (http://www.defiantjs.com/) to search your JSON structure.
var fbData0 = {
...
},
res = JSON.search( fbData0, '//*[./location and ./from/name]' ),
str = '';
for (var i=0; i<res.length; i++) {
str += res[i].location +': '+ res[i].from.name +'<br/>';
}
document.getElementById('output').innerHTML = str;
Here is a working fiddle;
http://jsfiddle.net/hbi99/XhRLP/
DefiantJS extends the global object JSON with the method "search" and makes it possible to query JSON with XPath expressions (XPath is standardised query language). The method returns an array with the matches (empty array if none were found).
You can test XPath expressions by pasting your JSON here:
http://www.defiantjs.com/#xpath_evaluator

JSON child object with dojo/data/ObjectStore

Given the following JSON data:
[
{
"pk": 2,
"model": "corkboard.announcement",
"fields": {
"body": "Test announcement 2 body.",
"date": "2012-04-10T00:59:12Z",
"title": "Test Announcement 2"
}
},
{
"pk": 1,
"model": "corkboard.announcement",
"fields": {
"body": "Test Announcement 1 body.",
"date": "2012-04-10T00:58:56Z",
"title": "Test Announcement 1"
}
}
]
I'm creating a dojox/DataGrid but I can't seem to find a way to access "fields" children.
Here is the javascript:
<script>
var announcementStore, dataStore, grid;
require(["dojo/store/JsonRest", "dojo/store/Memory", "dojo/store/Cache", "dojox/grid/DataGrid", "dojo/data/ObjectStore", "dojo/query", "dojo/domReady!"],
function(JsonRest, Memory, Cache, DataGrid, ObjectStore, query){
announcementStore = Cache(JsonRest({target:"/corkboard/announcements/"}), Memory());
grid = new DataGrid({
store: dataStore = ObjectStore({objectStore: announcementStore}),
structure: [
{name:"Title", field:"title", width: "200px"},
{name:"Body", field:"body", width: "200px", editable: true}
]
}, "target-node-id");
grid.startup();
query("#save").onclick(function(){
dataStore.save();
});
});
</script>
I tried using fields.title and fields.body when defining the field, but that didn't work.
In this example, how would I access "fields" children?
You need to use the formatter method in the structure of the grid like below.
{name:"Title",field:"_item",widht:"200px",formatter:function(item){return item.fields.title}}
Remember, you need to passs _item in the field, which will give you entire row in the formatter method and using dot notation, you can return the reuqired data