Can not bind updated oData to sapui5 view - odata

I am getting oData from backend.its data with timestamp and I need to get the difference from those timestamp.Which I did like bellow code.Now I want to add my that timestamp array to my view.Can I push those new array data to the view or how can I add update data to my sapui5 view.
Bellow are the codes.xml view:
GenericTile class="cm_productTile sapUiMediumMarginTop" header="Flow" visible="{=${dataResults>/flowSelected}==='true'}">
/flow}" class="sapUiSmallMarginTop">
<core:Fragment fragmentName="castor.fua.FUAsapui5FE.fragments.flowMainNode" type="XML"/>
<core:Fragment fragmentName="castor.fua.FUAsapui5FE.fragments.flowSubNode" type="XML"/>
var flowData = this.getView().getModel("elementsDataModel").oData.flow;
//for (var i = 0; i < flowData.length; i++) {
console.log(flowData);
var differences = [];
for (var i = 0; i < flowData.length - 1; i++) {
var diff = new Date(flowData[i + 1].createdAt) - new Date(flowData[i].createdAt);
var diffMins = new Date(diff).getSeconds();
differences.push({diff:diffMins});
}
So differences array is the new data and I need to add those values in the view.

If you can access the backend, best way is to calculate the timestamp difference in the backend and expose it as a property of your oDataModel.
Alternatively (but I don't know if it fits your requirements) you can put your data coming from the oData model in a JSONModel, elaborate them and then bind this JSONModel with the new 'timestamp' column to the view.

Related

Associate specific string to a number in google sheets

I am using a google forms to collect responses which I will then use to score people. Unfortunately some of those responses only make sense in a non numeric form, here is an example:
Q: What is your most common mode of transportation?
Car
Carpool
Public transportation
Bike
Walk
I want to be able to have google sheets automatically convert those string responses into a number, as in Car will be 20, carpool 15 and so on so that I can "grade" them and give them a score. Can this be done through google forms? Or maybe some sort of dictionary function?
Thank you!
Another method, requiring no coding, would be to make a worksheet with the encoding of the options and then use VLOOKUP to translate them.
Yep, this can be done through Google Forms. Have a look at https://developers.google.com/apps-script/reference/forms/duration-item#setPoints(Integer)
Using their code, you could go something like
var formResponses = FormApp.getActiveForm().getResponses();
// Go through each form response
for (var i = 0; i < formResponses.length; i++) {
var response = formResponses[i];
var items = FormApp.getActiveForm().getItems();
// Assume it's the first item
var item = items[0];
var itemResponse = response.getGradableResponseForItem(item);
if (itemResponse != null && itemResponse.getResponse() == 'Car') {
var points = item.asMultipleChoiceItem().getPoints();
itemResponse.setScore(points * 20);
// This saves the grade, but does not submit to Forms yet.
response.withItemGrade(itemResponse);
}
}
// Grades are actually submitted to Forms here.
FormApp.getActiveForm().submitGrades(formResponses);

Receiving array in route data to be used in Where func + MVC

I am receiving an array in route data containing different string values. Since I don't know how many string values I am going to receive, I cant have a definite Where function. Hence I am looping through the array and making multiple calls to DB set, fetching the result and adding it into another Master List. Please see code below.
public JsonResult GetModels(string brand)
{
string[] brands = brand.Split(seperator);
MasterList.Clear();
for (int i = 0; i < brands.Length; i++)
{
tempString = brands[i];
tempList = db.Devices.Where(r => r.Brand.Equals(tempString)).Select(r => new MySelectList { Value = r.PhoneModel, Text = r.PhoneModel }).Distinct().ToList();
for (int a = 0; a < tempList.Count; a++)
MasterList.Add(tempList[a]);
}
return Json(MasterList, JsonRequestBehavior.AllowGet);
}
Is there a way I can somehow avoid looping through the array and use it directly in Where function? Meaning Where function can look into values of the array and return result based on it.
You can do this by using Contains. This effectively generates an SQL IN clause:
tempList = db.Devices.Where(r => brands.Contains(r.Brand));

Initializing a 2-dimensions array in a domain class

I am trying to represent a 16*16 grid in a domain class. Currently the grid is represented by a one-dimension array. This works:
class Grid {
List cells = []
static hasMany = [cells:Cell]
void init() {
// Initialize cells
for(int x = 0; x < 16*16 ; x++) {
addToCells(new Cell())
}
}
}
But what is the right way to create a two-dimensions array? Also, is there a way of initializing all the cells without iterating?
A domain class represents an entity that is mapped onto an underlying database table. I realized it had no point in storing a 2D array as such in a database.
As #Tim_Yates suggested in the comments, it's better to use the collate method. I've kept the 1D array as a property and can still access a proper 2D-representation of the grid using:
Cell[][] cellz = cells.collate(edgeSize)
Cell myCell = cellz[5][9]

Fastest way to check Map for duplicate values?

Given a Map, assignment, what is the fastest way to check if it contains any duplicate values in Dart? I am currently using a Set formed from the Map's values and checking its length against the original Map, which works of course, but I'm wondering if there's an especially performant alternative.
Set d = new Set.from(assignment.values);
if (d.length < assignment.length) {
return false; // indicates has duplicates in this context
}
EDIT:
Tried #mezoni's solution modified for my program, but it actually ran a bit slower than my original version. It probably has more to do with constant times than anything else.
List values = new List.from(assignment.values);
Set set = new Set();
for (var i = 0; i < assignment.length; i++) {
if (!set.add(values[i])) {
return false;
}
}
Complexity wise you won't be able to get anything faster. Creating the Set and filling it with the values of the Map is linear in the number of elements. Clearly you have to run through all the values, so you can't do any better than that.
Maybe you could find a solution with a smaller constant factor, but that's not clear. In particular for larger sets I think the Set solution is pretty efficient.
This is more of a algorithms question than a Dart question. In any case, you have to check every value against the others, giving n-1 + n-2 + ... + n-(n-1) checks, or n^2/2. Programmatically, it's easy to create a set, but you could also generate an array, sort the array, and then iterate once to check for duplicates. That finishes in O(n log n).
Fastets way (if you realy need better performance):
void main() {
// Values from map
var values = [1,2,3,2,1,3,2,1];
var length = values.length;
var set = new Set();
var duplicate = false;
// Only for statistics purpose
var statistics = 0;
for(var i = 0; i < length; i++) {
statistics++;
if(!set.add(values[i])) {
duplicate = true;
break;
}
}
print("Duplicate: $duplicate");
print("Performed in ${statistics} iteration(s) from $length possible");
}
Output:
Duplicate: true
Performed in 4 iteration(s) from 8 possible
P.S.
The first example can be recommended to use with List values.
But because Map.values not a List but Iterable then it would be more efficient do not convert them to List but use as is.
Here is modified sample for use with Iterable objects.
It will be work faster because in this algorithm not required convert all values to the List object because it not want using of all elements without exception.
Instead it wants use as less as possible access operation on original source. If the source supports lazy operation of the access to values (as Iterable) this will be even better.
void main() {
// Values from map
var values = [1,2,3,2,1,3,2,1];
var assignment = {};
var length = values.length;
var key = 0;
for(var value in values) {
assignment[key++] = value;
}
var set = new Set();
var duplicate = false;
// Only for statistics purpose
var statistics = 0;
for(var value in assignment.values) {
statistics++;
if(!set.add(value)) {
duplicate = true;
break;
}
}
print("Duplicate: $duplicate");
print("Performed in ${statistics} iteration(s) from $length possible");
}

ASP.NET MVC 2 - HTML.EditorFor() and Custom EditorTemplates

With MVC 2's addition of the HtmlHelper EditorFor() it is not possible to create strongly typed Display and Editor templates for a given Model object and after fiddling with it I am a bit stumped as to how to pass additional Model data to the editor without losing the strong-typing of the editor control.
Classic Example: Product has Category. ProductEditor has a DropDownList for Category containing the names of all Categories. The ProductEditor is strongly typed to Product and we need to pass in the SelectList of Categories as well as the Product.
With a standard view we would wrap the Model data in a new type and pass that along. With the EditorTemplate we lose some of the standard functionality if we pass in a mixed Model containing more than one object (first thing I noticed was all of the LabelFor/TextBoxFor methods were producing entity names like "Model.Object" rather than just "Object").
Am I doing it wrong or should Html.EditorFor() have an additional ViewDataDictionary/Model parameter?
You can either create a custom ViewModel which has both properties OR you'll need to use ViewData to pass that information in.
I am still learning, but I had a similar problem for which I worked out a solution.
My Category is an enum and I use a template control which examines the enum to determine the contents for the Select tag.
It is used in the view as:
<%= Html.DropDownList
(
"CategoryCode",
MvcApplication1.Utility.EditorTemplates.SelectListForEnum(typeof(WebSite.ViewData.Episode.Procedure.Category), selectedItem)
) %>
The enum for Category is decorated with Description attributes to be used as the text values in the Select items:
public enum Category
{
[Description("Operative")]
Operative=1,
[Description("Non Operative")]
NonOperative=2,
[Description("Therapeutic")]
Therapeutic=3
}
private Category _CategoryCode;
public Category CategoryCode
{
get { return _CategoryCode; }
set { _CategoryCode = value; }
}
The SelectListForEnum constructs the list of select items using the enum definition and the index for the currently selected item, as follows:
public static SelectListItem[] SelectListForEnum(System.Type typeOfEnum, int selectedItem)
{
var enumValues = typeOfEnum.GetEnumValues();
var enumNames = typeOfEnum.GetEnumNames();
var count = enumNames.Length;
var enumDescriptions = new string[count];
int i = 0;
foreach (var item in enumValues)
{
var name = enumNames[i].Trim();
var fieldInfo = item.GetType().GetField(name);
var attributes = (DescriptionAttribute[])fieldInfo.GetCustomAttributes(typeof(DescriptionAttribute), false);
enumDescriptions[i] = (attributes.Length > 0) ? attributes[0].Description : name;
i++;
}
var list = new SelectListItem[count];
for (int index = 0; index < list.Length; index++)
{
list[index] = new SelectListItem { Value = enumNames[index], Text = enumDescriptions[index], Selected = (index == (selectedItem - 1)) };
}
return list;
}
The end result is a nicely presented DDL.
Hope this helps. Any comments about better ways to do this will be greatly appreciated.
Try using ViewData.ModelMetadata this contains all of your class Annotations.
Excellent article http://bradwilson.typepad.com/blog/2009/10/aspnet-mvc-2-templates-part-4-custom-object-templates.html

Resources