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

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; :-)

Related

How to construct Django enum filter

The following is to get 1 record out of database by matching enum type.
TYPES = (
('ABC_ABC', 'abc abc'),
('XYZ_XYZ', 'xyz xyz'),
)
class Hello(models.Model):
type = models.CharField(max_length=8, choices=TYPES, blank=True)
database:
'1', 'ABC_ABC', 'other data'
Queryset:
qset = Q(type__in=('ABC_ABC'))
hello = models.Hello.objects.filter(qset)
Output:
print('count: {}'.format(hello.count()))
Result is 0. It should be 1. What's wrong?
Try changing your Q filter as follow:
qset = Q(type='ABC_ABC')
Explanations:
When using in lookup, django expects an iterable. As you provided only one value, I suspect, it tried to iterate over the string 'ABC_ABC' which is why you didn't get any hit.
If you really want to use the in lookup, which is useless in this case, you should add a comma in to force creating a 1-tuple.
qset = Q(type__in=('ABC_ABC',))
Further thoughts
As this query is quite basic, using a Q object is superfluous. You can simply call
.filter(type='ABC_ABC')

how to compare the query value from url to id using react?

i want to check if the url contains ?query_param if so then get its value and compare that value to an id.
consider the url /path/20?query_parm=2234
and i have to get the param_id and compare it with the item id.
so i do something like below,
handle_location = (path) => {
let opened_item, param_id;
param_id = new
URLSearchParams(this.props.location.search).get('query_param');
if (this.state.items) {
opened_item = this.state.items.find(item => item.id ===
param_id);
}
};
the data structure for items is below,
items = [{
id: 2244;
attributes: something;
}
{
id: 33;
attributes: nothing;
}]
But this gives the opened_item value undefined since item.id is never equal to param_id... because of type being different.
How can i fix this...or is there a better way to find the query_param from url and get its value and use it accordingly to find the item that matches with the query_param value.
Given you understand that both data types are different, you could use avoid using strict equality and leverage type coercion which would work
item.id == param_id
The most efficient way though would be to convert param_id to the appropriate type before comparing e.g.
param_id = parseInt(param_id, 10);
It means one conversion and you can keep the strict equality
You will need to either cast both of the values to the same type(either Number or String) and then perform the comparison or you could use == operator which will try to coerce the types automatically(not recommended). You can also always fall back to some default value if none of the items matched the id.
if (this.state.items) {
opened_item = this.state.items.find(item => item.id ===
param_id) || 'some default value'
}
try this:
const param_id = this.props.match.params.id

Dapper Results(Dapper Row) with Bracket Notation

According to the Dapper documentation, you can get a dynamic list back from dapper using below code :
var rows = connection.Query("select 1 A, 2 B union all select 3, 4");
((int)rows[0].A)
.IsEqualTo(1);
((int)rows[0].B)
.IsEqualTo(2);
((int)rows[1].A)
.IsEqualTo(3);
((int)rows[1].B)
.IsEqualTo(4);
What is however the use of dynamic if you have to know the field names and datatypes of the fields.
If I have :
var result = Db.Query("Select * from Data.Tables");
I want to be able to do the following :
Get a list of the field names and data types returned.
Iterate over it using the field names and get back data in the following ways :
result.Fields
["Id", "Description"]
result[0].values
[1, "This is the description"]
This would allow me to get
result[0].["Id"].Value
which will give results 1 and be of type e.g. Int 32
result[0].["Id"].Type --- what datattype is the value returned
result[0].["Description"]
which will give results "This is the description" and will be of type string.
I see there is a results[0].table which has a dapperrow object with an array of the fieldnames and there is also a result.values which is an object[2] with the values in it, but it can not be accessed. If I add a watch to the drilled down column name, I can get the id. The automatically created watch is :
(new System.Collections.Generic.Mscorlib_CollectionDebugView<Dapper.SqlMapper.DapperRow>(result as System.Collections.Generic.List<Dapper.SqlMapper.DapperRow>)).Items[0].table.FieldNames[0] "Id" string
So I should be able to get result[0].Items[0].table.FieldNames[0] and get "Id" back.
You can cast each row to an IDictionary<string, object>, which should provide access to the names and the values. We don't explicitly track the types currently - we simply don't have a need to. If that isn't enough, consider using the dapper method that returns an IDataReader - this will provide access to the raw data, while still allowing convenient call / parameterization syntax.
For example:
var rows = ...
foreach(IDictionary<string, object> row in rows) {
Console.WriteLine("row:");
foreach(var pair in row) {
Console.WriteLine(" {0} = {1}", pair.Key, pair.Value);
}
}

extjs 4 tree select a specific node by two internal id

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
);

EXTJS Store issue with Null Values -- useNull: doesn't have an affect --Help?

Folks,
I have a combobox component backed by a JSONStore. The data loaded into the store is returning null for the combobox's value. The value is an int. The JSON decode process is converting the null value into a zero; causing the combobox to fail to render when it attempts to find the pk, zero that doesn't exist in its backing store.
I've found the useNull: config option for data.Field objects, upgraded to 3.3.0 Final and set my int value for the combobox to useNull:true. This isn't having any affect at all, unfortunately. The decoded value is still being changed from null to zero.
Any ideas about how to not set the field to a zero when the data for a JSON field is null?
Here's a pic of what's going on. Notice the data: value is zero, but the JSON value is null.
Thanks!
(gah! stoopid reputation < 10 so I can't directly post the pic. View it here: debug pic )
Also, here's my store's field config:
fields: [
{name:"id", type:"int"},
{name:"occurenceDate", dateFormat: 'Y-m-d\\TH:i:s', type:"date"},
{name:"docketNumber", type:"string"},
{name:"courtLocationId", type:"int", useNull:true},
{name:"assignedOfficerId", type:"int", useNull:true},
{name:"primaryIncidentTypeId", type:"int", useNull:true},
{name:"secondaryIncidentTypeId", type:"int", useNull:true},
{name:"tertiaryIncidentTypeId", type:"int", useNull:true},
{name:"incidentLocation", type:"string"},
{name:"summary", type:"string"},
{name:"personalItemsSeized", type:"string"},
"supplements",
"parties",
"judgeIds"
]
Try using it without type declaration. You may also use convert method:
{
name: "primaryIncidentTypeId",
convert: function(value, row) {
return (value == null) ? null : parseInt(value);
}
}
About the combo width: I usually use
defaults: {
anchor: '100%'
}
in the form declaration and have no problems with widths.
Isnt't it possible to provide convert functions from server side together with all other metadata?
And I'm still using ExtJS 3.2 - no need of any new bugs in the production systems :)
This also got me, you can additionally override the type convert function in Ext.data.Types to allow null values for integer type fields.
Ext.data.Types.INT.convert = function(v){
v = parseInt(String(v).replace(Ext.data.Types.stripRe, ''), 10);
return isNaN(v) ? null : v;
};
You must use defaultValue: null ,useNull : true because default value for integet type is zero
Example:
{name:"primaryIncidentTypeId", type:"int", useNull:true , defaultValue: null },

Resources