How to set nested record using "set()"? - extjs

I'm facing problem in setting my record value.
I have nested record inside store like this :
- data
act_reading // I can set value of this record using -> record.set('act_reading', 'dsds');
adj_reading
act_reading_nested
- data
arr_act_colour // How can I set this record?
arr_act_rating // How can I set this record?
arr_act_ferrous // How can I set this record?
idrectype1 // How can I set this record?
adj_reading_nested
- data
arr_adj_colour
arr_adj_rating
arr_adj_ferrous
idrectype2
How can I set idrectype1 value inside act_reading_nested?
I also have to do the same thing for arr_act_colour, arr_act_rating, & arr_act_ferrous.
Thanks in Advance

Assuming record is variable holding reference to your recrd, wouldn't that work?
record.get('act_reading_nested').set('arr_act_colour','value');
record.get('act_reading_nested').idrectype1 = 'something';
Yes it would. Just checked.

From my experience with current implementation of the Store you can not :( I'm facing with such kind issue too, when I'd like to edit a non-plain Store in the GridPanel. see http://www.sencha.com/forum/showthread.php?119573-Event-beforeedit-in-EditableGridPanel&highlight=afteredit
I did not check it myself (rather found a hack workaround), but more clean way is to fix it with your own Store implementation using of Ext.override(Ext.Store, { ...}) facility.
See how I did it for 'standard' HttpProxy implementation.
Ext.override (Ext.data.HttpProxy, {
buildUrl : function (action, record) {
var ret = '';
if (window.location.pathname != '/') {
ret = window.location.pathname;
};
return ret + Ext.data.HttpProxy.superclass.buildUrl.call(this, action, record);
}
});
It requires a bit deep Extjs internal knowledge though.

Related

I'm trying to update the row values in MUI datagrid, i'm using processRowUpdate property.But, I'm only getting recent updated cell value only

Here **Skills ** will have the array of objects which includes the values Which I'm updating.
const handleProcessRowUpdate = (newRow, oldRow) => {
console.log("update called", newRow, oldRow);
let skills = assignedskillList;
let result = skills.map((item) => {
if (item.skill_id == params.skill_id) {
console.log("ids", params.skill_id, item.skill_id);
if (item.skill_level !== newRow.skill_level) {
item.skill_level = newRow.skill_level;
}
if ((item.target_level = newRow.target_level)) {
item.target_level = newRow.target_level;
}
console.log("idsss", item.skill_level, item.target_level); // here for first column edit //I'm getting current edited value only, for the second col edit getting only second column //value.the first value is resetting
}
setUpdatedVal(result); //I'm setting this in a new array to use this for post to API
return item;
});
tried onCellEdit commit , but, thats not worked. any other solutions ?
or correct me if I'm setting the value wrongly.
Thanks In advance.....
Finally, I found it.
Hope it might be helpful for someone in future
instead of updating the value inside the map. I've just defined a result array outside and assigned the map to that, Now I can get all updated values..
Thank you...

unexpected result in a query in laravel

I’m a beginner in Laravel but have a problem at first. I wrote this query and I’m waiting for Sonya Bins as result but unexpectedly I see ["Sonya Bins"]. what’s the problem?
Route::get('products', function () {
$articles=DB::table('users')->where('id','2')->get()->pluck('name');
return view('products',compact('articles'));
});
pluck will return array if you want to get only single value then use value
// will return array
$articles=DB::table('users')->where('id','2')->get()->pluck('name');
//will return string
$articles=DB::table('users')->where('id','2')->value('name');
// output Sonya Bins
here is an example from the documentation:
if you don't even need an entire row, you may extract a single value from a record using the value method. This method will return the value of the column directly:
$email = DB::table('users')->where('name', 'John')->value('email');
Read more about it here
Hope it helps.
Thanks
pluck() used to return a String before Laravel 5.1, but now it returns an array.
The alternative for that behavior now is value()
Try this:
Route::get('products', function () {
$articles=DB::table('users')->where('id','2')->get()->value('name');
return view('products',compact('articles'));
});
I think it's easier to use the Model + find function + value function.
Route::get('products', function () {
$articles = User::find(2)->value('name');
return view('products',compact('articles'));
});
pluck will return the collection.
I think id is your primary key.
You can just get the first record, and call its attribute's name:
DB::table('users')->where('id','2')->first()->name;
or
DB::table('users')->find(2)->name;
First thing is that you used invalid name for what you pass to view - you don't pass articles but user name.
Second thing is that you use get method to get results instead of first (or find) - you probably expect there is only single user with id = 2.
So to sum up you should use:
$userName = DB::table('users')->find(2)->name;
return view('products',compact('userName'));
Of course above code is for case when you are 100% sure there is user with id = 2 in database. If it might happen there won't be such user, you should use construction like this:
$userName = optional(DB::table('users')->find(2))->name;
($userName will be null if there is no such record)
or
$userName = optional(DB::table('users')->find(2))->name ?? 'No user';
in case you want to use custom string.

AngularJS Filter expression replacing with variable instead

I have a table object and I want to filter out a particular "index" row using filter function as shown below.
However, $controller.expression is not working out.
IF `$controller.expression = "3";
It would work. But not
$controller.expression = "3,4";
$controller.expression = [3,4];
$scope.dataToBeTransfer = $scope.myDataTable.filter(function (el)
{
return el.index== $controller.expression;
});
So how do I solve this issue?
If you're going to use an array you need to loop through to see if el.index is included in the array. This is probably the easiest way to do it from the given code:
$scope.dataToBeTransfer = $scope.myDataTable.filter(function (el) {
return $controller.expression.includes(el.index);
});

UIGRID: Changing cellFilter in columnDefs is not well updated

I need to dynamically change the decimal precision of a column.
I have tried different approaches and non of them worked.
I did a quick example in Plunker in which there is a button to swap data. They have different data and different columnDefs: one has 3 decimals and the other 4.
You can see there that the first rows maintain the first format.
I'm using notifyDataChange in gridApi but is not working for this kind of change:
$scope.gridApi.core.notifyDataChange( this.uiGridConstants.dataChange.ALL );
This is a problem for me because in my case I want to keep the same data but dynamically change the decimal precision of a column.
Does anybody knows how to refresh/reload that new columnDefs configuration?
I would really appretiate it!
Sadly it seems none of the usual methods to refresh/update the grid will work with this use case.
I managed to make it work though by deleting all rows and then recreating them after a $timeout.
I updated your plunker here.
The main edit is as follows:
$scope.swapData = function() {
$scope.gridOpts.data = []{
$timeout(function() {
if (!$scope.isData2) {
$scope.gridOpts.data = data2;
$scope.gridOpts.columnDefs = columnDefs2;
}
else {
$scope.gridOpts.data = data1;
$scope.gridOpts.columnDefs = columnDefs1;
}
$scope.isData2 = !$scope.isData2;
});
};
Looks like there is an answer on the ui-grid github repo.
From a contributor:
The cellFilter is joined in with the cell template when the cell
template is fetched/used. It doesn't get updated live.
With possible solution:
colDef.cellFilter = 'date:col.filterFormat'
uiGridApi.core.registerColumnsProcessor(function(columns, rows){
angular.forEach(columns, function(col){
var filterFormat = col.colDef.filterFormat;
if ( filterFormat ){ col.filterFormat = filterFormat; }
});
return rows;
}, 40);

In Firebase, is there a way to get the number of children of a node without loading all the node data?

You can get the child count via
firebase_node.once('value', function(snapshot) { alert('Count: ' + snapshot.numChildren()); });
But I believe this fetches the entire sub-tree of that node from the server. For huge lists, that seems RAM and latency intensive. Is there a way of getting the count (and/or a list of child names) without fetching the whole thing?
The code snippet you gave does indeed load the entire set of data and then counts it client-side, which can be very slow for large amounts of data.
Firebase doesn't currently have a way to count children without loading data, but we do plan to add it.
For now, one solution would be to maintain a counter of the number of children and update it every time you add a new child. You could use a transaction to count items, like in this code tracking upvodes:
var upvotesRef = new Firebase('https://docs-examples.firebaseio.com/android/saving-data/fireblog/posts/-JRHTHaIs-jNPLXOQivY/upvotes');
upvotesRef.transaction(function (current_value) {
return (current_value || 0) + 1;
});
For more info, see https://www.firebase.com/docs/transactions.html
UPDATE:
Firebase recently released Cloud Functions. With Cloud Functions, you don't need to create your own Server. You can simply write JavaScript functions and upload it to Firebase. Firebase will be responsible for triggering functions whenever an event occurs.
If you want to count upvotes for example, you should create a structure similar to this one:
{
"posts" : {
"-JRHTHaIs-jNPLXOQivY" : {
"upvotes_count":5,
"upvotes" : {
"userX" : true,
"userY" : true,
"userZ" : true,
...
}
}
}
}
And then write a javascript function to increase the upvotes_count when there is a new write to the upvotes node.
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);
exports.countlikes = functions.database.ref('/posts/$postid/upvotes').onWrite(event => {
return event.data.ref.parent.child('upvotes_count').set(event.data.numChildren());
});
You can read the Documentation to know how to Get Started with Cloud Functions.
Also, another example of counting posts is here:
https://github.com/firebase/functions-samples/blob/master/child-count/functions/index.js
Update January 2018
The firebase docs have changed so instead of event we now have change and context.
The given example throws an error complaining that event.data is undefined. This pattern seems to work better:
exports.countPrescriptions = functions.database.ref(`/prescriptions`).onWrite((change, context) => {
const data = change.after.val();
const count = Object.keys(data).length;
return change.after.ref.child('_count').set(count);
});
```
This is a little late in the game as several others have already answered nicely, but I'll share how I might implement it.
This hinges on the fact that the Firebase REST API offers a shallow=true parameter.
Assume you have a post object and each one can have a number of comments:
{
"posts": {
"$postKey": {
"comments": {
...
}
}
}
}
You obviously don't want to fetch all of the comments, just the number of comments.
Assuming you have the key for a post, you can send a GET request to
https://yourapp.firebaseio.com/posts/[the post key]/comments?shallow=true.
This will return an object of key-value pairs, where each key is the key of a comment and its value is true:
{
"comment1key": true,
"comment2key": true,
...,
"comment9999key": true
}
The size of this response is much smaller than requesting the equivalent data, and now you can calculate the number of keys in the response to find your value (e.g. commentCount = Object.keys(result).length).
This may not completely solve your problem, as you are still calculating the number of keys returned, and you can't necessarily subscribe to the value as it changes, but it does greatly reduce the size of the returned data without requiring any changes to your schema.
Save the count as you go - and use validation to enforce it. I hacked this together - for keeping a count of unique votes and counts which keeps coming up!. But this time I have tested my suggestion! (notwithstanding cut/paste errors!).
The 'trick' here is to use the node priority to as the vote count...
The data is:
vote/$issueBeingVotedOn/user/$uniqueIdOfVoter = thisVotesCount, priority=thisVotesCount
vote/$issueBeingVotedOn/count = 'user/'+$idOfLastVoter, priority=CountofLastVote
,"vote": {
".read" : true
,".write" : true
,"$issue" : {
"user" : {
"$user" : {
".validate" : "!data.exists() &&
newData.val()==data.parent().parent().child('count').getPriority()+1 &&
newData.val()==newData.GetPriority()"
user can only vote once && count must be one higher than current count && data value must be same as priority.
}
}
,"count" : {
".validate" : "data.parent().child(newData.val()).val()==newData.getPriority() &&
newData.getPriority()==data.getPriority()+1 "
}
count (last voter really) - vote must exist and its count equal newcount, && newcount (priority) can only go up by one.
}
}
Test script to add 10 votes by different users (for this example, id's faked, should user auth.uid in production). Count down by (i--) 10 to see validation fail.
<script src='https://cdn.firebase.com/v0/firebase.js'></script>
<script>
window.fb = new Firebase('https:...vote/iss1/');
window.fb.child('count').once('value', function (dss) {
votes = dss.getPriority();
for (var i=1;i<10;i++) vote(dss,i+votes);
} );
function vote(dss,count)
{
var user='user/zz' + count; // replace with auth.id or whatever
window.fb.child(user).setWithPriority(count,count);
window.fb.child('count').setWithPriority(user,count);
}
</script>
The 'risk' here is that a vote is cast, but the count not updated (haking or script failure). This is why the votes have a unique 'priority' - the script should really start by ensuring that there is no vote with priority higher than the current count, if there is it should complete that transaction before doing its own - get your clients to clean up for you :)
The count needs to be initialised with a priority before you start - forge doesn't let you do this, so a stub script is needed (before the validation is active!).
write a cloud function to and update the node count.
// below function to get the given node count.
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);
exports.userscount = functions.database.ref('/users/')
.onWrite(event => {
console.log('users number : ', event.data.numChildren());
return event.data.ref.parent.child('count/users').set(event.data.numChildren());
});
Refer :https://firebase.google.com/docs/functions/database-events
root--|
|-users ( this node contains all users list)
|
|-count
|-userscount :
(this node added dynamically by cloud function with the user count)

Resources