I've already searched on Stackoverflow with no luck;
I want to make one filter form in which user enters data and according to that data, I want to show results in Struts2 jQGrid.
By default when page loads, Struts2 jQGrid already have action url having all data.
I tried setting target of form and <sj:a>
, but it didnt work.
See code:
This is my Grid:
<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>
My Form:
<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>
and ajax reloadTopics function:
$.subscribe("reloadMyGrid", function() {
//alert("df")
$("#filterForm").submit();
$("#filterGrid").trigger("reloadGrid");
return false;
});
Actually I have seen such style in another stackoverflow question (here) and i dont know what to write in above function.
Thank You
EDIT #1
I ve modified the code as:
<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">
The form gets submitted,json data also received,but grid not showing anything,even by reloading
EDIT #2
After modifying my complete code is:
<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>
and the 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;
}
}
and ive also written
$.subscribe("reloadGrid", function(){
$("filterGrid").trigger("reloadGrid");alert("DF");
});
Now whats problem i couldnt understand... Plz provide me solution..plz