struts2 jquery grid-beginner简要示例

时间:2014-04-29 10:16:22

标签: struts2-jquery struts2-jquery-grid

我想创建一个包含文本字段和可编辑网格表的表单页面,其中动态输入的文本字段中的数据应该添加到表单中的网格表中,我们可以编辑网格表中的数据。我正在获取静态输入的数据并没有保存在网格中。我正在附加我现在只是建议我做什么的类,因为我是struts2的新手。

我的jsp页面是:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
 pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<%@ taglib prefix="sj" uri="/struts-jquery-tags" %>   
<%@taglib prefix="sjg" uri="/struts-jquery-grid-tags"%>

 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 <html>
 <head>
 <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
 <title>Product Master</title>
 <sj:head jqueryui="true"/>
 </head>
 <body>

 <tr><a href="javascript:update('<s:property      value="anyValueYouwantToSend"/>')">Stock</a></tr>
 <tr><a href="javascript:update('<s:property value="anyValueYouwantToSend"/>')">Substitutes</a></tr>

 <s:form action="Welcome" namespace="/">
 <s:textfield name="productCode" label="Product Code"></s:textfield>
 <s:textfield name="productName" label="Product Name"></s:textfield>
 <s:textfield name="packaging" label="Packaging"></s:textfield>
 <s:textfield name="conversion" label="Conversion"></s:textfield>
 <s:textfield name="rackNumber" label="Rack Number"></s:textfield>
 <s:textfield name="manufacturer" label="Manufacturer"></s:textfield>

 <s:submit name="btnSave" type="submit" value="Add"/>
 </s:form>
 <s:url var="hello" action="getData" method="post"></s:url>
 <sjg:grid id="gridtable"
   caption="Product Master"
   dataType="json"
   href="%{hello}"
   pager="true"
   gridModel="gridModel"
   rowList="10,15,20"
   rowNum="15"
   rownumbers="true">

<sjg:gridColumn name="productCode" index="productCode" title="ProductCode" sortable="false" value="34545"/>
<sjg:gridColumn name="productName" index="productName" title="ProductName" sortable="false" value="hgfhgh"/>
<sjg:gridColumn name="packaging" index="packaging" title="Packaging" sortable="false" value="786878"/>
<sjg:gridColumn name="conversion" index="conversion" title="Conversion" sortable="false" value="78787"/>
<sjg:gridColumn name="rackNumber" index="rackNumber" title="RackNumber" sortable="false" value="7898"/>
<sjg:gridColumn name="manufacturer" index="manufacturer" title="Manufacturer" sortable="false" value="87878"/>


</sjg:grid>

</body>
</html>

我的行动课程:

import java.`util.ArrayList`;
import java.`util.List`;
import `com.srivasishta.hms.pharmacy.domain.Productmaster`;
import `com.opensymphony.xwork2.ActionSupport`;

public class `ProductAction`  extends `ActionSupport`{

    List<`Productmaster`> list=null;

    public List<`Productmaster`> `getList`() {
        return list;
    }

    public void `setList(List<`Productmaster> list) {
        this.list = list;
    }
    public String execute()throws Exception
    {
        list=new `ArrayList<Productmaster>`();
        `Productmaster` product=new `Productmaster`();
        `Productinfo` pi=new `Productinfo`();
        `product.setProductCode`(34545);
        `product.setProductName`("hgfhgh");
        `product.setPacks`(786878);
        `product.setQuantityPack`(78787);
        `product.setPurchaseRate`(7898);
        `product.setIpSellingRate`(87878);
        `product.setOpSellingRate`(87878);
        `product.setRowNo`("hgfh");
        `product.setRackNo`("hgfhnbm");

        `product.setProductType`("hgfjkl");
        `product.setBatchNo`("hqwed");
        `product.setMrp`("herwt");
        `list.add`(product);

        `Productmaster` product1=new `Productmaster`();
        `product.setProductCode`(34545);
        `product.setProductName`("hgfhgh");
        `product.setPacks`(786878);
        `product.setQuantityPack`(78787);
        `product.setPurchaseRate`(7898);
        `product.setIpSellingRate`(87878);
        `product.setOpSellingRate`(87878);
        `product.setRowNo`("hgfh");
        `product.setRackNo`("hgfhnbm");

        `product.setProductType`("hgfjkl");
        `product.setBatchNo`("hqwed");
        `product.setMrp`("herwt");
        list.add(product1);
        `pi.setGridModel`(list);
        return "success";
    }
}

我的Productinfo.java类是:

import `java.util.List`;
import `com.srivasishta.hms.pharmacy.domain.*`;
public class `Productinfo` {
    private List<`Productmaster`> `gridModel`;
    // get how many rows we want to have into the grid – rowNum attribute in the
    // grid
    private Integer rows = 0;
    // Get the requested page. By default grid sets this to 1.
    private Integer page = 0;
    // sorting order – asc or desc
    private String sord;
    // get index row – i.e. user click to sort.
    private String `sidx`;
    // Search Field
    private String `searchField`;
    // The Search String
    private String `searchString`;
    // he Search Operation
    // ['eq','ne','lt','le','gt','ge','bw','bn','in','ni','ew','en','cn','nc']
    private String `searchOper;
    // Your Total Pages
    private Integer total = 0;
    // All Record
    private Integer records = 0;
    public void execute() {
        int to = (rows * page);
        //int from = to–rows;
        page = 1;
        rows = `gridModel.size`();
        records = `gridModel.size`();
        // calculate the total pages for the query
        total = records;
    }
    public List<`Productmaster`> `getGridModel`() {
        return `gridModel`;
    }
    public void `setGridModel`(List<`Productmaste`r> `gridModel`) {
        `this.gridModel` = `gridModel`;
        execute();
    }
    public Integer `getRows`() {
        return rows;
    }
    public void `setRows`(Integer rows) {
        this.rows = rows;
    }
    public Integer `getPage`() {
        return page;
    }
    public void `setPage`(Integer page) {
        this.page = page;
    }
    public String `getSord`() {
        return `sord`;
    }
    public void `setSord`(String sord) {
        `this.sord` = sord;
    }
    public String `getSidx`() {
        return sidx;
    }
    public void setSidx(String sidx) {
        `this.sidx` = sidx;
    }
    public String `getSearchField`() {
        return searchField;
    }
    public void `setSearchField`(String searchField) {
        `this.searchField` = searchField;
    }
    public String `getSearchString`() {
        return searchString;
    }
    public void `setSearchString`(String searchString) {
        `this.searchString` = searchString;
    }
    public String getSearchOper() {
        return searchOper;
    }
    public void `setSearchOper`(String searchOper) {
        `this.searchOper` = searchOper;
    }
    public Integer `getTotal`() {
        return total;
    }
    public void `setTotal`(Integer total) {
        this.total = total;
    }
    public Integer `getRecords`() {
        return records;
    }
    public void `setRecords`![enter image description here][1](Integer records) {
        this.records = records;
    }
    // Getters and Setters for Attributes
}

1 个答案:

答案 0 :(得分:0)

您需要一个返回json结果的操作

private List<?> gridModel; //setters and getters omitted

@Action(value = "account-summary-data-provider", 
       results = { @Result(type = "json") })

 public String dataProvider() {

 //Make some object and put it in grid
 UserObject sample1 = new UserObject ();
 sample1.setName('foo');
 UserObject sample2 = new UserObject ();
 sample2.setName('bar');

 gridModel.add(sample1);
 gridModel.add(sample2);
 return SUCCESS;

}

现在你需要在示例html中通过ajax调用它并将json转换为table

<script type="text/javascript">
$.ajax({ // ajax call starts
    url: '/youaction/adata-provider.ib', // JQuery loads serverside.php
    dataType: 'json', // Choosing a JSON datatype
    success: function(data) {
        $.each(data.gridModel, function(key, val) {
            var tr=$('<tr></tr>');
            $.each(val, function(k, v){
                $('<td>'+v+'</td>').appendTo(tr);
            });
            tr.appendTo('#sampleTable');
        });
    }
    });    

</script>

<table id="sampleTable"></table>

你需要做更多关于制作更好的桌子。

注意:如果你不想使用struts 2 jquery grid,至少考虑使用jqGrid这将为你节省生命。网络上有更多json to table工具。