I have a form along with a add new row button. Each row consist of different fields. Now I have already defined an ArrayList of the above fields in the command bean. My problem is that how can I dynamically take in new values and bind it to my bean during the runtime. I want to add a dynamically generated list and post it back to save it in the database. How to achieve this?
For example I have form with three text areas and there is an add button. Now when I click the add button there is a dynamic generation of the three text fields on the front end. and now I have to bind it to my bean where I have declared and arraylist for this three text areas. Now how should I achieve this. My ArryList is as follows..
private ArrayList<CommText> commText;
and the class CommText has these three variables
class CommText
{
private String text1;
private String text2;
private String text3;
//Getters and Setters...
}
Try using a javascript Ajax framework such as JQuery to post this to your Controller, which can in turn update your collection.
Which version of spring-mvc / webflow are you using?
Related
I have a Model object with a List collection of sub objects. I need to load the form fields for the sub object to the page, and provide a link to dynamically add form fields for a second object. So in other words, multiple sub objects can be created via one form and one post back.
My first thought was just to put the form fields in a partial view and load the view via Ajax.ActionLink. This worked but the problem comes in when trying to uniquely identify each object in the collection and bind the collection of objects on postback. For this it seems the correct usage would be to use an #Html.EditorFor() helper, but I don't know how to call that via Ajax to dynamically add the object's editor template to the page when the link is clicked.
It sounds like you were on the right track with regards to a partial view, you'll just need to sort out the name property of your inputs so you can correctly bind to a collection of objects.
Have a look at this article by Phil Haack explaining how binding to lists works, or google for something more up to date with razor syntax.
I need to inject a new behaviour for every dropDownList control of my web app (asp.net/C#).
Each time a new value is selected in the dropdown a web method (using Ajax/JSON) is invoked.
Since I have "normal" controls of type DropDownList and custom controls using DropDownList within them, I planned to create a class inheriting from DropDownList class with the new behaviour and then switch all the currrent DropDownList instances to this one.
In this way all my normal and custom drop downs would automatically get the new feature.
However the custom control has already an impementation for the "onselectedindexchanged" event, where some other functions are implemented.
Since I would use the same event for the new (parent) class, it would be overwritten in the custom control.
Of course I can write in the custom control the same code as in the parent, but is there a way to avoid duplicate code?
Thanks,
Luca
Of course I can write in the custom
control the same code as in the
parent, but is there a way to avoid
duplicate code?
C#:
public void doSomething() {
base.doSomething(); //parent class
//My stuff here
}
Java
public void doSomething() {
parent.doSomething(); //parent class
//My stuff here
}
My question is quite basic and to do with validating an object in your view model using enterprise library validation application block 5.0.
It seems that when a user enters invalid data, whilst the UI displays the error template for a control, the property in the view model that the control is bound to does not get updated.
This is a problem as I wanted to call code such as this
ValidatorFactory factory = EnterpriseLibraryContainer.Current.GetInstance<ValidatorFactory>();
myValidator = factory.CreateValidator<Customer>();
ValidationResults results = myValidator.Validate(this.CustomerProperty);
if (!results.IsValid)
{
// etc
}
in my save button command in the view model to check that the data is valid.
Example of problem I'm having.
e.g.
If I have a textbox bound to a string property in the vm with a string length validator (min length 1, max 10) then the following could happen;
Enter text of 'ABC'.
UI does not show error.
View model property updated to 'ABC'.
Delete contents of textbox (so now invalid).
UI now shows error (good).
But view model property is now out of sync with UI. Property in the view model is still set to ABC.
Save validation will still pass as the view model data is still valid.
How should I be doing this?
To solve this I ended up removing the validationRule (as per the example in the EntLib 5 hands-on labs documentation) and implemented IDataErrorInfo in my Customer class.
I then changed my XAML and in my textbox binding added
ValidatesOnDataErrors=True
This then validates my object as I'd expect. i.e. Invalid data is propagated to the view model property which I can then validate on my save command.
As a follow on to an answered question on Adding Components Dynamically in JSF 2.0 (see link below), I like the approach of using a dataTable, but what about Removing one of the added components?
How to dynamically add JSF components
Based on the code snippet in the other question you linked, you need to do the following changes:
Add a column with a delete button to the table.
<h:column><h:commandButton value="delete" action="#{bean.delete}" /></h:column>
Add a DataModel<Item> property to the bean and wrap the list of items in it so that you will be able to obtain the table row where the button was clicked.
private DataModel<Item> model = new ListDataModel<Item>(items);
(don't forget the getter, note that you can also instantiate this in bean constructor or postconstruct)
Use this in the datatable instead.
<h:dataTable value="#{bean.model}" var="item">
Add a delete method to the bean.
public void delete() {
items.remove(model.getRowData());
}
See also:
Benefits and pitfalls of #ViewScoped - contains JSF 2.0 CRUD table example
Some of the forms represent parent objects and I want most field disabled in each of these forms, if they're for a 'parent' object. Is there an easy way to do this?
The easiest way is to give the fields a particular semantic class, and then disable all the ones with that class using jQuery. Assuming the forms are all contained in some larger container with an id of formgroup and you use dependent-field as the class name, you can use something like:
// Set the 'disabled' attribute on every dependent field in this group of forms.
$('#formgroup').find('.dependent-field').attr('disabled', true);