How to use setSortOrderProvider in Grid Vaadin 8? - grid

Im trying to use Grid Component. I need to define the order of a column, I'm using this proyect:
https://github.com/vaadin/tutorial/tree/v8-step4
And I add this code:
Column name = grid.addColumn(customer -> customer.getFirstName() + " " + customer.getLastName())
.setCaption("Name")
.setSortOrderProvider(direction -> Stream.of(
new QuerySortOrder("lastName", direction)
));
grid.setSortOrder(GridSortOrder.asc(name));
But I'm not getting the expected results, I'm getting ordered by firstName and then by lastName but i need the results ordered by lastName.
Have you had the same problem? How have you solved it?
Thank you.

I digged into the code and found out that you need you need to call setComparator instead of the setSortOrderProvider. The former is intended for in-memory data providers. Unfortunately, it's a little bit confusing and not really well documented.

I use this implementation of setComparator and it's working. : )
Column name = grid.addColumn(customer -> customer.getFirstName() + " " + customer.getLastName())
.setCaption("Name")
.setComparator(new SerializableComparator<Customer>() {
#Override
public int compare(Customer arg0, Customer arg1) {
return arg0.getLastName().compareTo(arg1.getLastName());
}
});
With Lambda:
.setComparator((customer0, customer1) -> {
return customer0.getLastName().compareTo(customer1.getLastName());
});
and this other option:
Column name = grid.addColumn(customer -> customer.getFirstName() + " " + customer.getLastName())
.setCaption("Name")
.setComparator(grid.getColumn("lastName").getComparator(SortDirection.ASCENDING));

Related

HTTPServletRequest getParameterMap() vs getParameterNames

HTTPServletRequest req, has a method getParameterMap() but, the values return a String[] instead of String, for post data as
name=Marry&lastName=John&Age=20.
I see in the post data it's not an array, but getParameterMap() returns array for every key(name or lastName or Age). Any pointers on understanding this in a better way?
The code is available in Approach 2. Approach 1 works completely fine.
Approach 1:
Enumeration<String> parameterNames = req.getParameterNames();
while (parameterNames.hasMoreElements()) {
String key = (String) parameterNames.nextElement();
String val = req.getParameter(key);
System.out.println("A= <" + key + "> Value<" + val + ">");
}
Approach 2:
Map<String, Object> allMap = req.getParameterMap();
for (String key : allMap.keySet()) {
String[] strArr = (String[]) allMap.get(key);
for (String val : strArr) {
System.out.println("Str Array= " + val);
}
}
If you are expecting pre determined parameters then you can use getParameter(java.lang.String name) method.
Otherwise, approaches given above can be used, but with some differences, in HTTP-request someone can send one or more parameters with the same name.
For example:
name=John, name=Joe, name=Mia
Approach 1 can be used only if you expect client sends only one parameter value for a name, rest of them will be ignored. In this example you can only read "John"
Approach 2 can be used if you expect more than one values with same name. Values will be populated as an array as you showed in the code. Hence you will be able to read all values, i.e "John","Joe","Mia" in this example
Documentation

operator + cannot be applied to java.lang.charsequence

Trying to make a calculator, here's the button method when the line with the * is ran it comes up with the error "operator + cannot be applied to java.lang.charsequence". If any one know's a way around this please help.
public void Button_Click(View view) {
TextView result = (TextView) findViewById(R.id.result);
Button b = (Button)view;
if ((result.getText() == "0")||(operation_pressed))
result.setText("");
operation_pressed = false;
if (b.getText() == ".") {
result.getText() = result.getText() + b.getText()); ***
}
}
Thanks
In order to convert a charsequence to a String apply .toString() to the CharSequence Object and you should be fine to use + operators:
[https://docs.oracle.com/javase/7/docs/api/java/lang/CharSequence.html][1]
Also it looks like you have a syntax error:
result.getText() = result.getText() + b.getText()); *** the last ) is too much
Furthermore you cannot write into getText() .. Getters (indicated by getXXX()) should be used to get values while Setters (indicated by setXXX()) should be used to store values into an object.
result.setText(result.getText().toString() + b.getText().toString())

Take a column and convert value to int

public int AuthenticatedUserAge(String User_name)
{
string sql = "SELECT UserName,Age FROM tblDataProg WHERE (UserName ='" + User_name + "')";
ds = GetDataSet(sql);
int help = int.Parse(ds.Tables[0].Rows[0]["Age"].ToString());
return help;
}
I can't figure why this line doesn't convert the age to type int and return a value:
int help = int.Parse(ds.Tables[0].Rows[0]["Age"].ToString());
Completely offtopic to the question, but I think it's worth mentioning. Please don't create your SQL statements by concatenating strings. This creates SQL Injection attack possibility. Instead consider SqlParameter class and compose your WHERE predicates using such parameters.
Here you get nice example (look especially at convenient AddWithValue method).
Thanks!
Test your assumptions more. You are assuming GetDataSet is returning a row but it possibly isn't: -
int help = 0;
if (ds.Tables[0].Rows.Count == 0)
{
throw new ApplicationException("no rows were returned, here is an error to deal with");
}
else if (ds.Tables[0].Rows[0]["Age"] == System.DbNull.Value)
{
throw new ApplicationException("a row was found but Age is null, here is an error to deal with");
}
else
{
help = int.Parse(ds.Tables[0].Rows[0]["Age"].ToString());
}
try
int help;
int.TryParse(Convert.ToString(ds.Tables[0].Rows[0]["Age"]),out help);
and see the code in debug mode

asp.net RowFilter comparing string with int

Im having an issue with this , Im new to asp.net and really dont have much understanding with their syntax, I do understand the error but have no idea how to implement a fix:
ProductTable.RowFilter = "ProductID ='" & ddlCategory.SelectedValue & "'"
The error im recieving is : cannot perform '=' operation on system.int32 and system.string data view
my productID data type is : int;
Now i dont see how im comparing a string vs int, ddlCatergory selected index would also return a value... besides the point...im completely stuck.
any help is appreciated.
Using windows 8, VS 2012 , SQL database.
ddlCategory.SelectedValue is likely to be string given the SelectedValue attribute of the drop down list is a string, you are also assigning it as a string e.g. the filter value could end up being something like "ProductID ='123'" where 123 is the SelectedValue.
Assuming that SelectedValue is never null or empty I would use something like:
ProductTable.RowFilter = string.format("ProductID = {0}", ddlCategory.SelectedValue);
If it could be empty then I would do something along these lines:
if(string.IsNullOrEmpty(ddlCategory.SelectedValue))
{
ProductTable.RowFilter ="";
}
else {
ProductTable.RowFilter = string.format("ProductID = {0}", ddlCategory.SelectedValue);
}
OR
int tempInt;
if(int.TryParse(ddlCategory.SelectedValue,out tempInt))
{
ProductTable.RowFilter = string.format("ProductID = {0}", tempInt);
}
I've written the above in C# (not syntax checked though sorry!) though it would be easy enough to convert it to VB if necessary.
You should try by removing single quotes after = sign, so it give something like this
ProductTable.RowFilter = "ProductID = " + ddlCategory.SelectedValue

How to get full category path from a keyword in Tridion

Can any one help me to get full category path from a given keyword. I am giving one example as below,
Example:
Category 1----> Keyword 1 -----> Keyword 11,
say from metadata i got the value "Keyword 11", but i need whole path i.e. /Category 1/ Keyword 1/Keyword 11.
Can anyone help me how to achieve this in Template Building Block using c#.
Maybe you can try and play with one of the following:
keyword.ParentKeywords recursively to create the path you are looking for.
OrganizationalItem oi = keyword.OrganizationalItem; // to get all the organizational items
keyword.OwningRepository
Hope that helps!
Below code should help you to get the path.
bool isRecursive = false;
KeywordField kwdField = (KeywordField)metaFields["kwdField"];
Keyword curKwd = new Keyword(kwdField.Value.Id, engine.GetSession());
string kwdPath = curKwd.Title;
while (!isRecursive) {
if (curKwd.ParentKeywords.Count > 0){
foreach (Keyword kwd in curKwd.ParentKeywords) {
kwdPath = kwd.Title + "/" + kwdPath;
}
curKwd = curKwd.ParentKeywords[0];
} else {
isRecursive = true;
}
}
kwdPath = curKwd.OrganizationalItem.Title + "/" + kwdPath;

Resources