0

我已经在 Stackoverflow 上搜索过,但没有运气;

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

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

我尝试设置 form 和 的目标<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

4

1 回答 1

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>

然后在每一列上应用过滤器,这将非常方便地过滤您的网格

于 2013-03-20T10:38:02.757 回答