[英]Adding, editing, deleting rows of the grid using Struts2 jQuery Grid plugin
我正在使用struts2-jquery-grid-3.7.0插件使用Struts jQuery网格尝试CRUD操作,如展示 Grid(可编辑/多选)所示。
这是形式:
<s:form namespace="/admin_side" action="Test" validate="true" id="dataForm" name="dataForm">
<s:url id="remoteurl" action="TestGrid" namespace="/admin_side"/>
<s:url id="editurl" action="EditTest"/>
<sjg:grid
id="gridmultitable"
caption="Example (Editable/Multiselect)"
dataType="json"
href="%{remoteurl}"
pager="true"
navigator="true"
navigatorSearchOptions="{sopt:['eq','ne','lt','gt']}"
navigatorEdit="true"
navigatorView="true"
navigatorAddOptions="{height:280, width:500, reloadAfterSubmit:true}"
navigatorEditOptions="{height:280, width:500, reloadAfterSubmit:false}"
navigatorViewOptions="{height:280, width:500}"
navigatorDelete="true"
navigatorDeleteOptions="{height:280, width:500,reloadAfterSubmit:true}"
gridModel="gridModel"
rowList="5,10,15"
rowNum="5"
rownumbers="true"
editurl="%{editurl}"
editinline="true"
multiselect="true"
onSelectRowTopics="rowselect">
<sjg:gridColumn name="countryId" index="countryId" title="Id" formatter="integer" editable="false" dataType="Long" sortable="true" search="true" sorttype="integer" searchoptions="{sopt:['eq','ne','lt','gt']}"/>
<sjg:gridColumn name="countryName" index="countryName" title="Country Name" editable="true" sortable="true" search="true" sorttype="text"/>
<sjg:gridColumn name="countryCode" index="countryCode" title="Country Code" sortable="true" search="true" editable="true" sorttype="text"/>
</sjg:grid>
</s:form>
映射相应动作类中的方法,同时执行添加,删除和编辑行等操作。
@Namespace("/admin_side")
@ResultPath("/WEB-INF/content")
@ParentPackage(value = "json-package")
@InterceptorRefs(
@InterceptorRef(value = "store", params = {"operationMode", "AUTOMATIC"}))
public final class TestAction extends ActionSupport implements Serializable, ModelDriven<Country>
{
@Autowired
private final transient CountryService countryService=null;
private static final long serialVersionUID = 1L;
private Country entity=new Country();
private List<Country> gridModel=new ArrayList<Country>();
private String oper; //Getter and setter.
@Action(value = "EditTest",
results = {
@Result(name = ActionSupport.SUCCESS, location = "Test.jsp"),
@Result(name = ActionSupport.INPUT, location = "Test.jsp")},
interceptorRefs = {
@InterceptorRef(value = "defaultStack", params = {"validation.validateAnnotatedMethodOnly", "true", "validation.excludeMethods", "load"})})
public String edit() throws Exception {
System.out.println(entity.getCountryId()+" : "+entity.getCountryName()+" : "+entity.getCountryCode()+" : "+oper);
if(oper.equalsIgnoreCase("add")) {
//Adding a row.
}
else if(oper.equalsIgnoreCase("edit")) {
//Editing/updating a row.
}
else if(oper.equalsIgnoreCase("del")) {
//Deleting a row.
}
return ActionSupport.SUCCESS;
}
@Override
public Country getModel() {
return entity;
}
@Action(value = "Test",
results = {
@Result(name = ActionSupport.SUCCESS, location = "Test.jsp"),
@Result(name = ActionSupport.INPUT, location = "Test.jsp")},
interceptorRefs = {
@InterceptorRef(value = "defaultStack", params = {"validation.validateAnnotatedMethodOnly", "true", "validation.excludeMethods", "load"})})
public String load() throws Exception {
//This method is required to return an initial view on page load. Nothing to see here. Leave it empty.
return ActionSupport.SUCCESS;
}
}
删除时,ID(在本例中为countryId
)始终为null
。
编辑时, countryId
为null,因为我将editable="false"
设置为网格的相应列。 当它设置为true
,将检索它,但由于countryId
是数据库中的主键,因此不应对其进行编辑。
删除和编辑时如何获取此ID(编辑时,不应编辑)?
这与ModelDriven
不再特别相关。
编辑:
在编辑和删除时,在动作类中使用String类型的id
字段,
private String id;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
被初始化为所讨论的网格的行ID(所选行)。
删除多行时,会将其初始化为由选定ID组成的逗号分隔字符串。
基于网格的行ID执行操作是不可靠的。 它们应该基于数据库中的主键值来完成。 这可能吗?
默认情况下,网格使用实体的id
字段(如果有)作为行的唯一键。 但是,如果您的实体没有此类名称,则可以使用网格列标记的key
属性将列名称指定为应用编辑或删除操作时用于传递给操作的值的键。 例如
<sjg:gridColumn name="countryId"
index="countryId"
title="Id"
formatter="integer"
editable="false"
dataType="Long"
key="true"
sortable="true"
search="true"
sorttype="integer"
searchoptions="{sopt:['eq','ne','lt','gt']}"/>
countryId
将用作表中行的键值,并且将在操作期间填充此值。
刚放好
loadonce="true"
和搜索操作工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.