I faced with this challenge to assign array to another array such that the arrays won't be reactive on changes from one of the array.
So to achieve this I made a local copy of the same array and assigned one to another but I would love to do stuffs dynamically where I am having the reactive issues.
Below is what am trying to achieve
const ComponentA = {
data: function() {
return {
categories:[
{
name: 'Category 1',
series: [
{
name: 'Series 1',
value: 5
},
{
name: 'Series 2',
value: 5
}
]
},
{
name: 'Category 2',
series: [
{
name: 'Series 1',
value: 50
},
{
name: 'Series 2',
value: 56
}
]
}
],
tempCategories:[
{
name: 'Category 1',
series: [
{
name: 'Series 1',
value: 5
},
{
name: 'Series 2',
value: 5
}
]
},
{
name: 'Category 2',
series: [
{
name: 'Series 1',
value: 50
},
{
name: 'Series 2',
value: 56
}
]
}
]
}
},
methods: {
resetCategory() {
this.tempCategories.forEach((category,i)=>{
category.series.forEach((series,j)=>{
this.categories[i].series[j] = series;
});
});
}
}
}
after computations on categories i can reset the categories array with tempCategories by calling
this.resetCategory() and it works as expected.
Meanwhile I want to be able to do this assignment dynamically whereby when i have copy of the category array I can assign the array to both categories and tempCategories arrays, respectively and after computations with categories array i can reset the categories array with tempCategories array. So I did the following.
initialize(category) {
this.tempCategories = category;
this.categories = category;
}
I realize changes in this.categories reflects on this.tempCategories; I've even used the following options
initialize(category) {
this.tempCategories = category.slice();
//or this.tempCategories = Json.parse(Json.stringify(category));
this.categories = category.slice();
//or this.categories = Json.parse(Json.stringify(category));
}
yet the two arrays are still reactive. Please I need help on how to solve this problem.
Thanks guys, it works now both method
initialize(categories){
//let copy = _.cloneDeep(categories);
let copy = JSON.parse(JSON.stringify(categories));
this.tempCategories = copy;
this.categories = copy;
},
resetCategories(){
//this.categories = _.cloneDeep(this.tempCategories);
this.categories = JSON.parse(JSON.stringify(this.tempCategories));
},
There's no such Json. but It may be JSON.
initialize(category) {
this.tempCategories = JSON.parse(JSON.stringify(category));
this.categories = JSON.parse(JSON.stringify(category));
}
And you can't prevent fake copy of by
this.tempCategories = category;
this.categories = category;
May be those three Arrays are exactly same even one of them is updated.
I have object like this:
data() {
return {
headings: ['id','remark'],
rows: [
{
id: 1,
FirstName: 'Jhon',
LastName: 'Doe',
remark: 0
},
{
id: 2,
FirstName: 'Foo',
LastName: 'Bar',
remark: 1
}
]
}}
Is there away how do i check if key headings object exist in rows object.
Thanks and sorry for my bad english.
Yes, There's a way
function (row) {
return this.headings.reduce((res, heading) => res &= row.hasOwnProperty(heading), true)
}
This method should return true - if the row has the heading.
I am having a list which has table in a itemtpl
{
xtype: 'list',
deferEmptyText: false,
height:140,
store:'Store',
scrollable:false,
itemTpl:'<table><tr><td><b>{A}</b></td></tr><tr><td>{B} </td></tr><tr><td>{C}</td><td>{value}</td></tr></table>'
}
The data in my store is
{ A:'Name1',
B:'Name2',
C:'Name3',
value:0
}
I want the data to be displayed as
Name1 0
Name2 0
Name3 0
How should I arrange the data in my store so that it can be shown in the above mentioned format.Right now the td tag is not functioning as expected.
At first go here and see how itemTpl is used. And regarding your query, you try this snippet:
{
xtype: 'list',
itemTpl: '{title} {value}',
data: [
{
title: 'Name1',
value: 0
},
{
title: 'Name2',
value: 0
},
{
title: 'Name3',
value: 0
},
{
title: 'Name4',
value: 0
}
]
}
By this you will get your desired format of data. Here data is nothing but the store. So in your store data should be like this.
I am learning DOJO 1.6.
I have data
var data = [
{ FirstName: 'xyz', Lastname: 'QSD', rollNo: '1', EntryDate: '2012-09-11T17:35:31.835+02:00' },
{ FirstName: 'abc', Lastname: 'qgr', rollNo: '2', EntryDate: '2012-08-11T17:35:31.835+02:00' }
{ FirstName: 'ert', Lastname: 'fgd', rollNo: '3', EntryDate: '2012-18-11T17:35:31.835+02:00' }
];
I want to sort it with respect to Last name or EntryDate and display in a tree format.
Thanks in Advance.
Multiple root data
data: [
{ id: 'world', name:'The earth', type:'planet', population: '6 billion'},
{ id: 'AF', name:'Africa', type:'continent', population:'900 million', area: '30,221,532 sq km',
timezone: '-1 UTC to +4 UTC', parent: 'world'},
{ id: 'EG', name:'Egypt', type:'country', parent: 'AF' },
{ id: 'KE', name:'Kenya', type:'country', parent: 'AF' },
{ id: 'Nairobi', name:'Nairobi', type:'city', parent: 'KE' },
{ id: 'Mombasa', name:'Mombasa', type:'city', parent: 'KE' },
{ id: 'SD', name:'Sudan', type:'country', parent: 'AF' },
{ id: 'Khartoum', name:'Khartoum', type:'city', parent: 'SD' },
{ id: 'AS', name:'Asia', type:'continent', parent: 'world' },
{ id: 'CN', name:'China', type:'country', parent: 'AS' },
{ id: 'IN', name:'India', type:'country', parent: 'AS' },
{ id: 'RU', name:'Russia', type:'country', parent: 'AS' },
{ id: 'MN', name:'Mongolia', type:'country', parent: 'AS' },
{ id: 'OC', name:'Oceania', type:'continent', population:'21 million', parent: 'world'},
{ id: 'EU', name:'Europe', type:'continent', parent: 'world' },
{ id: 'DE', name:'Germany', type:'country', parent: 'EU' },
{ id: 'FR', name:'France', type:'country', parent: 'EU' },
{ id: 'ES', name:'Spain', type:'country', parent: 'EU' },
{ id: 'IT', name:'Italy', type:'country', parent: 'EU' },
{ id: 'NA', name:'North America', type:'continent', parent: 'world' },
{ id: 'SA', name:'South America', type:'continent', parent: 'world' }
],
Javascript Array has a native function, called sort. This will off-the-shelf sort the values alphabetically. For the purpose of sorting non-string-values, we need to supply a sortingfunction. Like so, in regards to Lastname:
data.sort(function(a,b) {
var _A=a.Lastname.toLowerCase(),
_B=b.Lastname.toLowerCase();
if (_A < _B) //sort string ascending
return -1
if (_A > _B)
return 1
return 0 //default return value (no sorting)
});
If youre sorting against a date, you would need to initialize _A and _B to a Date.
However, if youre aiming to represent the data in a dijit.Tree, there's inbuilt method for sorting the Store, lets wrap data into a dojo/data/ItemFileReadStore and show in a tree. Tree will have a model, using ItemFileWriteStore - so that items can be modified:
var sortableStore = new dojo.data.ItemFileReadStore({
data: {
identifier: 'rollNo',
items: data
},
comperatorMap: {
'EntryDate' : function(a,b) {
var _A = new Date(a), _B = new Date(b);
if(_A > _B) return 1;
else if(_A == _B) return 0;
else return -1;
}
});
Using 'store.fetch()' API while setting the 'sort' parameter, you control the order of returned items. The EntryDate you will have to create a comperatorMap of functions, as with Array.sort() in order to sort it properly. See the documentation.
var model = new dijit.tree.ForestStoreModel({
rootLabel: 'Names',
store: new dojo.data.ItemFileWriteStore({
data: {
identifier: 'rollNo',
items: data,
// blank, initially - can fill in with 'data' to show non-sorted items until sort is called
label: 'FirstName'
}
}) // blank itemsstore
});
var tree = new dijit.Tree({
model: model
});
OK, All set - but problem with .fetch is, it runs with callbacks (onComplete) and is difficult to control in a recursive manner. Instead, the functionality put in THIS FIDDLE duplicates the store data and then sorts it via native array sort - as opposed to using SimpleQueryEngine.
This will prove to give more reliable results - but does mess with DnD controllers and persist flag..
See how store can sort its items returned by fetch here: fiddle. This however only sorts one 'level' at a time and does not perform deep sorts.
IMO: The proper implementation of sort is a serverside sort, directly in the database query.
http://dojotoolkit.org/reference-guide/1.8/dojo/data/ItemFileReadStore.html#custom-sorting
http://dojotoolkit.org/reference-guide/1.8/dijit/Tree.html
In dojo 1.7.2, if I create a data store containing array values, dojox.grid.DataGrid displays them with no problem, separating each item with a coma.
However, in dojo 1.6, it takes only the first element of my array. I have a project where I have to use version 1.6. Is there any workaround for this in that version ?
To illustrate the problem, here are 2 examples :
On dojo 1.6 : http://jsfiddle.net/psoares/HbFNY/
On dojo 1.7 : http://jsfiddle.net/psoares/QLm65/
Thanks !
Apparently the problem comes from ItemFileReadStore rather than from the grid.
I modified my code for 1.6 to use ObjectStore and MemoryStore instead, and it worked.
See http://jsfiddle.net/psoares/HbFNY/16/
this is a flaw and yet it is not.. The construct of your JSON is not quite right as any value is not allowed as array unless it is one of the childAttrs. Due to nature of [1,2,3].toString() that is why your attempts on setting values as arrays are considered valid.
The get/set in an ItemFileReadStore works with its items as such:
store._arrayOfAllItems = {
value1 : { values : [ 'realvalue' ] },
value2 : { values : [ 'realvalue' ] }
};
the getter then says
store.get = function(itemById, val) { return itemById[val][0]; }
// why only the first arrayslot is pulled from store---/^
In your JSON construct, what prohibits you from settings the values as such following?
var data = {
id: 'id',
label: 'id',
items: [
{
id: "value1",
values: "a,b,c" // permit-able string value
},
{
id: "value2",
values: "foo"}
]
};
If you want multiple values by same key of one ID then you must deliver the data as children and handle them as such, like;
data: {
id: 'id',
label: 'id',
childrenAttrs: [ 'items', 'children'], // << default behavior
items: [ {
id: "value1",
children: [
{ id: "value1_1", values: 'a' },
{ id: "value1_2", values: 'b' },
{ id: "value1_3", values: 'c' }
]
}, {
id: "value2",
values: "foo"
} ]
}
However, only the dojox.grid.TreeGrid will allow using multi-lvl datastores