How to get final iteration number in Meteor - meteor

In Meteor we have the '#index' operator to get the index value of the iteration. But I wanted to get the total number of iterations then print that number on the page. So the page at top might read the total number of boys in a group.
For example, I might have something like:
Total = {{#each StudentMale}} {{formatMaleCount #index}} {{/each}}
and a register helper just to add 1 to the number
Template.registerHelper('formatMaleCount', function (count) {
return count + 1;
});
and this would print:
Total = 1234567
I'd like to have:
Total = 7
Coming up short on how to do this. I tried to have the helper put the values in an array, but this wouldn't work since a new array is produced on each iteration.

StudentMale is presumably an array or cursor, so in a new helper:
If it's an array:
arrayLength( array ) {
return array.length;
}
Or if it's a collection:
studentMaleLength() {
return StudentMales.find().fetch().length;
}
Then just call your helper.

Related

Laravel collection condition in sum

I have a collection of transactions with relations and I would like to sum column separated by condition of relation column. Right now I have this:
$delegatedProvision = 0;
$ownProvision = 0;
foreach ($transactions as $transaction) {
if ($transaction->discount->consider_improvement) {
$delegatedProvision += $transaction->stats->$column;
continue;
}
$ownProvision += $transaction->stats->$column;
}
$this->salesCollection->put('delegatedProvision', $delegatedProvision);
$this->salesCollection->put('ownProvision', $ownProvision);
It works but I would like to use Laravel collections. So far I have just this:
$provision = $transactions->sum(function ($transaction) use ($column) {
return $transaction->stats->$column;
});
And I don't know how to use condition in sum() method and according column $transaction->discount->consider_improvement (which is boolean) have sum in separated variables. I can use filter each for different consider_improvement but it means that I have to iterate all transactions twice.
Try this:
$collection->where(/* your condition */)->sum($column);

Display result of a query that calculates the total of a field as the value of a label in a list

I have two Datasource tables Projects and tt_records with a hours number field. There is a one to many relation between the Project and tt_records. I would like to display the total number of hours per project in a table. I am able to compute the total hours in server side function, how do I bind the total with a label on the UI. I am attempting to use the following in the binding on the field. I see the function is called through info statements in the console logs, however the value does not display on the UI clientgetProjHours(#datasource.item._key); following is the Client Script
function clientgetProjHours(key){
return (google.script.run.withSuccessHandler(function (key) {
console.info("received result");
}).getProjHours(key));
}
Following is the server side script
function getProjHours(key){
console.log ("In the function getProjHours (" + key +")");
var pRecords = app.models.Projects.getRecord(key);
console.log("Contents of " + pRecords);
var tRecords =pRecords.tt_record;
console.log("Contents of t Records" + tRecords);
var total = 0;
tRecords.forEach (function (item){
total += item.actuals;
});
console.log ("The result is: " + total);
return total;
}
Could you please suggest the best way to achieve this fuction.
Thank you very much for your help
key parameter in function (key) { is the result of the Server Script.
So you just need to replace:
function (key) {
With:
function (result)
Also replace:
console.info("received result");
With:
app.pages.[PageName].descendants.[LabelName].text = result;
But as it mentioned already Calculated Model should fit such use case better.

ES2015 - updating filtered items in a collection and combining updating the previous collection

So say i have a collection:
const collection = [0,3,2,4];
var r = collection.filter((item) => {
return item > 2;
}).map((item) => {
return item + 2;
});
// I could just `.map()` them back in but that seems ineffective
console.log(r); // 5,6
Is there a functional (fp) way to compose this code to update my previous collection to get the expected result (given the index of the result is important) ?
e.g. expected result: [0,5,2,6]
How about
var r = collection.map(item => item > 2 ? item + 2 : item)
instead?
I.e. .map all items but only modify the ones that match your condition.

METEOR - Automatically increment order numbers

What I need to do is use either collection-2 or another package to automatically create a new order number, incremented from the last order number used.
i.e. Starting off with PO123456, when I save this order, the next time I make a new PO, it automatically generates the number PO123457.
I've been looking for a good example or tutorial, but I'm not able to find one.
Using konecty:mongo-counter in conjuntion with aldeed:collection2 and aldeed:simple-schema should be pretty straightforward. In your schema definition try:
POnumber: { type: String, autoValue: function(){
if ( this.isInsert ){ // restrict to when inserting a document
var currentNumber = incrementCounter('purchase order'); // this will use mongo-counter
// WARNING: you can only ever get as rich as 10M POs!!
var zeroPad = "000000" + currentNumber; // pad with 6 zeros
zeroPad = zeroPad.substr(zeroPad.length-7); // restrict to 7 places
return 'PO' + zeroPad; // prefix with 'PO'
} else if ( this.isSet ){
this.unset(); // prevent attempts to change the number
}
}

PHP manipulating multiarray data

I have a problem. I don't know how to show the exactly data I want.
My array is like this:
$races[$numRace][$finalPosition]
$races[1][1] = array ('stephan','1:27,895');
$races[1][2] = array ('george', '1:29,075');
$races[1][3] = array ('peter', '1:29,664');
$races[1][4] = array ('benson', '1:29,915');
$races[2][1] = array ('benson', '1:41,113');
$races[2][2] = array ('stephan','1:41,434');
$races[2][3] = array ('george', '1:43,654');
foreach ($races as $v1) {
foreach ($v1 as $v2) {
foreach ($v2 as $v3) {
echo "$v3\n";
}
}
}
This one shows me every data of $race array.
My question is: How can I do for showing just results for race 2?
Important: We don't know how many runners have participated on each race (So, we need a "foreach").
I would like a result like this:
benson
stephan
george
Simply iterate through only the relevant array. Based on your code your foreach should be:
foreach ($races[2] as $pos => $info){
echo $pos.': '.$info[0];
}
I've hard-coded the value 2 in the code but you could easily use a variable (foreach ($races[$raceNum]).
If you don't already know, => makes it possible for us to use both the key and the value as variables in our loop.

Resources