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 }

 

转载于:https://www.cnblogs.com/Youngly/p/4556229.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值