gpt4 book ai didi

java - 使用 Struts2 jQuery Grid 插件添加、编辑、删除网格行

转载 作者:塔克拉玛干 更新时间:2023-11-02 07:47:32 26 4
gpt4 key购买 nike

我正在尝试使用 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>

映射对应的action类中的方法,在执行增删改行等操作时调用。

@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 不再特别相关。


编辑:

在编辑和删除时,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 将用作表中行的键值,该值将在操作期间填充。

关于java - 使用 Struts2 jQuery Grid 插件添加、编辑、删除网格行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21961115/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com