2 yljmonkey yljmonkey 于 2015.06.10 19:46 提问

jquery 向表格中添加的新行,提交表单添加的新行数据丢失
import java.util.List;
import org.apache.struts.action.ActionForm;
import erange.deanfu.service.vo.PermissionBean;
import erange.tools.AutoArrayList;
public class PermissionForm extends ActionForm{
private static final long serialVersionUID = -7967103081219242369L;
private List perm = new AutoArrayList(PermissionBean.class);
@SuppressWarnings("unchecked")
public void setPerm(List perms) {
    this.perm.clear();
    this.perm.addAll(perms);
}

public List getPerm() {
    return this.perm;
}
}

自定义ActionForm

import java.util.ArrayList;
public class AutoArrayList extends ArrayList {
private static final long serialVersionUID = -8293504731897767974L;
private Class itemClass;
public AutoArrayList(Class itemClass) {
    this.itemClass = itemClass;
}
@SuppressWarnings("unchecked")
public Object get(int index) {  
    try {  
        while (index >= size()) {  
            add(itemClass.newInstance());  
        }  
    } catch (Exception e) {  
    }  
    return super.get(index);  
} 
}

自定义ArrayList

<action path="/permManager" scope="request"
        name="permissionForm" parameter="op">
        <forward name="manager" path="/WEB-INF/jsp/manager.htm"/>
    </action>

绑定到Action

<l:iterate id="perm" property="perm" name="permissionForm">
    <tr>
        <td>
            <h:text property="permission_id" name="perm" indexed="true"
                                        readonly="true" style="width:50px" />
        </td>
        <td>
            <h:text property="permission_name" name="perm" indexed="true" />
        </td>
        <td>
            <h:text property="permission_desc" name="perm" indexed="true" />
        </td>
        <td>
            <h:text property="value" name="perm" indexed="true" />
        </td>
        <td>
            <a href="javascript:void(0)" name="update"><b:message key="op.update"/></a>
        </td>
        <td>
            <a href="javascript:void(0)" name="delete"><b:message key="op.delete"/></a>
        </td>
    </tr>
</l:iterate>

页面部分添加 indexed="true" ,以上用于Action批量处理数据.

ActionForm元素为基本类型时,源码应该是这样
input type="text" name="permission_id"

经过以上步骤之后,源码如下

input type="text" name="perm[0].permission_id" value="1" readonly="readonly" style="width:50px"

也就是说,如果页面中没有indexed="true"属性,Action无法获取input值.这里大概是关键位置.

然后我使用jquery向表格里插入新的行

$tr = "<tr><td><input type='text' name='perm[" + $idx + "].permission_id' readonly='true' style='width:50px'/></td>";
    $tr += "<td><input type='text' name='perm[" + $idx + "].permission_name'/></td>";
    $tr += "<td><input type='text' name='perm[" + $idx + "].permission_desc'/></td>";
    $tr += "<td><input type='text' name='perm[" + $idx + "].value'/></td>";
    $tr += "<td><a href='javascript:void(0)' name='save'>保存</a></td><td><a href='javascript:void(0)' name='moveout'>去除</a></td></tr>";

页面点击action.submit()时,除了新增行的数据丢失,其他原有数据都可以从action中获取. 求解决办法.

********自己解决了.*********

1个回答

u012216727
u012216727   Ds   Rxr 2015.06.10 20:02

不知道你在说什么,你贴代码能不能贴关键的出来

yljmonkey
yljmonkey 您看您能看懂了么?
2 年多之前 回复
Csdn user default icon
上传中...
上传图片
插入图片