How to use struts tag iterator and OGNL to realize multiple rows selection - tags

I would like to do multiple rows selection. Rows are display through strut2 tag s:iterator, how can I get the selection information, which should contains a list of selected "id"
<s:form action='Selection'>
<s:iterator value="transInfos">
<input type='hidden' name=id value='<s:property value="id" />' />
<s:checkbox name="selected"/>
<s:property value="name" />
</s:iterator>
<s:submit value="Selection" />
</s:form>

One option which seems to me is to create a hidden field in your form like
<s:form action="selection">
<input type='hidden' name="selectedId" value=""/>
</s:form>
you can add a on-click event to your check-box and if it get checked you can add the value t a variable and set in the hidden field,each new addition should be added as new values in comma separated way like in end hidden field should be like
<input type='hidden' name="selectedId" value="1,2,3,4"/>
moment you submit the form you can parse the form value and can split it based on the separator ","
other option is to name the check-box with same name so moment it will get submitted the values of the checked one will be submitted as a collection, choice is all yours and you need to decide which way to go

I'm glad I can answer this question myself.
The answer is quite simple.
<s:form action="..." >
<s:iterator value="transInfos">
<input type="checkbox" name="transIds" value='<s:property value="transID" />'/>
</s:iterator>
<s:submit value="Select"/>
</s:form>
the value of checkbox is what you want to pass to the action, all the selected checkboxes will pass their values as a list to the action.

Related

Submitting the value in the property tag to the action form

The JSP:
<s:form action = "addfriend">
<s:property value="Username" />
<s:submit value="Add friend" />
</s:form>
Does this code submit the value in the <s:property> tag to the action form?
No, <s:property> tag doesn't generate input fields. Merely description of the <s:property> tag you can find here. It's used to print the value from the value stack to JSP output. To submit the value to the action the form needs to have a tag which generate a HTML <input> tag or <textarea> tag. That's what the <s:textfield> tag provides.
<s:textfield name="Username" value="%{Username}"/>
Note, there are many other tags that generate input fields, you can see the output generated in HTML browser source window.
If you need to send a value that is displayed through s:property, simply add an s:hidden field to it:
<s:form action = "addfriend">
<s:hidden name = "Username" />
<s:property value = "Username" />
<s:submit value = "Add friend" />
</s:form>
Remember, if a tag doesn't have the name attribute, it won't be posted to the Action.
Also avoid variables starting with an uppercase letter: username would be mapped to setUsername and getUsername, but Username could create problems, and is not standard.
As already pointed by #Roman, here's the code :
You can do :
<s:form action="addfriend">
<s:textfield name="Username"/>
<s:submit value="Add friend" />
</s:form>

zend framework multiple checkboxes should be inserted in database after i checked one or more of them

I have multiple checkboxes in zend ,I want that when i check them they are inserted into database .how to use that as an array and use for each to break them .I want to do this in my model insert them in in database.
You can define checkbox names as array:
<form action="" method="post">
<input type="checkbox" name="check[]" value="1" /> option one
<input type="checkbox" name="check[]" value="2" /> option two
<input type="checkbox" name="check[]" value="3" /> option three
</form>
then, when you submit form, you should get your $_POST[check] variable filled in with ids of checked options. You can then foreach through this to insert into your table.
You can get the array on your controller.
$chkArr=$this->_getParam('chkboxName');
After getting this you can pass this array to your model.
You can check the count etc. in model or controller depends on your requirement.

AngularJS form inputs: how to set default values based on $scope but bind to new form model

Building a CMS with node, mongoDB & angularjs. I have an edit form with inputs that I want to bind to the $scope so their values are populated with existing data. Ex:
<form ng-submit="editArticle(article._id, article)" class="edit-form">
<label for="hed">Hed: <input type="text" name="hed" ng-model="article.hed" value="{{article.hed}}" /></label>
<label for="dek">Dek: <input type="text" name="dek" ng-model="article.dek" placeholder="{{article.dek}}" /></label>
<input type="submit" value="Save Changes"></input>
</form>
This appropriately fills in the input values with the current data from my mongo database. However, I would like to bind to a new form scope, so it doesn't try to pass in every value in the article scope, just the new scope within the form. (Article scope currently consists of every field in the article document in Mongo. If I pass all of this data in, I get an error about invalid schema because it is trying to pass in the __v field as well.)
So ideally I want to do this:
<form ng-submit="editArticle(article._id, form)" class="edit-form">
<label for="hed">Hed: <input type="text" name="hed" ng-model="form.hed" value="{{article.hed}}" /></label>
<label for="dek">Dek: <input type="text" name="dek" ng-model="form.dek" placeholder="{{article.dek}}" /></label>
<input type="submit" value="Save Changes"></input>
</form>
So on submit, it only passes form.hed and form.dek to my database. However, now these input fields are blank because they're not bound to the original article data. The user would have to re-fill in each of the fields with the original values. Is there a way to bind to multiple models orrrr bind to a current model, but on submit only send the updated form data?
EDIT:
It looks like I have to add the ng-change attribute to each field, to populate the input fields with the original scope, then on change, set the form scope equal to the changed value. Eg.
<form ng-submit="editArticle(article._id, form)" class="edit-form">
<label for="hed">Hed: <input type="text" name="hed" ng-change(form.article = article.hed) ng-model="article.hed" /></label>
<label for="dek">Dek: <input type="text" name="dek" ng-change(form.dek = article.dek) ng-model="article.dek" placeholder="{{article.dek}}" /></label>
<input type="submit" value="Save Changes"></input>
</form>
Not sure how thrilled I am about this solution, but it works. If anyone knows a more efficient way to accomplish this please let me know!
Do a copy of the original object (or part of it) upon receiving it from DB and bind your from to it.
Coping objects before edit is a very good pattern since it allows you to easily provide additional functionality like revert edits and disable save buttons if no edit was made.

How to handle Zend SubForms when the number of each is unknown

I have a 'customer' form which has a section called 'contacts'. To start with this contacts section will contain the following elements..
<input type="text" name="contacts[0][fname]" />
<input type="text" name="contacts[0][sname]" />
But the user may want to add another contact which will duplicate the elements with javascript to produce the following:
<input type="text" name="contacts[0][fname]" />
<input type="text" name="contacts[0][sname]" />
<br />
<input type="text" name="contacts[1][fname]" />
<input type="text" name="contacts[1][sname]" />
I know how to produce the first set of elements, however if the form gets submitted and there are errors, how can i ensure that the correct number of 'contacts' elements get rendered?
Ive never had to do this with Zend_Form but i have done it with Symfony 1.4's sfForm which has a similar API and theory of operation. Based on that the basic process is:
In the parent forms constructor intialize some default number of subforms. Youll want to separate out the logic for actually creating and embedding n subforms into a separate method(s). Ill refer to this as the method emebedContacts($count = 1)
Override the isValid and setDefaults methods on the parent form so that they detect the number of subforms in the $data arguments passed to them and then call embedContacts before calling parent::isValid() or parent::setDefaults().
Hope that helps.

Submit a JSF form using GET

How do I submit a form to the same page and use GET parameters?
JSF page contents:
<f:metadata>
<f:viewParam name="item1" value="#{bean.item1}"/>
<f:viewParam name="item2" value="#{bean.item2}"/>
</f:metadata>
...
<h:form>
<h:inputText value="#{bean.item1}"/>
<h:inputText value="#{bean.item2}"/>
<h:button value="Submit" >
<f:param name="item1" value="#{bean.item1}"/>
<f:param name="item2" value="#{bean.item2}"/>
</h:button>
</h:form>
If I request the page: form.jsf?item1=foo&item2=bar, it will populate the text fields, but the form submission to itself doesn't seem to work.
Replace <h:button> by
<h:commandButton value="Submit" action="form?faces-redirect=true&includeViewParams=true"/>
It effectively fires a PRG (Post-Redirect-Get) which will include the <f:viewParam> params in the query string. Noted should be that the target page must have exactly same <f:viewParam>.
Another solution is to use a plain HTML <form> instead of <h:form>, give the input elements an id matching the parameter name and use a plain <input type="submit">. You can of course also use plain HTML <input type="text"> here.
<form>
<h:inputText id="item1" value="#{bean.item1}"/>
<h:inputText id="item2" value="#{bean.item2}"/>
<input type="submit" value="Submit" />
</form>
You should still keep the <f:viewParam> in both sides. You only need to realize that conversion/validation couldn't be done in this form, they have to be performed via the <f:viewParam> on the target page.
See also:
What can <f:metadata>, <f:viewParam> and <f:viewAction> be used for?
You could also register a NavigationHandler that handles a keyword like 'self' and redirects to the the current view and adds the necessary query parameters.