我正在尝试使用struts2-jquery-grid-3.7.0插件使用Struts jQuery网格进行CRUD操作,如showcase网格(可编辑/多选)所示。
这是表格:
<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
为空,因为我将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执行操作是不可靠的。它们应该基于数据库中的主键值来完成。这可能吗?
答案 0 :(得分:5)
默认情况下,网格使用实体的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
将用作表格中行的键值,此值将在操作期间填充。
答案 1 :(得分:0)
刚刚放
loadonce="true"
和搜索操作工作。