Stripes Checkboxes and Textfields - stripes

All -
I'm using stripes to do some form input for a problem I'm working on and I'm stuck on how best to submit a a pair of data using stripes and checkboxes.. for example my page looks like the following:
I have a list of options where users can enable a selection by clicking the box, and also supply some input for that item by entering data into the text field next to it:
<tr>
<td><stripes:checkbox name="item.enable" value="${item.id}"/></td>
<td><stripes:text name="item.value" value="${item.value}"/></td>
</tr>
.....
next item...
When the form is submitted I'd expect my Collection<Item> to be populated yet that's not the case..
How can I best submit a pair of items using the check box fields.
Thanks in advance.
..Chris

Read the documentation on indexed properties. You need to tell Stripes that you have multiple items, by naming them items[0], items[1], etc.:
<tr>
<td><stripes:checkbox name="items[0].enable" value="${item.id}"/></td>
<td><stripes:text name="items[0].value" value="${item.value}"/></td>
</tr>
<tr>
<td><stripes:checkbox name="items[1].enable" value="${item.id}"/></td>
<td><stripes:text name="items[1].value" value="${item.value}"/></td>
</tr>
This supposes that you action bean has a setItems(List<Item> items) method, that the Item class has a public no-arg constructor, and has a setEnable(String itemId) and a setValue(String value) method.

I would wrap this in a JSTL 'forEach' tag and I would put the items in a List. Similar to what JB Nizet said, you also need public setters in the action bean. If you are using Collection<Item> with some implementation other than List<Item> the below snippet won't work.
<c:forEach var='itemIndex' begin='0' end='2'>
<c:set scope='page' var='item' value='${items[itemIndex]}'>
<tr>
<td><stripes:checkbox name="items[${itemIndex}].enable" value="${item.id}"/></td>
<td><stripes:text name="items[${itemIndex}].value" value="${item.value}"/></td>
</tr>
</c:forEach>
There is another case when you don't want the list to default to 3 items. The one I'm thinking of is when the list is already populated. If that is the case I would change the 'end' attribute of the <c:forEach> to be: ${fn:length(actionBean.items) == 0 ? 3 : fn:length(actionBean.items)-1}

Related

How to Handle Blazor Wasm Table Input and Changes [duplicate]

How to handle row selected changed event for a table using Blazor?
I tried handling #onchange as well as #onselectionchange.
The syntax for the table looks like this:
<table class="table" #onchange="#this.SelectionChanged">
Your event binding doesn't work because the table element does not emit change events.
Instead, you could add an input element (for example a checkbox) inside your table rows. You can then handle row selection changes by adding your event binding to the input elements.
Read more on the HTMLElement change event in this article.
You can use Onclick in the row:
<tbody>
#foreach (var item in Forecasts)
{
<tr class="#item.Clase" #onclick="#(() => DoSomething(item))">
<td>#item.Date</td>
<td>#item.TemperatureC</td>
<td>#item.TemperatureF</td>
<td>#item.Summary</td>
</tr>
}
</tbody>
and create a Dosomething to receive the item

thymeleaf used in Spring, adding a list to iterate over with th:each

I want to use the the th:each construct to create a table of items. I have problems with adding the data to the model. I used this contruct:
model.addAllAttributes(repository.findByusername(username));
findByusername returns a java.Util.List of object. Then I always only have the last element in the list in the model. I gues that it depends on that the attribute namn is the class name so each addition of a new element in the list overrides the prviously added element.
How can I add each element as a separate attribute to the model and then easily iterate them with th:each?
Your Controller is should be return,
model.addAttribute("usersList", repository.findByusername(username));
And your thymeleaf code is should be like,
<th:block th:each="user : ${usersList}">
<td th:text="${user.username}">Test</td>
</th:block>

Unable to reference all Model objects in MVC View

I have an odd problem whereby the model in my vbhtml file will correctly reference and bring up the model's attributes for 1 item but not for another.
FYI, The page is added as a partial page, connected to another vbhtml page.
My page is structured such that I want to list a number of people (beneficiaries), below each question (question text).
I am referencing my Model type using the syntax: #ModelType RHEAL_START.QuestionWithAnswers, so this should automatically allow the model items to be declared when instantiated.
The #ModelType declaration correctly allows the referencing of the 2nd model field (Model.benAnswers) but not the first one I have declared (Model.questionText).
Both items are part of the same model and, I have tried moving the declarations above and below the and headers but, this did not bring up an IntelliSense for the Model.questionText.
I looked at the following post and tried addding in a reference to the Layout file but alas, this did not bring up an IntelliSense either.
MVC3 - render view that is not a method in a controller
See the code below. Can anyone suggest where I'm going wrong and what else I can try?
If you need more information, please let me know.
#ModelType RHEAL_START.QuestionWithAnswers
#Code
Layout = "~/Views/MedicalQuestions/MedicalQuestions.vbhtml"
End Code
<div id="questionPanel">
#Code
Dim qAndARef As Integer = 0
End Code
<!-- Show question text and number -->
<h1>Model.questionText</h1>
<table>
<tr>
<td style="font-weight: bold">
Beneficiary </td>
<td style="font-weight: bold">
Yes/No </td>
<td style="font-weight: bold">
Details </td>
</tr>
<!-- !!Iterating over each beneficiary for Medical Question -->
#For Each benanswer As RHEAL_START.BeneficiaryAnswer In Model.benAnswers
Html.Partial("../MedicalQuestions/BeneficiaryAnswerPartial", benanswer)
Next
</table>
#*#Html.ValidationSummary()*#
</div>
#Section Scripts
#Scripts.Render("~/Scripts/RHEAL/YesNoDropDownValidation.js")
End Section
I'm not that familiar with the VB syntax for Razor, but it looks like you're missing a # sign in front of #Model.questionText

MVC equilvelant of repeater with multiple form elements and checkbox to select

I need to replicate the functionality you get with an asp repeater that contains checkboxes, textboxes, and the occasional dropdownlist in an MVC application. Om traditional webforms you get a postback event and its pretty easy to get at all the controls and their values associated with the checkbox (id).
Anybody have any good suggestions on how to get the values back on a post for the items that have the checkbox checked. I am able to get some arrays back in the FormCollection (and even a strongly typed view) but I have not been able to figure out a good way to link the values effectivly.
All rows have a checkbox and a textbox, some rows also have a dropdownlist
To further explain...
row 1 has a checkbox and a textbox
row 2 has a checkbox a textbox, and a dropdown list.
if the user selects row 1 and 2, I need to get the values for all the form elements (for some calculations). Also, I haven't come up with a good method of handling validation errors.
any suggestions are greatly appreciated.
The simplest thing to do would be to iterate the collection for which you would have a repeater for. Using Razor syntax:
#using (Html.BeginForm()) {
<table>
#for (var i = 0; i < Models.Items.Count; i++) {
<tr>
<td>
#Html.CheckBoxFor(x => Model.Items[i].SomeBool)
</td>
<td>
#Html.TextBoxFor(x => Model.Items[i].SomeString)
</td>
</tr>
}
</table>
<button type="submit">Go</button>
}
Then a controller method:
[HttpPost]
public ActionResult Index(MyViewModel viewModel) {
// view model will be bound to values in form
}
Assuming the the Model in the view is of type MyViewModel. Once you have a sense of how this works you can look into ASP.NET MVC grid frameworks.

Spark View Engine Partial

I think I just need someone to show me the obvious.
I have a spark partial
_MessageItem.spark
that is used within a view
like so
<for each="var m in messageList">
<MessageItem message="m"/>
</for>
the partial looks like this:
<tr id="${message.MessageId}">
<td >${message.CreateDate.ToString("M/d/yy h:mm")}</td>
<td >
<b>${message.Subject}</b>
</td>
<td >${message.FromUser.FullName}</td>
<td >${message.ToUser.FullName}</td>
</tr>
<tr>
<td/>
<td colspan="3">${message.Body}</td>
</tr>
works like a champ, except when I try and call the partial directly from an action like so:
public ActionResult GetMessage(Message message)
{
return PartialView("MessageItem",message);
}
When I do the above I get
error CS0103: The name 'message' does not exist in the current context
So my current solution is to create a wrapper partial that feeds the MessageItem partial
like so:
_ActionMessageItem.spark:
<MessageItem message="(Message)ViewData.Model"/>
So can someone state the obvious and tell me how to modify
1) Modify my MessageItem partial so whether being called from PartialView() or within a .spark file it will work
2) Tell me how I need to modify my Controller Action so it won't throw an exception
<viewdata model="Message" message="Message" />
<var msg="message ?? Model" />
Then use the msg variable instead of message (like, ${msg.Subject}, etc).
You may also have luck with adding single
<default message="Model" />
but the first way is the one I think will work.
When you pass parameters PartialView, spark doesn't know anything about your parameter name, only the value that was passed in. So, it uses the name of the argument, which I believe it is model. Since your code is looking for the parameter message it throws an error. I think one solution might be to change to something like this:
<MessageItem model="(Message)ViewData.Model"/>
model may be cased as Model, you'll have to guess and check.
try to call the partial with the underscore and an anonymous object.
ViewData["message"] = message;
return PartialView("_MessageItem");
the problem is that when you call it from the action you're passing data as a Model, but when calling from another view you're passing the data as a parameter. you could only use your data as Model if the other view also shares the same Model object type.
otherwise, what I'd do is pass it in ViewData in your Action:
public ActionResult GetMessage(Message message)
{
ViewData["message"] = message;
return PartialView("MessageItem");
}

Resources