Grails checkbox handling - grails

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)
}
}
}

Related

Getting null or empty when i want to show image from data in MVC

I want to dispaly image from database in Mvc but it show me null or empty error. Value cannot be null or empty. Parameter name: contentPath.
Please tell me any one what are my mistake in this code:
#foreach (tbl_AdminManageProducts item in #Model)
{
<tr>
<td>#item.CategoryCode</td>
<td>#item.ProductName</td>
<td>#item.ProductPrice</td>
<td>#item.ProductDetail</td>
<td><img src="#Url.Content(item.ProductImage)" /></td>
<td>#item.CompanyName</td>
</tr>
}
This is controller:
public class AdminController : Controller {
// GET: /Admin/
public ActionResult Index() {
FYP_WISHEntities db = new FYP_WISHEntities();
return View(db.tbl_AdminManageProducts.ToList());
}
}
Looks like you are trying to call the Url.Content method with a NULL value. The possible reason for this could be a null value is returned from the expression item.ProductImage for one of the iteration of your loop. So adding a null check would fix your error.
<td>
#if(String.IsNullOrEmpty(item.ProductImage))
{
<img src="#Url.Content(item.ProductImage)" />
}
</td>
This code will try to render the image tag markup only if your item has a non null/non empty ProductImage property value.
Url.Content method takes a valid content path to the file. So make sure your item.ProductImage returns that when not null or empty.

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>

Struts2 checkbox preselect

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>

GRAILS g:each problem

I cant get the g:each to work. I am trying to iterate over anything but it never works = doesnt generate any html.
index.gsp
<g:each var="i" in="${userList}" controller="user">
<li>Item ${i.name}</li>
</g:each>
userController.groovy
class UserController {
...
def userList = {
User.list()
}
...
}
Then I have User.groovy filled with number of users..
What am i supposed to write in in="${.....}" to iterate for example over users declared as User.groovy ? I have tried : User, users, User.list() ...
Thanks
EDIT:
Let's say i have a
def findOne {
[users : User.findAllByNameLike("%Petr%")
}
in my UserCotroller.
How do i use g:each for it because
<g:each var="user" in="${findOne}">
won't do anything..
In your example. userList is a Closure, so it's the action name, so I'm assuming you're accessing http://localhost:8080/appname/user/userList
If you return something from a controller action to be rendered in the GSP, it has to be in a Map, the "model". Each value in the map is exposed in the GSP using the map key as its name. So the controller action corresponding to your GSP would be
def userList = {
[users: User.list()]
}
and then you can iterate with
<g:each var="user" in="${users}">
<li>Item ${user.name}</li>
</g:each>
The name doesn't matter - it just has to be the same in the model map as in the GSP.

Use 2 databases in the same query in Grails

In my grails application I have an sql query that selects column data from 1 database based on a column in another database.
Thus it is interacting with 2 databases in the SAME query.
Is there a way to pick data from 1 database based on data retrived from another database.
In a scenario like this, how would you put down the code.
Thoughts?
Found a way through.
Created 2 domain objects, Cdisc and Tape.
cdisc and tape are 2 tables in 2 different databases. Both the tables have a common primary Id, player_id.
The idea is to display data from these 2 tables on one page.
Cdisc.groovy
package tune
class Cdisc {
String name
double cId
String id
static mapping = {
table 'cdisc'
version false
columns {
id column:'player_id'
cId column:'c_id'
name column: 'name'
}
}
}
Tape.groovy
package tune
class tape {
String id
String tapeDate
String comments
static mapping = {
table 'tape'
version false
columns {
id column:'player_id'
tapeDate column:'tape_date'
comments column: 'comments'
}
}
public def getName(){
def cdisc = Cdisc.findById(this.id)
return cdisc.name
}
}
TapeController.groovy
def list = {
params.max = Math.min(params.max ? params.int('max') : 10, 100)
[tapeInstanceList: tape.findAllByCommentsLike('%Test%'), tapeInstanceTotal: Tape.count()]
}
Finally displaying name from table cdisc using the getter
list.gsp
<g:each in="${tapeInstanceList}" status="i"
var="tapeInstance">
<tr class="${(i % 2) == 0 ? 'odd' : 'even'}">
<td><g:link action="edit" id="${tapeInstance.id}">
${fieldValue(bean: tapeInstance, field: "id")}
</g:link></td>
<td>
${fieldValue(bean: tapeInstance, field: "tapeDate")}
</td>
<td>
${fieldValue(bean: tapeInstance, field: "comments")}
</td>
<td>${tapeInstance.getName()}</td>
</g:each>
Thus we are using a getter to get the data from the 2nd table in another database.
Worked for me. Let me know if theres any other workaround.

Resources