struts2 ajax传值

<table id='data-table' class='data-table table table-bordered table-striped table-hover' style='margin-bottom:0;'>
                        
</table>
<script>
    $(document).ready(function(){
    //表格
        var table = setDataTable($(".data-table"));
    });     
    this.setDataTable = function(selector) {
        return selector.DataTable({
            aLengthMenu: [2, 5]
            ,iDisplayLength:2
            ,sDom: "<'row-fluid'<'span6'l><'span6 text-right'f>r>t<'row-fluid'<'span6'i><'span6 text-right'p>>"
            ,sPaginationType: "bootstrap"
            ,oLanguage: {
                sProcessing:   "处理中...",
                sLengthMenu:   "显示_MENU_ 项结果",
                sZeroRecords:  "没有匹配结果",
                sInfo:         "显示第_START_ 至_END_ 项结果,共_TOTAL_ 项",
                sInfoEmpty:    "显示第0 至0 项结果,共0 项",
                sInfoFiltered: "(由_MAX_项结果过滤)",
                sSearch:       "搜索:",
                oPaginate: {"sNext": "下一页","sPrevious": "上一页"}
            }
            ,sAjaxSource: "admin_toTable?type=1"//1、action后传参数
            ,bServerSide:true
            ,bFilter: true //过滤功能
            //,searching: false
            ,fnServerData : function(sSource, aoData, fnCallback){
                    
                 $.ajax( {    
                        "type": "get",     
                        "contentType": "application/json",    
                        "url": sSource,     
                        "dataType": "json",    
                        "data": { aoData: JSON.stringify(aoData),type:2 }, // 2、以json格式传递参数  
                        "success": function(resp) {    
                            fnCallback(resp);   
                            //alert(resp.aaData);
                        }    
                    });    
            }
            ,aoColumns: [  
                           { "mData": "id",'title':'ID',"bVisible":false }
                          ,{ "mData": "username",'title':'登录名 '}//登录名 
                          ,{ "mData": "name",'title':'姓名 ' }//姓名  
                          ,{ "mData": "gender",'title':'性别 ',
                             "mRender":function(data,type,row){
                                 data=data==0?"":"";
                              return"<label>"+data+"</label>";
                          }}//性别  
                          ,{ "mData": "addresscode",'title':'所在地  '}//所在地  
                          ,{ "mData": "idcard",'title':'身份证  '}//身份证                          
                          ,{ "mData": "tel",'title':'电话'}//电话
                          ,{ "mData": "email",'title':'电子邮箱'}//电子邮箱
                       
                      ] 
        });
    };
</script>

同时,在action中定义type,在调用的函数处获取。

 1  private String type;
 2 public String getType() {
 3         return type;
 4     }
 5 
 6     public void setType(String type) {
 7         this.type = type;
 8     }
 9 
10 /**
11      * 显示表格信息
12      * @return
13      */
14     public String toTable(){
15         try {
16             HttpServletRequest request = ServletActionContext.getRequest();
17             JSONArray jsonParam = JSONArray.fromObject(request.getParameter("aoData"));
18             
         //也可以通过request获取参数         
        /**
          String type = request.getParameter("type").toString();
        **/
19 for(int i=0;i<jsonParam.size();i++) //从传递参数里面选出待用的参数 20 { 21 JSONObject obj=(JSONObject)jsonParam.get(i); 22 23 24 25 26 if(obj.get("name").equals("iDisplayStart")) { 27 iDisplayStart=obj.get("value").toString(); 28 29 } 30 if(obj.get("name").equals("iDisplayLength")) { 31 iDisplayLength=obj.get("value").toString(); 32 33 } 34 if(obj.get("name").toString().contains("sSearch_")) { 35 36 System.out.println("name:"+obj.get("name")+";value:"+obj.get("value")); 37 } 38 39 } 40 41 long count = service.getUserCount("1"); 42 System.out.println(count); 43 List<User> ls = service.searchPageUser(iDisplayStart,iDisplayLength,type); 44 JSONArray arr = JSONArray.fromObject(ls); 45 resultJson.put("aaData", arr); 46 resultJson.put("iTotalRecords", count); 47 resultJson.put("iTotalDisplayRecords", count); 48 } catch (Exception e1) { 49 e1.printStackTrace(); 50 } 51 52 return "toTable"; 53 }

 

posted @ 2015-08-14 11:05  Youngly  阅读(1925)  评论(0编辑  收藏  举报