【问题标题】:Adding, editing, deleting rows of the grid using Struts2 jQuery Grid plugin使用 Struts2 jQuery Grid 插件添加、编辑、删除网格行
【发布时间】:2014-03-24 13:01:34
【问题描述】:

我正在尝试使用 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 操作类中的 String 类型字段,例如,

private String id;

public String getId() {
    return id;
}

public void setId(String id) {
    this.id = id;
}

初始化为相关网格的(选定行的)行 ID。

在删除多行时,初始化为一个逗号分隔的字符串,由选定的ID组成。

根据网格的行 ID 执行操作是不可靠的。它们应该基于数据库中的主键值来完成。这可能吗?

【问题讨论】:

    标签: java jquery jsp struts2 struts2-jquery-grid


    【解决方案1】:

    默认情况下,网格使用实体的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 将用作表中行的键值,该值将在操作期间填充。

    【讨论】:

    • 虽然这完全正确,但我们不能在删除多行时拥有List&lt;Long&gt; 吗?相反,它在删除多行时将操作类中的 id 字段初始化为逗号分隔的 id 字符串。例如,如果我将 3 行标记为删除,其 id 为 1、2 和 3,则操作类中 String 类型的 id 字段将被初始化为类似1,2,3 的字符串。如果 id 字段的类型是LongList&lt;Long&gt; 那么,它将把这些id 作为一个整数,如123。如果 id 的类型是List&lt;String&gt;,那么它将再次被初始化为一个逗号分隔的字符串,如1,2,3
    【解决方案2】:

    随便放

    loadonce="true"
    

    和搜索操作工作。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-03-26
      • 1970-01-01
      • 1970-01-01
      • 2014-09-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多