Need to store the text field data and store and then sumI am creating multiples sub category Just like to-do on button click it creates a field where user can put his/her input.
The Input is product Price Now I wanna store this all data in similar list As it's subcategories for some product and wanna display their total sum along side all values.
Consider using fold function
arrayName.fold(0, (result, eachItem) => result+eachItem)
it will add current item to result(which is 0 initially) and return it
eg: given your data of List< String > items
here is your a function that will parse the items to int and return the sum
int getTotal()=> items.fold(0,(total, item) {
int? price = int.tryParse(item);
if(price!=null) return total+price;
else return total;
});
Although according to your existing code you have on bug, which is the price is not being updated form the text field
Therefore you must add onChanged to your text field
onChanged:(value) => items[index]=value,
Then you can display it like this
Text("Total price: ${getTotal()}"),
Related
I have an array of thousands of rows with an element of "Order Number." I want to filter that array where that Order Number does not exist in a Dataverse table column.
I've tried a number of things, always starting with a List Rows action on the Dataverse table. From there, I feel like the thing to do is to do a Select action where I map OrderNumber to OrderNumber from the List Rows. I believe that creates an array of the order numbers.
I'm not sure if I'm on the right track, but how can I efficiently filter the original array where the Order Number does not exist in the Dataverse table?
Edit: Here's a sample item in the output of my current filter array:
This might help if you are able to use javascript.
// where output equals your data
const records = JSON.parse( output );
// we will create new array of filtered data
let newData = [];
// loop array searching for matching criteria
for (let n = 0; n < records.length; n++; ) {
// replace 123456 with search criteria
if(records[n]["Order Number"] != "123456") {
newData.push(records[n]);
}
// once you reach the end
if(n == records.length-1) {
// stringify object (may not be required)
let fileteredData = JSON.stringify(newData);
/* open filteredData with program */
}
}
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];
}
I am sending a string to my controller containing seat numbers: A1, A2 etc. And a string containing an ID.
I want to select each db row from the seats table where the column: seatnum equals each of the seat numbers in the seatss string and update a related column called status from 0 to the ID string.
I have used the below code to select the row where the seatnum = seatss string and return the matching row as a json object. This works if there is one seat number in the string but not if there are multiple. I'm assuming I would need to implement an array of some sort but have no idea how to do so. The seat numbers are posted as this: A1, A2, A3. So I'd like to separated/explode them on the , and then match them to the database rows.
//
// POST: /seats/
public JsonResult Checkout(string seatss, string ID)
{
var seats = from b in db.seats
select b;
seats = seats.Where(b => b.seatnum.Equals(seatss)); // Selects the db row where .seatnum = string seatss.
//Update seatnum to the value of seatss
return Json(seats, JsonRequestBehavior.AllowGet); // returns the selected seats as a json object.
}
So in summary I need a way to match each seat number in the string to the corresponding seatnum in the database and update the status to the integer contained in the ID string.
My database table(seats) has two columns called seatnum and status. The seatnum column contains rows: A1 etc and the status is 0 by default.
You could either change the way that you are passing your seat numbers, so instead of a string parameter named seats your action could expect an array or a generic list of string values, or you could just cast your current string parameter to an array and then modify you LINQ query to use a contains function. The sample provided is of the latter implementation since the former is a little more involved, of course this is untested:
public JsonResult Checkout(string seatss, string ID)
{
var seats = from b in db.seats
select b;
seats = seats.Where(b => seatss.Contains(b.seatnum));
foreach(var seat in seats)
{
... Perform status update on seat
}
return Json(seats, JsonRequestBehavior.AllowGet); // returns the selected seats as a json object.
}
I have a Map of type Sobject as a key and Integer as a value. The Sobject has field of type Text, other field of type date and another field of type Number(16,2). When I put data into the map and then debug it, the map returns data in a sorted way. The map sorts it by the Number field present in the Key i.e the number data field of the object. Can I get the map sorted by the date field of the object which is its key? Below is the rough structure of my Map and the key object fields.
Map<Effort_Allocation__c, Double> cellContent;
Effort_Allocation__c.Allocated_Effort_Hours__c; //The Number field by which the map gets sorted
Effort_Allocation__c.Assignment_Date__c; // The date field by which I want the map to get sorted
It's a bad idea to use an object as a key for a map. You should, instead, use the object's ID as the key. The reasons for this are discussed in detail here. Short version - the object's values may change which changes the object's hash value and wrecks your map.
Although maps cannot be sorted directly, lists can be sorted, so they can be used to access map elements in sorted order. You'll need a wrapper class for your object that implements the "Comparable" interface. There's an example of that here. Note that the example sorts by date.
The class is declared with "comparable"
global class AccountHistoryWrapper implements Comparable{
and has the following CompareTo method
global Integer compareTo(Object compareTo) {
// Cast argument to AccountHistoryWrapper
AccountHistoryWrapper aHW = (AccountHistoryWrapper)compareTo;
// The return value of 0 indicates that both elements are equal.
Integer returnValue = 0;
if ( aHW.account.CreatedDate > aHW.account.CreatedDate) {
// Set return value to a positive value.
returnValue = 1;
} else if ( aHW.account.CreatedDate < aHW.account.CreatedDate) {
// Set return value to a negative value.
returnValue = -1;
}
return returnValue;
}
I have a grid within a form as in the attached image. When the the customer name is changed, then the grid store is loaded with records corresponding to the customer. I want the balance textfield to be populated with the sum of Amount due column.
The image is here.
Use like this:
store.load({
scope : this,
callback: function(records, operation, success) {
//here the store has been loaded so you can use what functions you like.
//This code sum numbers in certain column
sum = 0;
store.each(function (rec) { sum += rec.get('NameColumn'); });
}
});
Use the store summary function:
var sum = grid.getStore().sum('NameColumn');
sencha api: sum store