How to know which checkbox (es) is/are selected - grails

I have this result from the DataBase. The number of result can be one, more than one, or none at all. For every register returned, a table is created, and inside one column a checkbox is displayed. In the view, I need to know which one of the checkboxes are checked, so I can pass the content of another column of the table to the controller, to be processed. It's like "Database returns me a list of people, and I want to send an email to some of them, not all of them. I select the checkbox, look for the "email" column and if the checkbox of that person is checked, the email is sent".
Is there any (preferably, easy) way to achieve that?
Thank you.

If you give all your checkboxes the same name, but distinct values:
<g:checkBox name="sendTo" value="${firstPerson.id}" checked="${false}"/>
....
<g:checkBox name="sendTo" value="${secondPerson.id}" checked="${false}"/>
then in the controller you can use
params.list("sendTo").each { personId ->
def person = Person.get(personId)
// send email to this person
}
params.list() is a handy tool to know about when dealing with potentially multi-valued parameters. A standard params.sendTo would give you null if no checkboxes were selected, a String if one is selected, and a List if two or more are selected. Using params.list("sendTo") will always give you a List (with zero, one or more than one element as appropriate).

Related

grails application design for multiple checkbox scenario

I have a Domain lookup table of Ethnicities. A person can have multiple ethnicities - caucasian, indian, latino, etc. There are around 10 total.
In one part of my application a user will select their own ethnicity, so they will check one or more of the checkboxes. In another part of the application a different type of user can specify to be matched with someone by ethnicity. This user can also specify one ore more, but the can also specify 'no preference'.
So the first user would see:
caucasian
latino
asian
indian
...
The second user sees:
No preference
caucasian
latino
asian
indian
My question is how to implement this in the lookup table/domain user object. The 'no preference' is throwing me off. Is it a boolean in the user object, or another value in the ethnicity lookup table that gets filtered out when the view for first user is displayed?
There is also the option of having the no preference triggering a check all by jquery behind the scenes so that second user would have every ethnicity as part of their their domain object. But then I have to do kludgy stuff like 'does the second user have every ethnicity stored in their domain, if yes then check 'no preference' in the view'.
EDIT:
It's probably better to show the UI. This is what I need to implement. The ethnicities are stored in a look up table. My domain will look something like this:
class Profile P
static hasMany = [ethnicity:Ethnicity]
...
}
And here is what the other use will see:
if ANY == [caucasian, latino, asian, indian......] then additional property is not required.(if user select ANY, you add all ethnicities)
ELSE you can use a list [ANY, caucasian, latino, asian, indian...]. although 'ANY' is not ethnicity.

AutoID Field in Many-to-Many, Insert Using TDataSet / Query (Delphi / MSAccess)

I have a database of "Items", which are assigned to multiple "Categories". Categories can have multiple items, and vice versa. The relevant portion of the database structure is as follows:
[tblItem]
ItemID (AutoNumber)
MainText (Text)
[tblCategory]
CategoryID (AutoNumber)
Name (Text)
[tblItemCategory]
ItemID (Long Integer)
CategoryID (Long Integer)
I want to build a panel component which shows a category name at the top, with a databound grid of items belonging to that grid below. There will be many instances of this panel component, and the end-user should be able to create a new item and simultaneously assign it to the category in question from any one of them.
In MS Access, it's possible to create a nested form, with the "child" one databound to a query which is "MasterFields" linked to a databound "Category" field on the "parent" form, such that the grid of items changes as the Category field is changed. This Items grid can also easily have new records added to it, with both the ItemID (in tblItem AND table tblItemCategory) and the linked CategoryID field (in tblItemCategory) being populated automatically.
The query for that Access form's grid is:
SELECT tblItemCategory.CategoryID, tblItem.*
FROM tblItemCategory LEFT JOIN tblItem ON tblItemCategory.ItemID = tblItem.ItemID
ORDER BY tblItemCategory.CategoryID;
If I try the same thing in Delphi, the ItemID AutoNumber field doesn't get populated, resulting in the following error:
..exception class EOleException with message 'The field 'tblItemCategory.ItemID' cannot contain a Null value because the Required property for this field is set to True. Enter a value in this field'.
..and the ItemID field is accordingly blank in the grid.
Is there a way to get Delphi/ADO to handle the behind-the-scenes two-table ItemID population as easily/neatly as Access does, without manually handling it programmatically? If not, what's the best/most elegant way to handle it programmatically?
I'd like to keep whatever solution I end up with as closely tied to the conventional TDataSet / TDataSource approach as possible, as I use a number of different kinds of databound controls, all of which will have to deal with this same data structure.
(Note: I'm using Delphi 2007 and an MSAccess 2000 format MDB file.)
Pretty much the same way. Theres's master source and master fields properties, so you just chain detail to master.
So mastersource would be Customer, Detail Source, Orders linked by CustomerID.
Dead easy to show this, but hard to explain.
Some other bloke wrote it all out though.
Master Detail Forms And Delphi
If you want to add new records to the tables via the grid, then you will have to use the BeforePost method of the underlying query in order to obtain the new key, which you then insert manually into the link table along with the category id.
I admit that I never write code with editable (and insertable) grids so my answer may need a little tweaking.

form has many rows, how to reference each row in my action?

I have many rows that I display on a page, and each row has a textbox with a user entered value in it.
When the user submits the form, I have take each row's textbox value, and do some db work on them.
The db work is performing updates, so I have to be able to reference the ID of the row also (which I have to store somewhere??)
what are my options here?
You need to set the name of the input(s) correctly. The Action won't see which "row" is associated with the value, nor will it see the id. It will only see the name.
Whilst this method isn't exactly 'nice', it would work for what I believe you may want.

Complex model binding

I have a Client dto that contains a bunch of fields and also contains a List.
Now, I can bind to it quite easy, and it will display the Client with all of his addresses. The thing is that the user can delete and add addresses dynamically.
I thought about adding forms surrounding each address but then I end up with inner forms and I know browsers don't play well with that.
Then I thought about using javascript but if an address is removed, I have to go over all addresses and change their indexes (addresses[0].City )because I noticed that if the indexes are not in order, and the action takes a ClientForm as a parameter, then only the addresses that have consecutive indexes and they start at 0 - will get in the ClientForm.Addresses list.
Any other solutions that are easy to implement ? Am I missing something ?
If you place a submit button with a different name on each of the addresses but no form tags, your outer form can check for the existence of a specific button and redirect to the right action (e.g edit address number 1, delete address 3 etc)
If you are using jquery validation one caveat is that the type of all the "child" submit buttons must be set to "cancel" so on pressing them validation does not occur.
HTH,
Dan
For each address on your page provide a hidden form field called addresses.Index, this will take an integer value. The ASP.NET MVC model binder (in version 2 and above) will receive the multiple values of the addresses.Index form field, and use the integer values to determine which addresses[index].property field values logically belong together.
E.g.
addresses.Index = 0
addresses.Index = 3
Would prompt the model binder to go looking for...
addresses[0].City
addresses[0].Street
addresses[3].City
addresses[3].Street
...and populate an ICollection<Address> in your controller action with two elements.
Of course if you can delete and insert these records on your page, your Javascript will need to track the next index to use in a global variable, to avoid re-using the same index for multiple rows (i.e. don't just rely on the length of a table that holds the address elements, etc).
This solution described in further detail by Phil Haack here.

Showing many tables in many dropdown lists. c#, asp.net-mvc, linq2sql

I want to use an example to explain what I want.
Assume I've following DB design:
Item (id, name, categoryID);
Category (id, name);
When user wants to create an Item (fill in form), I'll give a list of categories in a dropdownlist, and when user chooses one of the categories ASP.NET MVC will automatically bind categoryID, to the selected one. I need to present same dropdown list when editing the item with correct selected one.
Question:
But my DB is very big, and it requires around 30-40 (maybe even more) category-like tables, that contain just "id" and "name", and all tables need to be shown in dropdown list while creating some other object, and also needs to be presented while editing the object. Definitely above schema doesn't work, because it's tedious to write same logic 100 times with just different table names. (I'm using Linq2SQL)
Currently my solution is:
Make a view that's based in all such tables and in application I just call a function that construction dropdownlist from that single view. But it's still tedious to change view definition everytime I add a new table.
Do you guys think of a better solution for this tedious work, possibly using reflection or some other tecnologies.
It is not a problem "Definitely above schema doesn't work, because it's tedious to write same logic 100 times with just different table names."
If I were you, I will mark an addition interface on these class using "partial class" feature.
Then, I will write few extension method for the partial class.
If anyone interested in the solution:
I've used reflection to solve this problem.
I use reflection over DataContext to get the Table (by string name), and get its fields and construct the optionlist.

Resources