I'm using primefaces autocomlete force selection to search over accounts by name
JSF Component
<p:autoComplete value="#{journal.journal.debit}"
completeMethod="#{account.completeAccount}"
var="var" itemLabel="#{var.name}" itemValue="#{var}"
forceSelection="true" immediate="false" required="true">
Bean Method:
public List<Account> completeAccount(String query) {
List<Account> allAccounts = service.get(Account.class);
List<Account> filteredAccounts = new ArrayList();
for (int i = 0; i < allAccounts.size(); i++) {
Account foundAccount = allAccounts.get(i);
if (foundAccount.getName().toLowerCase().contains(query.toLowerCase())) {
filteredAccounts.add(foundAccount);
}
}
return filteredAccounts;
}
this works fine, now if I want to change the search to search also for account number in the query value. I have used the following:
if (foundAccount.getName().toLowerCase().contains(query.toLowerCase()) || foundAccount.getNumber() == Integer.parseInt(query)) {
filteredAccounts.add(foundAccount);
}
but then, the filter is only returning searching for the number and ignoring name search. How can I achieve this?
I think that what Deepak was trying to say is that what you're doing is perfectly valid, and not an issue with primefaces rather something wrong with your condition. And indeed, the most obvious thing is that there is no Integer.parseInteger(String s) method, at least no in java.lang.Integer.
If this is a typo of some sort, and if you're working with Integer objects (not the int primitives) , make sure that you're comparing them by using .equals method. Comparing Integers by == will work only in the range from -128 - 127, outside of that range it will compare references.
Hope it helps
try this
if (
(foundAccount.getName().toLowerCase().contains(query.toLowerCase()))
||
(foundAccount.getNumber() == Integer.parseInteger(query))
)
{
filteredAccounts.add(foundAccount);
}
Related
I have a page with two inputtext (I'll name them "name" and "id"): one that compares the text entered with a string and another one that compares it with an int converted to string.
Now the problem is this: if I have an "id" == 1 and type 1 into the inputtext, the autocomplete shows only results with two or more digits/chars (so 11,31,117 etc but not 1)...
This is the html:
<p:autoComplete id="CustomerId" value="#{myBean.CustomerBean.id}"
completeMethod="#{myBean.autoCompleteId}"
maxResults="10">
<p:ajax event="itemSelect" listener="#{myBean.selectCustomerById}"
update="resultMessage name idComp table newAssociation" />
</p:autoComplete>
And this is the autocomplete method:
public List<String> autoCompleteId(String query) {
CustomerList = myService.selectByFilters(CustomerBean);
setAcList(new ArrayList<String>());
for (int i = 0; i < CustomerList.size(); i++) {
CustomerAnag tip = CustomerList .get(i);
if(String.valueOf(tip.getId()).contains(query) || String.valueOf(tip.getId()).equals(query)) {
acList.add(tip.getId().toString());
}
}
return acList;
}
What in the sweet heaven am I doing wrong ?!
I have a custom rest control written in Java. Everything works fine for string fields, but not for multi-value string fields. My code returns
"[Value1, Value2, Value3...]". Note there are no commas around the values. On the web page the output looks like this:
If I can get commas around the values, the front-end framework can easily parse it.
I have tried to parse the value from the field and to get it formatted correctly, but cannot seem to get it right.
The first set of code works for a string. The second is an attempt to work for a multi-value field.
Any help would be greatly appreciated.
writer.startProperty("chgTitle");
writer.outStringLiteral(String.valueOf(columnValues.get(0)));
writer.endProperty();
writer.startProperty("plpAffected");
Object tmpObj = columnValues.get(5);
if (tmpObj instanceof Vector) {
String[] copyArr = new String[((Vector) tmpObj).size()];
((Vector) tmpObj).copyInto(copyArr);
writer.outStringLiteral(String.valueOf(copyArr));
} else {
}
writer.endProperty();
I would recommend a slightly different approach. I like to force my JSON structures to use an array anywhere a multiple value situation is possible. It also looks like you're using Philippe Riand's specialized JSON writer, so I'll assume that.
Here's about how I would attack it:
writer.startProperty("chgTitle");
writer.outStringLiteral(String.valueOf(columnValues.get(0)));
writer.endProperty();
writer.startProperty("plpAffected");
Vector<?> tmpVec = Util.getValueAsVector(columnValues.get(5));
writer.startArray();
for ( Object ob : tmpVec ) {
writer.startArrayItem();
// assuming String contents
writer.outStringLiteral(String.valueOf(ob));
writer.endArrayItem();
}
writer.endArray();
writer.endProperty();
To wrap the returning columnValues, I'm using a Java equivalent of my SSJS getValueAsVector helper function. It checks for Vector or ArrayList, which I happen to use almost exclusively; if it's not, it shoves it into a new Vector. Here's what that method looks like,
public static Vector<?> getValueAsVector(Object obj) {
if(obj instanceof java.util.Vector){
return (Vector<?>) obj;
}else if( obj instanceof ArrayList ) {
List<?> o = (List<?>) obj;
Vector<Object> tmpVec = new Vector<Object>();
for(int i=0;i<o.size();i++){
tmpVec.add(o.get(i));
}
return tmpVec;
}else {
Vector<Object> tmpVec = new Vector<Object>();
tmpVec.add(obj);
return tmpVec;
}
}
I am using RadGrid with Nested Hierarchy of Master/Detail Tables. I want to Expand the Master Row when the detail Table inside the row has few rows. I am trying to achieve the same using below code
Private Sub RadGrid_ItemDataBound(ByVal sender As System.Object, ByVal e As Telerik.Web.UI.GridItemEventArgs) Handles dbgView.ItemDataBound
If <considtion to check if row is expanded>Then
e.Item.Expanded = True
End If
However even after setting the Expanded flag as True, if I check the value of the state in QuickWatch, it still remains False. Can someone help me understand why state for that specific row is not getting changed? If this is not the right way in changing the state programmatically, can someone let me know the alternate way?
Try to rebind the grid after setting the Expanded property or simply move the condition in the Page_Load method. Just make sure that after the changing of the value, there is rebind or the NeedDataSource method is executed.
Hope these suggestions help.
For resolving the issue , i used two hidden fields
<asp:HiddenField ID="hdnExpandCollapse" Value="0" runat="server" />
<asp:HiddenField ID="hdnExpanded" Value="0" runat="server" />
Then the following two grid events are used to capture the state of the grid item
/* Start functions used for collapse the grid */
protected void Grid_PreRender(object sender, EventArgs e)
{
int i = 0;
foreach (GridDataItem item in Grid.MasterTableView.Items)
{
GridTableView DetailsTable = (GridTableView)item.OwnerTableView;
System.Collections.Hashtable ht = DetailsTable.DataKeyValues[i];
string strDataKey= ht["DataKey"].ToString();
if (strDataKey == hdnExpandCollapse.Value)
{
if (hdnExpanded.Value == strDataKey)
{
item.Expanded = false;
hdnExpanded.Value = "0";
}
else
{
item.Expanded = true;
hdnExpanded.Value = strDataKey;
}
}
i++;
}
}
protected void Grid_ItemCommand(object source, Telerik.Web.UI.GridCommandEventArgs e)
{
if (e.CommandName == RadGrid.ExpandCollapseCommandName)
{
hdnExpandCollapse.Value = ((EntityClass)(e.Item.DataItem)).DataKey.ToString();
}
}
/* End functions used for collapse the grid */
Reply
I have an url (e.g. http://localhost/Aanbod/Pagina.aspx) and I want to know the tab id, so I can make a friendly url with query (e.g. http://localhost/Aanbod/Pagina/QueryKey/QueryValue/).
Anyone has an idea?
Edit:
I'm not on the page itself. Want to know it from any page possible.
The url does not contain the tab id itself, so it can't be extracted.
if Pagina.aspx is a page in dotnet nuke like Home or Getting Started then you can find the tab id by
DotNetNuke.Entities.Tabs.TabController objTab = new DotNetNuke.Entities.Tabs.TabController();
DotNetNuke.Entities.Tabs.TabInfo objTabinfo = objTab.GetTabByName("Pagina", this.PortalId);
int Tabid = objTabinfo.TabID;
Well, this post is a little bit old, and I don't know if someone still looks for a solution. I had this problem recently and here is the pieces of code I wrote to solve it:
public int GetTabIDFromUrl(string url, int portalID)
{
int getTabIDFromUrl = 0;
// Try the "old" way with the TabID query string
if (url.ToLower().IndexOf("tabid") > 0)
{
Int32.TryParse(Regex.Match(url, "tabid[=/](\\d+)", RegexOptions.IgnoreCase).Groups[1].Value, out getTabIDFromUrl);
}
// When there is no result (because of advanced or human friendly or whatever Url provider)
if (getTabIDFromUrl == 0)
{
TabCollection tabs = TabController.Instance.GetTabsByPortal(portalID);
foreach (KeyValuePair<int, TabInfo> k in tabs)
{
TabInfo tab = k.Value;
if (tab.FullUrl.StartsWith(url))
{
getTabIDFromUrl = tab.TabID;
break;
}
}
}
return getTabIDFromUrl;
}
This could be a pain with sites that have a lot of pages, therefore it could be useful if you have some additional information to shrink the list that you have to loop through - e.g. a ModuleId of a module that is placed on this tab:
public int GetTabIDFromUrl(string url, int moduleID, int portalID)
{
int getTabIDFromUrl = 0;
// Try the "old" way with the TabID query string
if (url.ToLower().IndexOf("tabid") > 0)
{
Int32.TryParse(Regex.Match(url, "tabid[=/](\\d+)", RegexOptions.IgnoreCase).Groups[1].Value, out getTabIDFromUrl);
}
// When there is no result (because of advanced or human friendly or whatever Url provider)
if (getTabIDFromUrl == 0)
{
IList<ModuleInfo> modules = ModuleController.Instance.GetTabModulesByModule(moduleID);
foreach (ModuleInfo module in modules)
{
TabInfo tab = TabController.Instance.GetTab(module.TabID, portalID);
if (tab.FullUrl.StartsWith(url))
{
getTabIDFromUrl = tab.TabID;
break;
}
}
}
return getTabIDFromUrl;
}
Hope that helps someone...
Happy DNNing!
Michael
I hope this will solve your issue
http://www.willstrohl.com/Blog/EntryId/66/HOW-TO-Get-DNN-TabInfo-page-object-from-TabId
Sorry, my bad!!
Here is your answer
http://www.dotnetnuke.com/Resources/Forums/forumid/118/threadid/89605/scope/posts.aspx :)
In this code:
<html:text property="txtItem5" disabled="disTxtItem5">
I can see that "txtItem5" comes from a getTxtItem5() in the ActionForm, but searching the project for other substrings of "disTxtItem5" seemingly reveals nothing remotely related, though clearly somehow the framework is pulling a boolean from this string, which clearly means that it's more complicated than my current understanding.
Could someone give a good explanation of how these expressions are evaluated, or point me in the direction of one?
EDIT: In my original response (see below) I said that Struts manages the conversion, but I was wrong. I didn’t remember exactly what was going so I pulled out Struts’s sources and took a look. It turns out that the conversion is made by the server. The JSP is converted to a servlet before execution and it is here that false is used for non boolean values.
For example, I used the following tag:
<html:text property="nr" disabled="BlaBla" />
Which was translated to the following html (no disabled):
<input type="text" name="nr" value="123">
This happened in the servlet. Here is what my servlet contains for the above tag:
// html:text
org.apache.struts.taglib.html.TextTag _jspx_th_html_005ftext_005f0 = (org.apache.struts.taglib.html.TextTag) _005fjspx_005ftagPool_005fhtml_005ftext_005fproperty_005fdisabled_005fnobody.get(org.apache.struts.taglib.html.TextTag.class);
_jspx_th_html_005ftext_005f0.setPageContext(_jspx_page_context);
_jspx_th_html_005ftext_005f0.setParent((javax.servlet.jsp.tagext.Tag) _jspx_th_html_005fform_005f0);
_jspx_th_html_005ftext_005f0.setProperty("nr");
_jspx_th_html_005ftext_005f0.setDisabled(false);
int _jspx_eval_html_005ftext_005f0 = _jspx_th_html_005ftext_005f0.doStartTag();
As it can be seen, the disabled value is generated with false, directly. I did some more digging into the Jasper compiler (I used Tomcat) and I think that it is the org.apache.jasper.compiler.JspUtil class that is responsible for the conversion, with the following code:
public static boolean booleanValue(String s) {
boolean b = false;
if (s != null) {
if (s.equalsIgnoreCase("yes")) {
b = true;
} else {
b = Boolean.valueOf(s).booleanValue();
}
}
return b;
}
Since I inserted BlaBla in the disabled field this should fallback to Boolean.valueOf(s).booleanValue(); which does the following:
public static Boolean valueOf(String s) {
return toBoolean(s) ? TRUE : FALSE;
}
private static boolean toBoolean(String name) {
return ((name != null) && name.equalsIgnoreCase("true"));
}
This way, BlaBla results in false.
ORIG: The following was my original response, but was incorrect. What I was describing was in fact what happens when request parameters are binded to the action form.
The disabled attribute is of type boolean, so it should receive only values that map to a boolean. disabled="disTxtItem5" would throw a ConversionException because the text disTxtItem5 does not map to a boolean.
Struts uses CommonBeanUtils to make conversions, so a BooleanConverter will be used, with code like the following:
String stringValue = value.toString();
if (stringValue.equalsIgnoreCase("yes") ||
stringValue.equalsIgnoreCase("y") ||
stringValue.equalsIgnoreCase("true") ||
stringValue.equalsIgnoreCase("on") ||
stringValue.equalsIgnoreCase("1")) {
return (Boolean.TRUE);
} else if (stringValue.equalsIgnoreCase("no") ||
stringValue.equalsIgnoreCase("n") ||
stringValue.equalsIgnoreCase("false") ||
stringValue.equalsIgnoreCase("off") ||
stringValue.equalsIgnoreCase("0")) {
return (Boolean.FALSE);
} else if (useDefault) {
return (defaultValue);
} else {
throw new ConversionException(stringValue);
}
At this point I don’t remember if Struts just logs the exception and fails silently setting false as the value of the parameter or, the exception is propagated (it’s been a while since I used Struts :D but I’m more inclined to think that it just sets false and continues).
The logs should point out the exception even if it is ignored. Setting a logger for org.apache.commons.beanutils or org.apache.struts should indicate any conversion errors.