Vue.js update subtotal in array - javascript

After some research i find this post what have an example about how to manage a complex array in Vue.js
https://forum.vuejs.org/t/vuex-best-practices-for-complex-objects/10143/2
I have create this jsbin (https://jsbin.com/jocezud/5/edit?html,js,output) to make some test but i´m stuck at this point. I want to make some totals based in the checked topics:
A) When the checkbox of each Topic is checked the price value must be copied to the subTotal value.
B) Also every user must have the userTotal updated with the sum of the subTotal of the checked Topics.
Here is a capture about my goal.
Thanks in advance

Finally i get it working with also a Final total and a Reset feature.
Here is the actual working version (https://jsbin.com/jocezud/12/edit?html,js,output)
Anyway i think there must be a better way to, in this case run some calculations of data in arrays when a array value change happens, right?

Related

Retrieve random key from firebase database

I face the problem that I have a list of keys inside my firebase database and I want to retrieve one random key out of it, without downloading all keys and selecting one in my own app. Downloading all keys with for example .once() is a big no go, especialy because I may up end with 10.000+ keys in the future and I dont want my firebase bills to explode, Im still a student with no budget at all.
I already researched this topic and found following answer for my question.
The problem with this is, that I can not use limitToFirst() and also limitToLast() in the same query, else I get following error:
[Unhandled promise rejection: Error: Query.limitToLast: Limit was
already set (by another call to limit, limitToFirst, or limitToLast).]
Also other people face the same problem like me and I generaly see that this question always gets much views but rather poor answers which are, like apperently in the case I mentioned earlier outdated, or just not for usage in Javascript and React-Native.
Additional informations: I have a counter for my keys so I know at every time how many keys are stored inside my database. Following picture shows my database entirely:
This is the part of my code which shows the mentioned error:
const numberOfUsers = await firebase.database().ref("usercounter").once("value").then(snapshot => {
return(snapshot.val()); //retrieve numbers of users from "usercounter"
});
const randomIndex = Math.floor(Math.random() * numberOfUsers) + 1; //"generating random number, excluding 0"
var ref = firebase.database().ref('users'); //ref to my users keys
await ref.limitToFirst(randomIndex).limitToLast(1).once('value').then(DataSnapshot =>
{
//This is the query that does not work at all.
console.log(DataSnapshot.val())
});
Incase there is a way to overcome that problem, please let me know.
I would solve that problem this way:
Create for each list element a value with the field name id or some other you want. It should be a simple integer. Idealy incrementaly growing with the list. You could use the counter to increase the value each time you add an element.
When trying to get a random element from the list first get a random integer between 0 and the total count of all elements. Then use this query to get a single random element:
firebase.database().ref("users").orderByChild("id").startAt(randomNumber).limitToFirst(1);
Even if you don't hit the id with the randomNumber you would the neareas next element. This should be able to scale to any size.
The most diffictult part would be to optain the id incrementaly without repeating them. I would use for that an cloud function that does it when an item is added. Even a combination with the counter would be interesting.

Vue - Percent of how many field has filled on form

I want to know how many percent of the data function is filled;
console.log(this.$data);
This is giving that
I am planing to count all of these fields. Even if they are an object I will count each key of object.
Then I am planing to also count which one of them is filled. Then i will get percent. If you thing there is better I need your help.

How to monitor inventory javascript?

This is my first time in this web site, I hope that you may help me. Thanks in advance. Mainly I need two things.
To create something with JavaScript for compare a field (inv_product.min_exi) from my database against existence of inventory. Wherein existence <= inv_product.min_exi.
And when existence <= inv_product.min_exi then send to email for several emails accounts
All in JavaScript because I'm using Pentaho
Thanks again.
Not sure if you need this solution : I have assume a DataGrid input passing the inventory value and used a JavaScript Step to compare the values (check the 2nd image). I have hardcoded the value of existence for testing purpose.If the condition is True, set a variable and then use a Filter Step to send out the mails.
This is a very simple example. Not sure if this is what you are looking for. By the question posted, if you are looking for doing all the above using JS, then you have to write codes for Mail in JavaScript.
Hope it helps :)
STEP by STEP Explaination:
Data Grid - it holds the input data. In your case, the values of "inv_product.min_exi". This can be any source.. You can use Table Input Step also and a PostgreSQl database.
Modified JavaScript Value - Use this step to do a initial checking condition. In your case "existence <= inv_product.min_exi". You can also do any other Javascript checkings. In the example i have given, i have considered the value of existence to be equal 2. But if the value of existence is dynamic or coming from a different source, you can remove this hardcoded value and use a dynamic input streaming.
Now if a condition is getting satisfied, you can assign a value to a variable like "flag_ex = 1" or "flag_ex=0".
Filter rows - You can filter out the input stream based on the flag_ex value. As per my example, if flag_ex=1 then it satisfies true else false.
Mail - Finally send out Mail of the True conditions.

How to read from another row in javascript step of Pentaho?

I'm working on an ETL process with Pentaho Data Integration (Spoon, before Kettle).
In the Modified Javascript step of Pentaho you can set a start, end and transform script. In the transform script you can write code that it will be executed only for each row, and from here I don't know how to access to data of the previous row (if it's possible).
I need access to the previous row because all rows are ordered by product, store and date (respectively), and the goal is to get the quantity on hand from the previous row and add the quantity sell or received on the current row (this would be the same product, same store but different date). I also need accessing to the previous row to compare the product and store of the current row with the previous row, because if someone of them changes I must to restart the field quantity_on_hand (I do it with a field of all columns named initial_stock).
On pseudocode would be something like this (if I hadn't the restriction of that the code written on the step is executed only for each row):
while(all_rows_processed()){
current_row.quantity_on_hand = current_row.initial_stock;
while(id_product_current_row == id_product_previous_row && id_store_current_row == id_store_previous_row){
current_row.quantity_on_hand = previous_row.quantity_on_hand + current_row.stock_variation;
}
}
This question related couldn't help me.
Any ideas to solve my problem would be appreciated.
May I ask you to reconsider Group By step? It seems suitable for your scenario.
If you sort the stream accordingly to your combination date/store/article, you can calculate cumulative sum for sell/received quantity. This way you can have a running total of inventory variation that would be reset on a group basis.
Also give a look both at this blog post and at the forum post it quotes.
I doubt you need to go to JavaScript for this. Check out the Analytic query step. That will allow you to bring a value from the previous row into the current.
The JavaScript step gives you tremendous flexibility, but if you can do it with the regular transform steps, it will typically be much faster.
use Analytic Query. By Using this Step u can access the previous / next record. Actually, not only prev and next record that you can read, but you can read N Rows Fordward or N Rows Back Wards.
Check the following URL for clearer expalanation :
http://wiki.pentaho.com/display/EAI/Analytic+Query
http://www.nicholasgoodman.com/bt/blog/2009/01/30/the-death-of-prevrow-rowclone/
Thanks for all, I've got the solution to my problem.
I've combined all your suggestions and I've used the Analytic Query, Modified Javascript and Group by steps.
Although the question wasn't very well formulated, the problem I had was to calculate the stock level on each row (there was one row for each product, date and store combination).
First (obviously later than sort rows by product_id, store_id and date ascending), I used the Analytic Query step to group by product_id and store_id, because with this step I've got a new field previous_date to identify the first row of each group (previous_date=null on the row of the group where date was the oldest).
Then I needed to calculate the quantity_on_hand of each group [product,store] at first row (first date of each group because it's sorted by date) because the initial_stock is different for each group. This is because of (sum(quantity_received) - sum(quantity sold)) != quantity_on_hand.
Finally (and the key was here), I used the Group by step like #andtorg suggested and do it as the next image shows.
This link that #andtorg suggested was very useful. It includes even two .ktr example files.
Thank you so much for help!

ExtJS: How can I prevent the sum of number columns in a GridPanel from going over a given maximum?

I have a GridPanel with just two columns: names and commission percentages. The sum of all of the percentages should never exceed a predetermined maximum (say that 25% of the sale is up for grabs).
Name Commission
Bob 10%
Sally 15%
(Maximum is 25%)
Everything is fine.
I’ve attached a custom validator (see "validator") to the NumberField editor which works just great for complex logic on individual values, and I can sum up all of the values for the number column in the data store easily. But the problem is combining the two; the new value isn’t pushed to the store until after a successful validation, and the custom validator is only given the current value in question, with no context as to what row is involved (with which I could potentially subtract the old value, and use this new value in the total).
How can I get this validator to work in such a way that it makes sure that a newly entered value doesn’t push the sum over the maximum?
Name Commission
Bob 10%
Sally 16% (Invalid! Total commissions must not exceed 25%)
(Maximum is 25%)
You need to pull in the dirty value of the changed element before it is saved to the database. It does not make sense to validate the clean value after the fact.
See here for info on data storage in ExtJS:
http://www.fusioncube.net/index.php/managing-dirty-rows-in-a-sencha-extjs-editorgridpanel
You can use properties such as field.originalValue and field.getRawValue() to get at the data store and see the difference.
I didn’t quite use js1568’s answer, as I wanted to block bad values as they’re being entered; even before the AfterEdit event fires and the row gets marked dirty, and long before writing to the database. But that link did inspire me through its use of the grid’s selection model. So, my quick little solution:
Because the NumberField doesn’t pass any context to the custom validator (aside from the current value), using field.originalValue would have been awesome, but hey, life is hard.
But due to the fact that editing a row first requires selection of that row (and reinforced by the fact that this grid’s RowSelectionModel is set to SingleSelect), I know that the row being edited must be the only element in grid.selModel.selections.items[]. From there, it’s just a matter of
var newTotal = TotalCommissionPercentagesInGrid() – oldValue + newValue;
With oldValue being grid.selModel.selections.items[0].data.CommissionPercentage and newValue being the value passed to the validator function from the NumberField.
return ( newTotal <= percentageAvailable ) ? true : "No dice.";
Very nice.

Categories

Resources