Iterate with index using thymeleaf - html

I am new to thymeleaf and am converting all my jsp code to thymeleaf.I don't know how to convert this below code to thymeleaf.Do anyone know how to convert the below code to thymeleaf?
<logic:iterate id="id" property="idList" name="sampleForm" indexId="i">
<label for="id<%=i%>">
<bean:write name="id" property="id" />
</label>
</logic:iterate>
Please tell me how to initialize the index value in thymeleaf to be used in some values??

<label th:each="id,status : ${idList}" th:for="|id${status.index}|" th:text="${id.id}"></label>
th:each will iterate over the idList, assign each item to id and create a label for each item. The status of the item can be assigned by adding an extra name, separated by a comma (status in this example).
th:for will set the for attribute of the label. The pipes (|) are used for easy string concatenation.
th:text will set the inner text of the label to the ID.

You can also use it like this:
<label th:each="id : ${idList}" th:for="${'id' + idStat.index}" th:text="{id.id}">
This starts the index from 0
If you want to start the index from 1 use this
<label th:each="id : ${idList}" th:for="${'id' + idStat.count}" th:text="{id.id}">
Check out the Thymeleaf documentation

Related

htmlspecialchars() expects parameter 1 to be string array given

I have an input field in which I am passing an array from my controller as hidden input but its giving me this error of array given.
Code of my view is
<input type="hidden" name="DiseaseDiagnosed[]" value="{{$DiseaseDiagnosed}}">
Code of controller passing the value to view is
return view('/doctorPanel/diagnoseDisease', ['chart' => $chart, 'patient' => $patient, 'symptomsStated' => $symptomsStated, 'DiseaseDiagnosed' => $DiseaseDiagnosed]);
Kindly tell me why I am getting this error
<input type="hidden" name="DiseaseDiagnosed[]" value="{!! jsond_encode($DiseaseDiagnosed) !!}">
Actually, your input is DiseaseDiagnosed is an array which is returned to view.
So you have to use {{ json_decode($DiseaseDiagnosed) }}
You can also try
#foreach($DiseaseDiagnosed as $disease)
<input type="hidden" name="DiseaseDiagnosed[]" value="{{ $disease }}">
#endforeach
Blade Template engine is producing this error. you can't print array like this using {{ }}. When passing this value, you can encode it using 'DiseaseDiagnosed'=>json_encode($DiseaseDiagnosed]), then you can use that syntax. After that when you want to use this value, don't forget to decode it using json_decode()
In order to create an array with inputs you need to have 1 input for each value inside the array. You are appending an array on the value when it only accepts Strings so thats why it warns you that an array was given when a String was expected.
As #Adnan suggested you can solve this using:
#foreach($DiseaseDiagnosed as $disease)
<input type="hidden" name="DiseaseDiagnosed[]" value="{{ $disease }}">
#endforeach
Then in your controller you will recieve the array DiseaseDiagnosed with all the values you inserted, eg: You will recieve all the values within the same array->
dd($request->DiseaseDiagnosed);
// You will see this is an array with all the values

Having trouble getting ID from JSON on Autocomplete

I have a typeahead that looks like this:
<input type="text" class='tk-proxima-nova degreeIn candidateProfile' placeholder="School / Institution" ng-model="university" typeahead="university.Service_Provider_Name for university in universitySuggest($viewValue)" />
it returns JSON that looks similar to this:
[{"Service_Provider_ID":133368,"Service_Provider_Name":"Duke University","Service_Provider_Desc":null,"NAICS_Id":1809},{"Service_Provider_ID":196282,"Service_Provider_Name":"Duke University Medical Center","Service_Provider_Desc":null,"NAICS_Id":1809},{"Service_Provider_ID":222220,"Service_Provider_Name":"Duke University Psychology Internship","Service_Provider_Desc":null,"NAICS_Id":1809},{"Service_Provider_ID":223427,"Service_Provider_Name":"Duke University Medical Center Psychology Internship","Service_Provider_Desc":null,"NAICS_Id":1809}]
When I select the option from the typeahead it puts the school name in the field as it's supposed to, but is there a way to also set the id to another hidden input so I can send that with my selected data as well. The ID is what is important here, but the name is needed for viewing.
Yes, you can do something like :
<input type="text"
ng-model="selected"
typeahead-on-select="changeSelect($item)"
typeahead="university as university.Service_Provider_Name for university in universities | filter:{Service_Provider_Name:$viewValue} " />
Here is the Plunkr
I've a little indented your code for more clarity.
In addition to the comments Apercu gave above regarding the format of the typeahead="..." ,you need to have a typeahead-input-formatter like this
typeahead-input-formatter="univChosen($model)"
$scope.univChosen = function(theId) {
var theItem = jQuery.grep($scope.universities,function(p) {
return p.Service_Provider_ID == theId});
return theItem[0].Service_Provider_Name;
};
I'm assuming here that $scope.universities contains the complete list of all the items in the typeahead choices. I've used jQuery here to find the matching item but you can just do a for(...) loop if you want. The function returns the string that will actually get displayed, in this case the name.

Is there an HTML 5 way of submitting fields with the same name but different identifiers?

I would like to submit multiple ROWS of field columns using an HTML form with the only difference being the corresponding server side identifier.
Pseudo:
ID(257) -> Name, Color, Price
ID(415) -> Name, Color, Price
I prefer NOT to:
Have each group as its own form and submit via JavaScript.
Concatenate the id and real name and unmerge on the server.
Thanks
You can use the same input name when attaching a [] to it, in PHP this will result in an array on submission:
<?php
print_r($_POST);
?>
<form method="post">
<input type="text" name="name[257]">
<input type="text" name="name[415]">
<input type="submit">
</form>
Result:
Array
(
[name] => Array
(
[257] => first field
[415] => second field
)
)
If you're using PHP as your server side language this is easy. Just name your fields with brackets which is array syntax in PHP:
Pseudo code:
name[1] = 'Name, Color, Price'
name[2] = 'Name, Color, Price'

Struts 1 bean tag inside HTML quotes

Here is my code:
<html:text name="rptData" property="emailAddress" onblur="checkIfEmpty('<bean:write name="rptData" property="status" />' , this.id)" />
This is nested inside a logic:iterate tag. What i need to do, is to pass value of 'status' to the javascript checkIfEmpty method. But i guess there is some error in the quotes. It is not working properly. Please anyone guide me. Thanks.
You can't nest custom tags like that, period, and it's unfortunate most of the answers imply you can, because that represents a fundamental misunderstanding of JSP.
Ideally, you wouldn't use the <logic:iterate> tag, as its use is not recommended when you have JSTL available: when JSTL and Struts 1 tag functionality overlaps, use the JSTL option. Then use standard JSP EL to access the variable.
<c:forEach items="listOfThings" var="current">
...
<html:text ... onblur="checkIfEmpty('${rptData.status}', ${current.id})" ...
...
In this example I also assumed that this was supposed to refer to the current iteration object, defined in the <c:forEach> tag.
ok after doing some research i found out the answer.
if i have some code like this:
<html:text styleId="emailAddress+<%=statusC.toString() %>" name="rptData" property="emailAddress" />
its output is as follows:
<input type="text" name="emailAddress" value="abc#gmail.com" id="emailAddress+<%=statusC.toString() %>" />
but if i use
<html:text styleId="<%=statusC.toString() %>" name="rptData" property="emailAddress" />
the output is:
<input type="text" name="emailAddress" value="abc#gmail.com" id="Approved" />
That means without string concatenation the output is correct. i.e. only using
styleId="<%=statusC.toString() %>"
rather then
styleId="emailAddress + <%=statusC.toString() %>"
or even
styleId="emailAddress" + <%=statusC.toString() %> - This results in an error, though
yeilds correct output.
So the workaround is to first intialize a complete java String in a scriplet then use it inside the styleId tag.
<% String emailId = "emailAddress" + statusC.toString() ;%>
<html:text styleId="<%=emailId%>" name="rptData" property="newEmailAddress" />
It will work fine. Cheers !
You might want to try adding some escape characters while using double quotes inside double quotes like in your case it would be some like:
onblur="checkIfEmpty('<bean:write name=\"rptData\" property=\"status\" />' , this.id)"
Try this:
<html:text name="rptData" property="emailAddress" onblur="checkIfEmpty('<bean:write name=\'rptData\' property=\'status\' />' , this.id)" />

dynamic struts <html:option>

How to write dynamic struts <html:option>, I'm doing as below
<logic:iterate id="data" name="sendEmailForm" property="eventList">
<html:option value="<bean:write name="data" property="eventId"/>"/>
<bean:write name="data" property="title"/>
</html:option>
</logic:iterate>
but getting following error:
JSPG0069E: Unmatched end tag found while parsing jsp. Expecting
logic:iterate found html:option at [87,130]
is there another way to do so?
Thanks in advance.
<html:option property="title">
<html:optionsCollection name="sendEmailForm" label="title" value="eventId" />
<html:option>
html:option tag
property="?" whatever you want to set like if attribute is title and corresponding method (getTitle(), setTitle(String title) ) then property= "title"
html:optionsCollection tag
name = "?" whatever you have put in your request/session attribute
like (request Or session).setAttribute("sendEmailForm", ListArr);
then name="sendEamilForm"
listArr is array of object of bean ( may be formbean or simple pojofile/bean).
label ="?" what ever you want to show like if you have attribute 'title' and
corresponding setter(setTitle(String title)) and getter(getTitle()) method then
label="title"
value ="?" what ever you want to put as value in option. you call your getter method
corresponding attribute like if you have attribute eventId and method (
setEventId(String eventId) , getEventId() ) then value="eventId"
ok finally i found below for solution
<html:optionsCollection name="sendEmailForm" property="eventList" label="title" value="eventId" />