extjs 4 tree select a specific node by two internal id - extjs

We can search a record in ExtJs 4 TreeStore by internal id using following code
var record = tree.getRootNode().findChild('id_name','XYZ',true);
Lets say I want to search a record where id_name should be XYZ and age 10 10.
Do we have a method which allows user to search on more than one internal Id's?

Well, the id, whether it is called "id" or "id_name", must be unique for each node in the tree so there is always only one node returned by findChild and no other search condition is needed.

Use findChildBy here:
var record = tree.getRootNode().findChildBy(
function(node) {
return node.get('id_name') == this.requiredIdName && node.get('age') == this.requiredAge;
},
{
// Putting the search criteria stuff in the context used for the function
requiredAge: 10,
requiredIdName: 'abc'
},
// true to do a deep search, false to search only on first level
true
);

Related

how to set a value for one property for every record in an array

I have an array with 1 or more records. for every record for ex: there is a property id. I want to set this name to a default value : 8. Please help me.
Array: [{id: 2, name: "x"},{id: 4, name : "y"}]
I tried this way
if (!Ext.isEmpty(detailLineItems) && (detailLineItems.length > 0)) {
canProcess = false;
Ext.Array.forEach(array, function (r) {
if (r.id == 0 || Ext.isEmpty(r.id)) {
canProcess = true;
//how do I set this id
r.id == 6;
}
});
You should not change the internal id of records, as this may break everything; the value in record.id is expected by the framework to be the same value as record.get(record.idProperty). Instead, you should always use the setter method: record.set("id", 8).
But what you are essentially searching for is the defaultValue configuration on your model field:
fields:[{
name: 'id',
type: 'int',
defaultValue: 8
}]
Please be advised that the field id gets a special handling in ExtJS, as long as the model's idProperty defaults to id. A store can contain only one record with the same non-null value in the field defined by idProperty, so if you add them all to the same store you end up with only one of the records, and all other ones are deleted. So if you need multiple records with the same id in the store, you have to change the idProperty on the model to something else, e.g.
idProperty: 'someNonExistentProperty'
This then may cause issues with store sync operations. If you only try to set the id to a fixed value because your backend requires integer id, and the default id generated for new records by ExtJS is non-numeric (e.g. ext-myModel-1), you can check out identifier: 'negative'.
Your question is already answered by Alexander but just to mention, your code statement r.id == 6; is using the equality operator rather than the assignment operator r.id = 6; :-)

How to remove output limit in FollowRecursive?

I'm using a FollowRecursive query to traverse a graph where every node is connected with the predicate "next". The problem is that I can never get more than 99 source => target mappings.
Why is the output limited to only 100 {source: N, Target: M} objects?
The query looks like follows (all variables are of course defined):
var chain_pred = "next";
var c1 = g.M().Out(chain_pred);
var start_node = "begin";
g.V(start_node).FollowRecursive(c1).ForEach( function(v){
g.V(v.id).Out(chain_pred).ForEach( function(t){
var node = {
source: v.id,
target: t.id
}
g.Emit(node)
})
})
I wrote the same query with java script recursive calls (in DepthFirstSearch), and it turns out that I can't get more than 100 objects. I can get the expected output till depth-3. At depth 4, I start to lose entire tree branches in the start node. This implies that there is definitely a cap on recursion that kills the query after 100 results.
How to remove this limitation?
A bit late, but I answer it anyway:
FollowRecursive(c1, -1)

Move reference between lists in redux store

In my store I have multiple lists of ids which reference to normalized entities.
It looks something like this:
{
list1: [ 1 ],
list2: [],
//other lists
entities: {
1:{data},
...
}
}
Users can edit items and can select in which list the item should be. I can't find an elegant way to move the item from one list to another if the user changed the list while editing.
To move an item I have to remove the item-id from the old list and add it to the new one.
How should I remove the Id from the old list after having written it into the new one? Looking into every list for the Id and if found removing it seems a bit wrong to me.
EDIT:
To explain my use case further:
There can be n lists, which are dates, for example "08-17-2016" and the items in it are events. A user can change the date of an event and so the event needs to move from one date to the other.
I would simply use Array.prototype.filter and check with Array.prototype.indexOf in which list this Id is present:
function moveOrAdd(idToMove, list) {
if (list.indexOf(idToMove) > -1) {
// idToMove was in list so remove
return list.filter(id => id !== idToMove);
} else {
// idToMove wasn't in list so add it
return [...list, idToMove];
}
}
// your store then, assuming an array of id to move or delete
[1, 2, 3 ... 2000].forEach(
index => {
{
list1: [...moveOrAdd(index, list1)],
list2: [...moveOrAdd(index, list2)],
/// etc...
}
}
);

angular JS select first instance of this item

So my question is: how do I scan the JSON in angular to find the first instance of isPrimary:true and then launch a function with the GUID that is in that item.
I have a webservice whos JSON defines available Accounts with a display name and a GUID this generates a dropdown select list that calls a function with the GUID included to return full data from a web service.
In the scenario where theres only 1 OPTION I dont show the SELECT and simply call the function with the single GUID to return the data from the service. If theres no options I dont show anything other than a message.
Code below shows what I currently have.
The Spec has now changed and the data they are sending me in the first service call which defines that select list is now including a property isPrimary:true on one of the JSON object along with its GUID as per the rest
I now need to change my interface to no longer use the SELECT list and instead fire the function call to the service for the item that contains the isPrimary:true property. However there may be multiple instances where isPrimary:true exists in the returning JSON so I just want to fire the function on the first found instance of isPrimary:true
Equally if that property isnt in any of the JSON items then just fire the function on the first item in the JSON.
My current Code is below - you can see the call to retrieve the full details is from function:
vm.retrieveAccount(GUID);
Where the GUID is supplied with each JSON object
Code is:
if (data.Accounts.length > 1) {
vm.hideAcc = false;
setBusyState(false);
//wait for the user to make a selection
} else if (data.Accounts.length == 1){
vm.hideAcc = true;
// Only 1 acc - no need for drop down get first item
vm.accSelected = data.Accounts[0].UniqueIdentifier;
vm.retrieveAccount(vm.accSelected);
} else {
// Theres no accounts
// Hide Drop down and show message
setBusyState(false);
vm.hideAcc = true;
setMessageState(false, true, "There are no Accounts")
}
Sample of new JSON structure
accName: "My Acc",
isPrimary: true,
GUID: "bg111010101"
Still think that's a weird spec, but simple enough to solve. Just step through the array and return the first isPrimary match. If none are found, return the first element of the array.
var findPrimary = function(data) {
if (!(Array.isArray(data)) || data.length == 0) {
return false; // not an array, or empty array
}
for (var i=0; i<data.length; i++) {
if (data[i].isPrimary) {
return data[i]; // first isPrimary match
}
}
// nothing had isPrimary, so return the first one:
return data[0];
}

Lua - How to check if list contains element

I need some help with my lua script for a game. I need to check if my inventory in the game contains any id from a list.
Here's a piece of my list:
local Game_Items = {
{id = 7436, name = "angelic axe", value = 5000},
{id = 3567, name = "blue robe", value = 10000},
{id = 3418, name = "bonelord shield", value = 1200},
{id = 3079, name = "boots of haste", value = 30000},
{id = 7412, name = "butcher's axe", value = 18000},
{id = 3381, name = "crown armor", value = 12000}
}
The following code might look a bit weird since you don't know what it's for, but it's basically this: the list above is a list of items in my game, and inside the game theres an inventory where you can keep items and stuff. Now I want to check if my inventory contains any of those IDs.
I tried adding 2 of the id's manually and it worked, but my list of items contains over 500 items in total and I don't want to write them all out. Is there a way to put the whole list and check if it's in there somehow?
if not table.contains({ 3035, 3043, Game_Items[id] }, tempItemCounter.id) then
This is what I tried so far. Those two first id's work 3035 and 3043, then I tried all my whole list and only check the Ids. but I dont know how to do that. That code does not work. Could anyone just help me include the whole list of id's in the table.contains ?
Basically wanna include my whole list in that line, without typing out all IDs manually.
Shouldn't Game_Items[id] work? Doesn't that mean all the "id" inside "Game_Items"?
Thanks!
No it doesn't mean that. If foo is a table, then foo[id] looks for a field in foo that is called whatever id refers to, such as a string (so if id is 1 you will get foo[1], if id is "bar" you will get foo.bar, etc).
You can't do it in one line, but you can create a function that will allow you to write your if condition. I'm not sure what tempItemCounter is but assuming that your inventory is a map of keys to entries of the form
inventory = {
[1234] = {....},
[1235] = {....},
...
}
where each integer key is unique, and assuming you want true only if all items are in inventory, then you could do this:
function isAllInInventory(items, inventory)
for i,item in ipairs(items) do
if inventory[item.id] == nil
return false
end
end
return true
end
if isAllInInventory(Game_Items, inventory) then
...
end

Resources