Why value is viewing with [] in grails view - grails

I am calling an action by remoteFunction for showing some value in some field.The value is viewing but with in []. I have no idea why it is behaving like this. Can anyone please help me on this please ? I am using grails 2.1.0. here are my attempts below :
my remoteFunction >>
<g:remoteFunction action="setValueForDetails" params="'procurementMasterId='+procurementMasterId" update="changedValue"/>
my action in controller >>
def setValueForDetails(){
def otmIFQDetailsByProcurementMaster
if(params.procurementMasterId != null && params.procurementMasterId != "" && params.procurementMasterId != "null"){
otmIFQDetailsByProcurementMaster = commonService.getOtmIFQDetailsValueByProcurementMaster(Long.parseLong(params.procurementMasterId))
}
render (template: 'ifqDetails', model: [otmIFQDetailsByProcurementMaster: otmIFQDetailsByProcurementMaster])
}
my field where I want to set the value in template >>
<g:textField id="PROCUREMENT_TYPE" name="PROCUREMENT_TYPE.id" readonly="" value="${otmIFQDetailsByProcurementMaster?.PROCUREMENT_TYPE}" class="form-control" />

I guess the 'PROCUREMENT_TYPE" is an Array of enums due to spelling, and displaying. So if You want to 'print' value without square brackets, You should change value to (if You want only first result):
value="${otmIFQDetailsByProcurementMaster?.PROCUREMENT_TYPE[0]}"
or if You want to should more than one element from list:
value="${otmIFQDetailsByProcurementMaster?.PROCUREMENT_TYPE.toString().replace('[', '').replace(']', '')}"
or simply iterate through the elements of PROCUREMENT_TYPE and show as many textfield as many PROCUREMENT_TYPE values You have.

Related

issue with arguments to messages.properties, all numbers except zero work correctly

In my Grails 2.4.4 app I am using messages.properties for internationalization, with the following value:
my.app.thing.allowance(s)={0} Allowance(s)
and it is being using in a gsp like so:
<g:message code="my.app.thing.allowance(s)" args="${item.allowances.size()}"/>
the message is correctly displayed for any values greater than 0, for example if item.allowances.size() == 4 then the message displayed is 4 Allowances(s)
the issue is that if item.allowances.size() == 0 then the message displayed is {0} Allowance(s)
I have tried to write the args in several different ways, for example:
<g:message code="my.app.thing.allowance(s)" args="${item.allowances.isEmpty() ? 0.intValue() : item.allowances.size()}"/>
I have debugged things and I am sure that item.allowances.size() == 0 but for some reason it can not handle a value of 0 properly. What is the correct way to pass an argument with an int value of 0 to messages.properties?
In g.message arguments are always passed as an List.
From: http://docs.grails.org/3.0.17/ref/Tags/message.html
args (optional) - A list of argument values to apply to the message when code is used.
Try this code instead:
<g:message code="my.app.thing.allowance(s)" args="[item.allowances.size()]"/>
The Bharat's answer is correct, but I want to add why it happened so:
You have passed args=0
And here it is code from message tag lib:
List args = []
if (attrs.args) {
args = attrs.encodeAs ? attrs.args as List : encodeArgsIfRequired(attrs.args)
}
In groovy 0 is false, that's why you didn't have filled in message in case of ZERO

multiple expressions for single condition result

I have to change the header string of primefaces dialog, acording to a variable state in my backing bean. The condition would be the following (pseudo code):
#{backingBean.editing ? resourceBundle.edit_string resourceBundle.item.id : msg.add_string}
and the short snippet example:
<p:dialog id="dokDialog" header="#{backingBean.editing ? resourceBundle.edit_string resourceBundle.item.id : msg.add_string}" ...>
<!-- content -->
</p:dialog>
In this example I want to display either value #{msg.edit_string} #{resourceBundle.item.id} or #{msg.add_string} according to boolean value of #{backingBean.editing}.
What I want to do is to show either Editing Item 01 or New Item in the title.
Also I get the following exeption because I have two expressions (resourceBundle.edit_string resourceBundle.item.id) for one result:
Caused by: org.apache.el.parser.ParseException: Encountered " <IDENTIFIER>
Thanks!
resourceBundle.edit_string resourceBundle.item.id - it's a wrong expression. You need to concatenate
String.concat may help if you are using an appropriate version of EL: resourceBundle.edit_string.concat(' ').concat(resourceBundle.item.id)

Grails GSP null safety check trouble

I am trying to populate a text field on my GSP as such:
<label>Phone(aaa-bbb-cccc):</label>&nbsp<g:textField name="phone" style ="border-radius: 5px"
value="${recordToEdit.telephones = [] ? null : recordToEdit.telephones.first()}"></g:textField><br>
but it still tell me I can't access first() on an empty list. telephones is a List of Strings each being a phone number.
as #gross-jonas pointed out, the recordToEdit.telephones = [] ? .. : .. is terribly wrong already, unless it's a typo
the check you are trying to make should look like:
value="${recordToEdit.telephones ? recordToEdit.telephones.first() : ''}"
or
value="${recordToEdit.telephones?.getAt( 0 ) ?: ''}"
You can just use the Null Safe Operator (?.) as
${recordToEdit.telephones?.first()}
for null checks, which is not sufficient.
UPDATE
for empty list checks and null checks,
${ recordToEdit.telephones ? recordToEdit.telephones[0] : '' }
will be good.
Dude, didn't you just mean == instead of = ?
It looks like you are overwriting your telephones which get issued successfully instead of comparing it.

rich:autocomplete strange behaviour

We are upgrading from jsf 1.2 to jsf 2.
We are using apache myfaces 2.1 and rich faces 4.3.
We are facing very strange issue with <rich:autocomplete>. Below is the xhtml code.
<rich:autocomplete mode="cachedAjax" minChars="2" autocompleteMethod="#{bean.getData}"
var="result" value="#{bean.inputData}">
<h:column>
<h:outputText value="#{result}" />
</h:column>
</rich:autocomplete>
Following is the scenario , when an input is entered in an autocomplete input box , suggetions are shown - that is bean.getData method is called.
Surprisingly , after any of the suggestion is selected , bean.getData method is called again with chosen option value , which I believe is not the correct behaviour
since user has selected an option and not typed any input.
I don't want the bean.getData method to be called again. Is there any alternative ?
Please help.
EDIT 1
Since I am running a db query to get suggestion values , they are different than the actual input value typed to get suggestions. That is if I type "at" , my sugegstions could be "check me" . I understand that once prefix for search is changed , suggestions are evaluated again , but here i am not changing the prefix but selecting the entire suggestion value.
EDIT 2
Providing the context the issue occured with.
When autoCompleted method is called , it populates a hashMap with all the suggestions with CustomUserObject as the value.
Below is the code for this :
public List<String> getData(FacesContext context, UIComponent component, String input) {
Map<String, CustomUserObject> userMap = new HashMap<String, CustomUserObject>();
//get users from db
List<CustomUserObject> users = bean.fetchUserList(input);
//put users in map
for (CustomUserObject user : users) {
userMap.put(user.id, user) ;
}
List<String> userList = new ArrayList<String>();
//convert the list to List<String>
if(userList != null && !userList.isEmpty()){
//convert the List<CustomUserObject> to List<String>
}
return userList;
}
Now because of the issue mentioned in the question , getData is called again and corrupts userMap.
This map is used to retrieve correct CustomUserObject by comparing it with selected Suggestion like below :
if(userMap != null && !userMap.isEmpty()){
for(CustomUserObject user : userMap.values()){
if(selectedSuggestion != null && selectedSuggestion.equals(user.name)){
//match is found
//set variables to update ui at re-render
}
//no match found
//set variables to update ui at re-render
}
}
EDIT 3
In addition to above issues , it seems that there is no "nothing label" attribute for rich:autocomplete which was present for rich:suggestionBox which comes into picture when there is no result found.
This issue alongwith above issue is really making a difficult job to get this component working same as rich:suggestionBox

How to change my view file to get a appropriate output?

I have a controller like this :
def mytask = {
def user = User.findByLogin(params.id)
def mywork = user.schedules.daySchedules
[ mywork : mywork ]
}
Where I'm trying to find all the tasks assigned to a particular user. I have a corresponding view file :
<g:each in="${mywork}" var="tasks">
<div id = "todayswork">
${tasks.task}
</div>
<div id ="Dates">
${tasks.startTime}-
${tasks.endTime}
</div>
<hr/>
</g:each>
Logic works fine, I'm getting the output as I wanted. For example, if I go to http://localhost:8080/scheduleTest/daySchedule/mytask/anto my browser showing all the task for the user anto. But there is a problem in rendering it.
I'm getting the output as :
But I need the output something like this one:
How change my view file to get the appropriate output.
Thanks in advance.
It's hard to tell from your examples, but my guess is you need to be looping over the tasks item, which appears to be a List in a List.
This means change this:
<g:each in="${mywork}" var="tasks">
to this
<g:each in="${mywork[0]}" var="tasks">
// or
<g:each in="${mywork.tasks}" var="tasks">
Again, I'm not exactly sure where the problem is occurring, but one of those will fix it.
The reason you are getting the output is that Groovy will automatically perform a property expansion on a list if the property is not defined on that list. Example:
use(org.codehaus.groovy.runtime.TimeCategory) {
def d1 = 5.minutes.ago
def d2 = 1.week.from.now
assert [d1, d2].time == [d1.time, d2.time]
}
It's the same thing as writing list*.property, and returns a new list containing each property on the original items.

Resources