Getting the sum from gwt table element - java

I have a table written in GWT from a List list. What i want it is to obtain the sum of a certain group of elements.
The problem with this, it is that the value I want to do the sum with its calculated, and thus not obtainable unless you calculate it before generating the List.
I was wondering if it was somehow possible to achieve this through DOM manipulation. And if so, how?.
I will show you an example:
DataA DataB DataC DataD
---------------------------------
aaaaa bbbbb ccccc 12
aaaa1 bbbb1 cccc1 15
aaa11 bbb11 ccc11 17
I want to get the sum of "DataD" column, but i dont know how can i do it.
Thank you in advance for your time,
Kind regards,
Elaborate: DataD column value it is calculated and the value comes from another system and its placed into the table through a third party program; thus i cannot get its value and use it into a sum to get the value i want.

You should be able to add some event handler to the table's widget so that whenever data is added/changed in the widget, you adjust the total. Please give more information about what widgets you are using to represent the table.
EDIT:
now that we know you are using an HTMLLayoutContainer, I assume DataD is being poopulated at a place beyond your controll in code. What you can do is add event handlers to handle the add remove etc events. For example, you could do something like below:
//Your container
HtmlLayoutContainer c = new HtmlLayoutContainer(templates.getTemplate());
c.addAddHandler(new AddEvent.AddHandler() {
#Override
public void onAdd(AddEvent event) {
//Do proper exception handling, check if it is the right widget for dataD, do the needed calculations etc
sum += Double.parseDouble(((HTML)event.getWidget()).getHTML());
}
});
c.addRemoveHandler(new RemoveEvent.RemoveHandler() {
#Override
public void onRemove(RemoveEvent event) {
//handle all conditions like in onAdd in AddHandler above
sum -= Double.parseDouble(((HTML)event.getWidget()).getHTML());
}
});
You can look into the API to see what exactl events match your needs best (http://dev.sencha.com/deploy/gxt-3.0.0/javadoc/gxt/com/sencha/gxt/widget/core/client/container/HtmlLayoutContainer.html)

Related

is there a function that can I use in condition to say (if a varaible was sent) so do something Anylogic

I would like to do a condition in my simulation that says (if the quantityToOrder was sent, then the currentStock decrement by the quantityToOrder value)
I didn't know how to do it in Anylogic.
screenshot of the problem
Do not do it like that. Condition-based modelling is very error-prone (same for state chart conditions).
Instead, wherever you send your orders off, you reduce the currentStock by the quantityOrdered
At that point, you can easily add if-conditions to check if you want to order in the first place, like:
if(weatherIsNice()) {
currentStock -= qtyOrdered;
}

Multiplication Field in Java / SceneBuilder

I'm a java Beginner and I've created a program where you can type in some food in a TableView and the details of the respective food you can type in a GripPane. One of the Details you have to type in is the quantity of the food, and another is the Calories per piece. Now I would like to create a button and a field. Or Maybe just a field that shows all calories of the food in the Table view. So it should multiplicate the quantity with the calories, for every food and add them all together. For a Total of Calories. Now I have no idea how to do that. Could somebody help me with step-by-step instructions? Not sure if it makes sense to add some code to the program. By the way, I use Eclipse on Windows and SceneBuilder. Thanks for every help.
Cheers Blarg
The first piece of advice from my side would be to try writing some code on your own! That way you learn and you wouldn't need to copy and paste somebody else's code.
And secondly, this is how I would approach it:
Create the fields as you described below in the Scene Builder and give them all id (names) so that we can access them in our controller (I am supposing you know how that works).
Add a button so that the user can click to perform the calculation
When the button is clicked, you can get all the information from each TextBox and create a Food Object with all the information. Performing the calculation is a rather simple task that can be done by converting the data received from the TextBoxes into numbers and multiplying
public void addFoodItemIntoTable()
{
...
String quantityOfFoodStr = quantityTextBox.getText();
int quantityOfFood = Integer.parseInt(quantityOfFoodStr);
String caloriesOfFoodStr = caloriesTextBox.getText();
double caloriesOfFood = Double.parseDouble(caloriesOfFoodStr);
double total = quantityOfFood * caloriesOfFood;
...
}
After adding all the elements in your TableView (Check this). You can easily get the total of the field by iterating all the elements of your table and adding them into a variable.
Example:
double total = 0;
for(Food currentFood : foodTable.getItems())
{
total = total + currentFood.getTotalCalculation(); // The naming should not be correct... Change it to whatever you find suitable
}
Good luck!

Trying to use an Esper Lambda Expression

I am trying to expand on an example from the Esper documentation for the where enumeration method, and having issues. Here is the example in question:
select items.where(i => i.location.x = 0 and i.location.y = 0) as zeroloc
from LocationReport
What I would like to do seems pretty simple. Instead of selecting items that match this expression :
I want to select LocationReports that contain at least one item that matches the expression.
Do it over a time_batch window (emphasized textnon-batched time window is a possibility as well).
So every n number of seconds I would receive a collection of LocationReports in which each report contains at least one zero location in its items List.
For Reference, here is the structure of the Java objects used in the Esper example:
public class LocationReport { List items; ...
public class Item { String assetId; // passenger or luggage asset
id Location location; // (x,y) location boolean luggage; //
true if this item is a luggage piece String assetIdPassenger; // if
the item is luggage, contains passenger associated ...
public class Location { int x; int y; ...
Background detail : Assume LocationReport is the actual object I am interested in... Using EPL like in the example above, the where logic works, but the problem is that, in returning only the items member, I do not see the LocationReport class it came from, which contains other properties besides items that my UpdateListener needs.
Also, probably not relevant, but in my case, I am receiving a high rate of messages where many LocationReports are duplicates (or close enough to be considered duplicates), and my where clause will need to make that determination and only forward "new" messages.
Thanks!
You could add the "*" to the select and that gives you the event objects alongside. select *, items.where(...) from LocationReport
You could add "output every N seconds" to output. Add "#time(...)" for the time window.

create array of values from heart rate sensor

i create an app that use in the SENSOR_HEART_RATE.
my app have one label that display the value of the heart rate, but the problem that i get a lots of values that changed every time, in addition the values change not frequent way.
there is possibility to save some values from the sensor and create array?
here part of my code:
public void onSensorChanged(SensorEvent sensorEvent) {
showHeartRate.setText("my heart rate:" + sensorEvent.values[0]);
int i=0;
while(i<10){
arrayHeartRateValues[i]= sensorEvent.values[0];
i++;
}
}
the problem is that my array have one value and not different values.
I've not played with the sensors of android, but I see a couple things that could be affecting the desired outcome of this.
First, you are using a preset array (arrayHeartRateValues) which will be prebuilt to a set size. Because you are not sure of the amount of sensorEvents you will be capturing, I would recommend using a more flexible option. Personally, I use the ArrayList class for most of those things, and the following code example will have that as well, but you may use whatever you like.
Secondly, You are (as stated in the comments) just setting all the values of the arrayHeartRateValues to be whatever sensorEvent.values[0] is.
Here is how I would handle this.
Somewhere in your code (wherever you initialize the arrayHeartRateValues) try this
ArrayList<SensorEvent> arrayHeartRateValues = new ArrayList<>();
Then in your method that catches the changes try this
public void onSensorChanged(SensorEvent sensorEvent) {
showHeartRate.setText("my heart rate:" + sensorEvent.values[0]);
arrayHeartValues.add(sensorEvent);
}
This should have the desired outcome.

Choices for combobox in a repeat

I've got a view that contains documents with various questions I want answered about Purchase Orders.
Using a repeat, I list all the questions. There are a few different kinds of questions, so I only render the answer field that I need based on the FieldType column value. I want to pull the choices for a combobox from the DialogChoices field on the question document.
I'm currently getting the choices showing as plain text on the next line after the empty combobox instead of as the selectItems. Where is my code going wrong?
<xp:comboBox id="comboBox1">
<xp:this.rendered><![CDATA[#{javascript:rowData.getColumnValue("FieldType") == "Dialog Box"; }]]></xp:this.rendered>
<xp:selectItems>
<xp:this.value><![CDATA[#{javascript:var doc:NotesDocument = rowData.getDocument();
var choicesVector:java.util.Vector= doc.getItemValue("DialogChoices");
var choices = [];
// loop through the vector, doing push into the array
for (i=0; i<choicesVector.size(); i++) {
choices.push (choicesVector.elementAt(i))
};
return choices;}]]>
</xp:this.value>
</xp:selectItems>
</xp:comboBox>
Strange, but a test database with the code above does not seem to give me strange results. Maybe it is because the data is in fact not an Vector but just a string?
Here are some tips :
The first thing you could change in your code is the loop to get all the data out of your field. Since the value property of a combobox already expects an array or vector you can change the code to something like:
<xp:this.value><![CDATA[#{javascript:var doc:NotesDocument = rowData.getDocument();
return doc.getItemValue("DialogChoices");
}]]>
</xp:this.value>
But it would be even better to remove the getDocument call at all. If possible you can add a column to the view are you are using for the repeat's datasource. In this column you get the data from the field directory. This way you can use the viewentry's getColumnValue() which is a performance optimization. Something like:
<xp:selectItems>
<xp:this.value><![CDATA[#{try{
return rowData.getColumnValue("DialogChoices");
}catch(e){// do something }]]>
</xp:this.value>
</xp:selectItems>

Categories

Resources