使用 Spring 动态表单的更好方法? [英] better way for dynamic forms with Spring?
问题描述
我想知道在使用 SpringMVC 和 Spring 表单时是否有更简单/更好的方法来处理动态表单(通过 js 向 dom 添加表单项)?
What I wonder is if there's a easier/better way to handle dynamic forms (adding form items to the dom via js) when using SpringMVC and Spring forms?
具有包含许多 LineItem 的 Invoice 对象的图像.
Imaging having an Invoice object that have many LineItems.
public class Invocie {
private List LineItems;
public Invoice() {
lineItems = ListUtils.lazyList(new ArrayList<LineItem>(), FactoryUtils.instantiateFactory(LineItem.class));
}
}
显示属于我当前使用的发票的项目
To show the items belonging to an Invoice I currently use
<forEach items="${invoice.lineItems}" varStatus="i">
<form:input path="lineItems[${i.index}].productName" />
</c:forEach>
要添加 LineItems,我有一些 js 可以计算新索引并将其添加到 DOM.删除 LineItem 时,我目前必须对所有索引重新编号,这是我想避免的部分,这可能吗?
To add LineItems I have some js that calculates the new index and adds that to the DOM. When deleting a LineItem i currently have to renumber all the indexes and that's the part I'd like to avoid, is it possible?
推荐答案
我已经实现了一个教程,可以帮助您在客户端使用 jQuery 和 Springs AutoPopulating 列表来帮助您解决这个问题.
I've implemented a tutorial that might help you solve this using jQuery on the client side and Springs AutoPopulating list for you form backing objects.
http://eggsylife.co.uk/2009/11/30/spring-forms-dynamic-lists-and-ajax/
编辑来自 Webarchive 的链接 https://web.archive.org/web/20160729163958/http://eggsylife.co.uk/2009/11/30/spring-forms-dynamic-lists-and-ajax/
EDIT Link from Webarchive https://web.archive.org/web/20160729163958/http://eggsylife.co.uk/2009/11/30/spring-forms-dynamic-lists-and-ajax/
这篇关于使用 Spring 动态表单的更好方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!