Struts2 checkbox preselect - struts2

I am creating checkbox inside iterator. Below are my code,
<s:iterator value="contacts" var="contact">
<tr>
<td>
<s:checkbox name="selectContactsCheckBox" fieldValue="%{#contact.contactid}" value="%{defaultContacts.contains(contact.contactid)}" theme="simple"/>
</td>
<td>${contact.fullname}</td>
<td>${contact.mobile}</td>
<td>${contact.organization}</td>
<td>${contact.department}</td>
</tr>
</s:iterator>
this code creates checkbox and works fine. But i want to preselect this checkboxes using a collection from action.
Below is a method from my action calss,
public List<String> getDefaultContacts() {
return Arrays.asList(this.selectedContacts);
}
suppose if i have 100 contact in list and if getDefaultConatacs() return just 5 string then i want to select those 5 checkbox u

If i use below line(value attributes check whether the contactid is available in list)
<s:checkbox name="selectContactsCheckBox"
fieldValue="%{contactid}"
value="%{contactid in defaultContacts}"
theme="simple">
</s:checkbox>

Related

Displaying a check instead of a bool

I am using MVC and have an Index View. One of the fields that is displayed is type Bool. Currently, it will display a True/False for its value however is there a way to display a check mark or a checked checkbox when True and nothing if False?
My Field -
public bool PrimaryContact { get; set; }
My View -
#foreach (var item in Model.Contacts)
{
<tr>
<td>#item.PrimaryContact</td>
<td>#item.ContactType</td>
<td>#item.Contact1</td>
</tr>
}
If you absolutely want to show a checkbox, you can do this.
<td> <input type="checkbox" checked="#item.PrimaryContact" /> </td>
But a checkbox is a form input control. So if this is a view only screen, why not simply print "Yes" or "No" instead of the input control.
<td>#(item.PrimaryContact?"Yes":"No")</td>

Distinct in Ienumerable model

I have a IEnumerable ViewModel. This has the same names and I want to select only one name and display it in the header. Please suggest.
My code is a below. I am trying to use distinct but it does not work
#foreach (var item in Model.abc)
{
{
<table>
<tr>
<td style="width:100px" >#Html.Label("Name")</td>
<td style="width:225px"> #Html.DisplayTextFor(model => item.Name.Distinct())</td>
</tr>
</table>
}
}
To be more specific: my result will have only names {a,a,a,a,a} so I just want to pick the single {a} and display it.
This has the same names and i want to select only one name and display
it in the header.
If they all are the same and assuming Name implements IEnumerable you could try First:
Update: add a new property to your model
otherProperty = item.Name.First();
Then:
#Html.DisplayTextFor(model => item.otherProperty)
use this code in your app
#foreach (var item in Model.Select(x => x.Title).Distinct())
{
<option>#item</option>
}
you can use #html.display(X=>item) instead #item

Grails checkbox handling

Let's say, i have this scenerio:
But let's say i have hundreds of those checkBoxes, which i need to handle everything at same time after submiting a form. I then will need to save to the BD something based on which boxes are checked, and the id of each block
So, i need this:
a) a way to know which checkboxes are checked, within hundreds of them
b) each checkbox should be 'linked' with an id which im gona pass, so that a specific action will be performed.
I have a <g:each> tag writing me the whole table, reading values from the DB. I would appreciate any help with this,
Thanks in advanced, RR
You can bind the params to a List property of a domain object or command object.
View:
<g:each in="${elements}">
<g:checkBox name="elementSelected[${it.id}]" value="${it.id}" />
</g:each>
Command Object:
class ElementCommand {
List elementSelected
}
Controller:
def execute = { ElementCommand cmd ->
cmd.elementSelected.each {
if (it) {
processId(it.toInteger())
}
}
}
In your gsp you need to display all the checkboxes:
<g:each in="${model}" status="i" var="invoiceItem">
<tr class="${(i % 2) == 0 ? 'odd' : 'even'}">
<td>
<g:checkBox name="invoiceItem_${i}"/>
</td>
</tr>
</g:each>
In the controller action you need to map the selected checkboxes to your domain objects
List invoiceList = session.invoiceList
params.each {
if (it.key.contains("invoiceItem_")){
if (it.value.contains("on")){
InvoiceItem invoiceItem = invoiceList.get((it.key - "invoiceItem_") as Integer)
}
}
}

ASP.Net MVC 3 CheckBoxList from database

i have a table called
tb_role
id role
1 admin
2 user
3 viewer
and for the View is like this :
<div style="width:50%; float:right;">
<legend>User Role</legend>
<table>
<tr>
<th>Role</th>
</tr>
<tr>
<td align="center"><input type="checkbox" id="CBRole"/></td>
</tr>
</table>
</div>
I want to ask, how to list my checkbox(CBRole) from my table? so my CBRole is listed from my table.
thanks a lot
EDIT
assumed that i have Roles table like this :
tb_role
RoleId Role_Name
1 SalesCreate
2 SalesEdit
3 AgentCreate
4 AgentEdit
i want to list role for Sales in checkbox (SalesCreate and SalesEdit, so its only have 2 checboxes), how to do that ?
thanks
From your controller you populate your view model with these properties:
Your RoleViewModel
public IList<int> RolesSelected { get; set; }
public MultiSelectList Roles { get; set; }
From controller that handles the Get call (/roles/edit/1 for example)
model.RolesSelected = new List<int>();
//here the code to populate the eventually already selected roles (update case)
model.Roles = new MultiSelectList(repository.GetRoles(), "Id", "Name", model.SettoriSelected);
then in your view (inside a form tag) you will do something like this
#foreach (var item in Model.Roles)
{
<div class="MyClass">
<label for="#item.Value" class="MyClassForCheck">
<input type="checkbox" id="#item.Value" name="RolesSelected" value="#item.Value" #(item.Selected ? "checked" : "") />#item.Text</label>
</div>
}
in the controller that answer the Post part you will access the RolesSelected with the IDs checked
In the example I have put a div, but yuo can change it to what you like obviously. Hope it helps
You probably want to have something that looks like the following post on StackOverflow, Enum to CheckBox

DropDownList setting selected item in asp.net MVC

I noticed what seems to me a bug in asp.net MVC or simply I am doing something wrong. I am currently using 1.0 so maybe this is something that will be addressed in the 2.0 release. But either way, here we go.
When I my view model has a property which is the same name as the declared id for a drop down list, the selected item is ignored and the rendered html has nothing selected.
Not sure if I did something wrong, but changing the name of the id fixes the problem. I simplified the example, hope it is clear, otherwise please let me know.
Here is my view where the declared ID is the same name as my list in the model:
<table border="0" cellpadding="0" cellspacing="0">
<tr>
<td>
<%= Html.DropDownList("IsMultipleServicers", Model.IsMultipleServicers) %>
</td>
</tr>
</table>
And the rendered Html
<table border="0" cellpadding="0" cellspacing="0">
<tr>
<td>
<select id="IsMultipleServicers" name="IsMultipleServicers">
<option value="false">No</option>
<option value="true">Yes</option>
</select>
</td>
</tr>
</table>
Now lets make a small change. I will change the declared id to be something different.
Here is my View:
<table border="0" cellpadding="0" cellspacing="0">
<tr>
<td>
<%= Html.DropDownList("MultipleServicers", Model.IsMultipleServicers) %>
</td>
</tr>
</table>
And now the rendered html:
<table border="0" cellpadding="0" cellspacing="0">
<tr>
<td>
<select id="IsMultipleServicers" name="IsMultipleServicers">
<option value="false">No</option>
<option selected="selected" value="true">Yes</option>
</select>
</td>
</tr>
</table>
Notice that now I get a selected option which would be the second element in the List.
Here is my ViewModel just to tie everything together:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace MVCProject.Models.ViewModels.Service
{
public class ServiceViewModel : ViewModel
{
public List<SelectListItem> IsMultipleServicers { get; set; }
}
}
Here is my action:
[AcceptVerbs(HttpVerbs.Get)]
public virtual ActionResult Service()
{
return View(new ServiceViewModel()
{
IsMultipleServicers = BuildBooleanSelectList(true)
};
}
private List<SelectListItem> BuildBooleanSelectList(bool isTrue)
{
List<SelectListItem> list = new List<SelectListItem>();
if (isTrue)
{
list.Add(new SelectListItem() { Selected = false, Text = "No", Value = "false" });
list.Add(new SelectListItem() { Selected = true, Text = "Yes", Value = "true" });
}
else
{
list.Add(new SelectListItem() { Selected = true, Text = "No", Value = "false" });
list.Add(new SelectListItem() { Selected = false, Text = "Yes", Value = "true" });
}
return list;
}
I think the problem is a confusion regarding the DropDownList overloads:
Html.DropDownList(string name) looks for a view model property of name and type IEnumerable<SelectListItem>. It will use the selected item (SelectListItem.Selected == true) from the list, unless there is a form post value of the same name.
Html.DropDownList(string name, IEnumerable<SelectListItem> selectList) uses the items from selectList, but not their selected values. The selected is found by resolving name in the view model (or post data) and matching it against the SelectListItem.Value. Even if the value cannot be found (or is null), it still won't use the selected value from the list of SelectListItems.
Your code uses the second overload, but specifies a "value" property that doesn't exist ("MultipleServicers").
To fix your problem, either use the first overload:
<%= Html.DropDownList("IsMultipleServicers") %>
Or, add a string MultipleServicers property to your view model and populate it in your controller. I'd recommend this solution as it gets around several problems with initial display, post display and mapping the post data to a view/post model:
public class ServiceViewModel : ViewModel
{
public string MultipleServicers { get; set; }
public List<SelectListItem> IsMultipleServicers { get; set; }
}
Then for your HTML:
<%= Html.DropDownList(Model.MultipleServicers, Model.IsMultipleServicers) %>
This technique maps into MVC2, as well:
<%= Html.DropDownListFor(x => x.MultipleServicers, Model.IsMultipleServicers) %>
I encountered this same problem using the Html.DropDownList(string name, IEnumerable selectList) overload. It appears that my model has a property of the same name as the name of the drop down list. This being the case, MVC favored the property value of my Model over the Selected property of each entry in the IEnumerable.
The solution was to use a name for the dropdown list that does not match up to a property name. Another solution would be to write my own extension method that ignores model and view state and instead always honor the selected property.
The DropDownList helper pulls the default value from the model. In the first case, the value in the model corresponding to the name is a SelectList -- this doesn't match any of the items in the list, it is the list, so no value is chosen. In the second example, your model does not include a property with that name so the value from the model can't be used and it defaults to the state indicated in the SelectList itself. Typically, I will have a property on the model for the selected value -- this becomes the default -- and another property representing the potential values for the list.

Resources