如何使用struts2网格与表单提交

时间:2013-03-19 14:42:09

标签: struts2 struts2-jquery struts2-jquery-grid

我已经在Stackoverflow上搜索没有运气了;

我想创建一个用户输入数据的过滤器表单,根据该数据,我想在Struts2 jQGrid中显示结果。

默认情况下,当页面加载时,Struts2 jQGrid已经拥有包含所有数据的动作URL。

我尝试设置表单的目标和<sj:a>,但它没有用。

参见代码:

这是我的网格:

    <sjg:grid gridModel="items"
                            href="%{itemsUrl}"
                            caption="Items"
                            id="filterGrid"
                            dataType="json"
                            rownumbers="true"
                            pager="true"
                            navigator="false"
                            rowList="10, 15, 20"
                            rowNum="10"
                            viewrecords="true"
                            loadonce="true"
                            formIds="filterForm"

                            >
    <sjg:gridColumn name="item.itemname" title="Item Name"></sjg:gridColumn>
    <sjg:gridColumn name="shelves.shelfname" title="Shelf Name"></sjg:gridColumn>
    <sjg:gridColumn name="quantity" title="Available Qauntity"></sjg:gridColumn>
    <sjg:gridColumn name="item.itemprice" title="Item Price"></sjg:gridColumn>
</sjg:grid>

我的表格:

<s:form id="filterForm" action="%{filterUrl}">
    <table style="width:100%">
        <tr>
            <td colspan="4">
                <s:textfield key="global.item.list.name" name="nameContains" />
            </td>
        </tr>
        <tr>
            <td colspan="2">
                <s:textfield key="global.item.list.pricefrom" name="priceFrom" />
            </td>
            <td colspan="2">
                <s:textfield key="global.item.list.priceto" name="priceTo" />
            </td>
        </tr>
        <tr>
            <td colspan="4">
                <s:select cssStyle="width: 100%" id="SelectCategoryList"
                    multiple="true" list="categoryList" listKey="categoryId" listValue="categoryType"
                    headerKey="-1" headerValue="Select Type" key="global.add.item.type"
                    loadingText="Item Types Loading..." />
            </td>
        </tr>
        <tr>
            <td colspan="4">
                <div id="fields"></div>
            </td>
        </tr>
        <tr>
            <td colspan="4">
                <sj:a targets="filterGrid" button="true"
                    key="global.item.list.form.submit" onClickTopics="reloadMyGrid" />
        </tr>
    </table>
</s:form>

和ajax reloadTopics函数:

$.subscribe("reloadMyGrid", function() {
        //alert("df")
        $("#filterForm").submit();
        $("#filterGrid").trigger("reloadGrid");
        return false;
        });

实际上我在另一个stackoverflow问题(here)中看到了这样的风格,我不知道在上面的函数中要写什么。

谢谢

编辑#1


我将代码修改为:

<sj:a targets="filterGrid" 
      button="true" 
      key="global.item.list.form.submit" 
      formIds="filterForm" 
      onSuccess‌​Topics="reloadGrid" /> 

<sjg:grid gridModel="items" 
          href="%{itemsUrl}" 
          caption="Items" 
          id="filterGrid" 
          dataType="j‌​son" 
          rownumbers="true" 
          pager="true" 
          navigator="false" 
          rowList‌​="10, 15, 20" 
          rowNum="10" 
          viewrecords="true"> 

表单已提交,json数据也已收到,但网格未显示任何内容,即使重新加载

编辑#2

修改完整代码后:

<s:url id="filterUrl" action="ListItemFilter" />
                <s:form id="filterForm" action="%{filterUrl}">
                    <table style="width:100%">
                        <tr> 
                            <td colspan="4">
                                <s:textfield key="global.item.list.name" name="nameContains" />
                            </td>
                        </tr>
                        <tr>
                            <td colspan="2"> <s:textfield key="global.item.list.pricefrom" name="priceFrom" /> </td>
                            <td colspan="2"> <s:textfield key="global.item.list.priceto" name="priceTo" /> </td>
                        </tr>
                        <tr>
                            <td colspan="4">
                                <s:select
                                    cssStyle="width: 100%"

                                    id="SelectCategoryList"
                                    multiple="true"
                                    list = "categoryList" 
                                    listKey = "categoryId" 
                                    listValue = "categoryType" 
                                    headerKey="-1"
                                    headerValue="Select Type"
                                    key = "global.add.item.type" 
                                    loadingText="Item Types Loading..."
                                />
                            </td>
                        </tr>
                        <tr>
                            <td colspan="4"> 
                                <div id="fields"></div>
                            </td>
                        </tr>
                        <tr>
                            <td colspan="4"> <sj:a targets="filterGrid" 
                            button="true" 
                            key="global.item.list.form.submit"
                            formIds="filterForm"
                            onSuccessTopics="reloadGrid"
                             />
                        </tr>
                    </table>
                </s:form>

<s:url id="itemsUrl" action="ListItems" />      
        <div  class="gridSection">
        <sjg:grid gridModel="items"
                        href="%{itemsUrl}"
                        caption="Items"
                        id="filterGrid"
                        dataType="json"
                        rownumbers="true"
                        pager="true"
                        navigator="false"
                        rowList="10, 15, 20"
                        rowNum="10"
                        viewrecords="true"                      
                        >
                <sjg:gridColumn name="item.itemname" title="Item Name"></sjg:gridColumn>
                <sjg:gridColumn name="shelves.shelfname" title="Shelf Name"></sjg:gridColumn>
                <sjg:gridColumn name="quantity" title="Available Qauntity"></sjg:gridColumn>
                <sjg:gridColumn name="item.itemprice" title="Item Price"></sjg:gridColumn>
             </sjg:grid>
        </div>

struts.xml中:

<action name="ListItems" class="com.acty.libsys.actions.ListItemsAction">
<result name="success" type="json"></result>
</action>
<action name="ListItemFilter" class="com.acty.libsys.actions.ListItemsAction" method="filter">
    <result name="success" type="json"></result>
</action>

和行动:

public class ListItemsAction extends ActionSupport {
    private static final long serialVersionUID = 1L;

    private List<ItemCategory> categoryList;

    public List<ItemCategory> getCategoryList() {
        return categoryList;
    }

    public void setCategoryList(List<ItemCategory> categoryList) {
        this.categoryList = categoryList;
    }

    public String execute() {
        serviceProvider.openConnection();
        /*This will be available to select box of ListItem.jsp*/
        categoryList = serviceProvider.getItemCategoryMapper().selectByExample(null);
        items = serviceProvider.getShelfItemsMapper().selectItemsFromShelf();System.out.println("fidddds "+items);
        serviceProvider.closeConnection();
        return SUCCESS;
    }

    public String filter() {
        ShelfItemsExample example = new ShelfItemsExample();

        if(!("".equals(nameContains)))
            example.createCriteria().andItemnameIsLike(nameContains);
        if(priceFrom != null && priceTo != null)
            example.createCriteria().andItemPriceBetween(priceFrom, priceTo);

        System.out.println("nameContains "+nameContains + " priceFrom " + priceFrom);
        if(itemCategoryFields != null && !(itemCategoryFields.isEmpty())) {
            List<String> fieldIds = new ArrayList<String>();
            List<String> fieldValues = new ArrayList<String>();

            for(Map.Entry<String, String> entry: itemCategoryFields.entrySet()){
                fieldIds.add(entry.getKey());
                fieldValues.add(entry.getValue());
            }
            example.createCriteria().andFieldIdIn(fieldIds);

            example.setCondition(fieldValues);
        }       

        serviceProvider.openConnection();
        items = serviceProvider.getShelfItemsMapper().selectFiltered(example);
        System.out.println("fids "+items);
        serviceProvider.closeConnection();
        return SUCCESS;
    }

    public void setItems(List<ShelfItems> items) {
        this.items = items;
    }

    /* Used when filter button clicked*/
    private String nameContains;
    private BigDecimal priceFrom;
    private BigDecimal priceTo;

    private Map<String, String> itemCategoryFields;

    public String getNameContains() {
        return nameContains;
    }

    public void setNameContains(String nameContains) {
        this.nameContains = nameContains;
    }

    public BigDecimal getPriceFrom() {
        return priceFrom;
    }

    public void setPriceFrom(BigDecimal priceFrom) {
        this.priceFrom = priceFrom;
    }

    public BigDecimal getPriceTo() {
        return priceTo;
    }

    public void setPriceTo(BigDecimal priceTo) {
        this.priceTo = priceTo;
    }

    public Map<String, String> getItemCategoryFields() {
        return itemCategoryFields;
    }

    public void setItemCategoryFields(Map<String, String> itemCategoryFields) {
        this.itemCategoryFields = itemCategoryFields;
    }

    /* used when first the form loads*/
    private List<ShelfItems> items;

    public List<ShelfItems> getItems() {
        return items;
    }

    private DataServiceProviderInterface serviceProvider;

    public DataServiceProviderInterface getServiceProvider() {
        return serviceProvider;
    }

    public void setServiceProvider(DataServiceProviderInterface serviceProvider) {
        this.serviceProvider = serviceProvider;
    }
}

并且我也写了

$.subscribe("reloadGrid", function(){
   $("filterGrid").trigger("reloadGrid");alert("DF");
});

现在我无法理解的问题...... Plz为我提供解决方案..plz

1 个答案:

答案 0 :(得分:0)

您必须使用过滤器来过滤您的列,并根据您的过滤器,您的网格将填充数据。

尝试类似这样的内容

<sjg:grid gridModel="items"
                            href="%{itemsUrl}"
                            caption="Items"
                            id="filterGrid"
                            dataType="json"
                            rownumbers="true"
                            pager="true"
                            navigator="false"
                            rowList="10, 15, 20"
                            rowNum="10"
                            viewrecords="true"
                            loadonce="true"
                            formIds="filterForm"

                            filter="true"
                            filterOptions="{stringResult :true,
                            searchOnEnter : true,
                            enableClear : true}"


                            >
    <sjg:gridColumn name="item.itemname" title="Item Name"></sjg:gridColumn>
    <sjg:gridColumn name="shelves.shelfname" title="Shelf Name"></sjg:gridColumn>
    <sjg:gridColumn name="quantity" title="Available Qauntity"></sjg:gridColumn>
    <sjg:gridColumn name="item.itemprice" title="Item Price"></sjg:gridColumn>
</sjg:grid>

然后在每个用于过滤网格的列上应用过滤器,非常方便