我已經在Stackoverflow上搜索沒有運氣;如何使用表單提交struts2網格
我想做一個用戶輸入數據的過濾器表單,根據這些數據我想在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"
onSuccessTopics="reloadGrid" />
<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">
的形式被提交,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給我提供solution..plz
你見過在Struts2的,jQuery插件展示柵格部篩選網格每一列應用過濾器? http://struts.jgeppert.com/struts2-jquery-showcase/index.action – 2013-03-19 15:23:53
謝謝安德烈先生,我從中得到了很多幫助。 但是又出現了問題。 我已修改的代碼爲: 表單獲取提交,json數據也收到,但網格沒有顯示任何東西,即使通過重新加載 –
Jaydeep
2013-03-19 16:28:50
你在哪裏定義'%{itemsUrl}'和'%{filterUrl}'?如果它們存在,請發佈兩個' '。如果他們不這樣做,那麼我們已經找出了(其中一個)問題。 –
2013-03-19 16:49:19