easyui-datagrid 加载数据 方法二 ( 自定义返回值 json方式)

效果图

 html部分

<table class="my_table"> 
        <tr> 
            <td class='my_dt_label'>主叫号码:</td> 
            <td class='my_dt_field'><input type='text' id='select_callerid' class='my_input' size='30' /></td> 
            <td class='my_dt_label'>被叫号码:</td> 
            <td class='my_dt_field'><input type='text' id='select_called' class='my_input' size='30' /></td> 
            <td class='my_dt_field'>呼叫类型:</td> 
            <td class='my_dt_field'><select id="select_call_type" name="select_call_type" class="easyui-combobox" style="height:28px;width:160px;" editable=false ><{$option_select_call_type}></select></td>
            
            <td class='my_dt_field'><input type='text' id='select_time_len_start' class='my_input' style='width:50px;' size='10' />
            <td class='my_dt_label'> <= 时长 <= </td> 
            <td colspan='3'><input type='text' id='select_time_len_stop' class='my_input' style='width:50px;' size='10' />&nbsp;</td> 
        </tr>    
    </table> 
    <table> 
        <tr> 
            <td style="width:65px;">开始时间:</td>
            <td><input id="switch_date_entered" class="easyui-switchbutton"  style="width:60px;height:26px" onText="部分" offText="全部" checked /> </td>  <!-- checked -->
            <td style="padding: 0px 0px 0px 2px;"> <input id="start_date_entered" value="<{$start_date}>" placeholder="开始日期" class="my_input laydate-icon" style="height:29px;"  onclick="laydate({istime: true, format: 'YYYY-MM-DD hh:mm:ss'})" readonly ></input></td> 
            <td><span class="input-group-addon" style="height:28px;"></span></td> 
            <td><input id="stop_date_entered" value="<{$stop_date}>" placeholder="结束日期" class="my_input laydate-icon" style="height:29px;" onclick="laydate({istime: true, format: 'YYYY-MM-DD hh:mm:ss'})"  readonly  ></input>    </td> 
            <td style="width:10px;"> </td>
            <td style="padding: 0px 0px 0px 5px;"> 
                <a href='#' class='easyui-linkbutton'   iconCls='icon-select' style="height:29px;width:64px;" onclick='Select()'>查询</a> 
            </td>
            <td >&nbsp;&nbsp<label id="lbl_sum_tel_rate"> </label>
            </td > 
        </tr>
        <tr><td height='5px;'></td></tr>
    </table> 
    <table id='grid' class='easyui-datagrid' style='width:100%;height:480px'  
            title='列表' iconCls='icon-table' pagination='true' rownumbers='true' fitColumns='true' singleSelect='true' toolbar='#toolbar' >  
        <thead>  
            <tr>  
                <th field='select' width='5' align='center'></th>
                <th field='UniqueId' width='13'align='center' hidden='true' >编号</th>   
                <th field='CallerId' width='13' align='center'>主叫号码</th> 
                <th field='Called' width='13' align='center'>被叫号码</th> 
                <th field='CallType' width='10' align='center'>呼叫类型</th> 
                <th field='StartTime' width='18'align='center'>开始时间</th>   
                <th field='EndTime' width='18'align='center'>结束时间</th> 
                <th field='BillableSeconds' width='10' align='center'>计费时长</th>
                <th field='CallFee' width='10' align='center'>话费</th>
            </tr>  
        </thead>  
    </table> 
    <div id='toolbar' style='display: flex;align-items:Center; height:35px;' > 
        <a href='#' class='easyui-linkbutton' iconCls='icon-select-file' plain='true' onclick='Detail()' <{$Detail_disabled}> >详细</a>
        <div class="btn-separator"></div>
        <a href='#' class='easyui-linkbutton' iconCls='icon-printer' plain='true' onclick='Print()' <{$Print_disabled}>  >打印</a> 
        <a href='#' class='easyui-linkbutton' iconCls='icon-excel' plain='true' onclick='ToExcel()' <{$ToExcel_disabled}> >导出</a> 
        <a href='#' class='easyui-linkbutton' iconCls='icon-refresh' plain='true' onclick='Refresh()'>刷新</a> 
        <a href='#' class='easyui-linkbutton'  iconCls='icon-cancel' plain='true' onclick='parent.TabClose();'>关闭</a>
    </div>   

javascript

    //获取 查询 条件 参数
    function Select_Parameter(){
        //var switch_date_entered= "true";//$("#switch_date_entered").switchbutton("options").checked;//alert(status);  //是否启动 日期查询条件
        var switch_date_entered=$("#switch_date_entered").switchbutton("options").checked;
        var start_date_entered=$('#start_date_entered').val();   //创建时间        
        var stop_date_entered=$('#stop_date_entered').val(); 

        var callerid=$('#select_callerid').val();       
        var called=$('#select_called').val();    
        var call_type=$('#select_call_type').val();

        var time_len_start=$('#select_time_len_start').val();
        var time_len_stop=$('#select_time_len_stop').val();
        
        //alert(start_date_entered);
         
        var query={    'switch_date_entered':switch_date_entered,'start_date_entered':start_date_entered,'stop_date_entered':stop_date_entered 
                     
                    ,'callerid':callerid,'called':called,'time_len_start':time_len_start,'time_len_stop':time_len_stop,'call_type':call_type  };     
        return query;
    }

    function Select(){   
        //$.messager.progress({  title: '提示',  msg: '数据正在处理中,请稍候……',  text: ''  });
        var query=Select_Parameter(); //alert(query);//获取 查询 条件 参数 

        //方法二
        $.ajax({  
            type: "POST", //POST和GET  
            async: false,  
            url: 'Api-index.php?module=<{$module_name}>&action=Api_GridView_Select<{$get_current_user}>',    //提交url 注意url必须小写  
            data: query,  // { "SearchContent": query, "pageSize": pageSize, "pageIndex": curr }
            success: function (result) {  //alert(result);
                var data = $.parseJSON(result);//转为 Json对象    
                $('#grid').datagrid('loadData', data);  
                
                //总金额
                if( data.total != 0){    //alert(data.sum_tel_rate);
                    var lbl_html="话费总金额: " + data.sum_tel_rate + " 元";
                    $("#lbl_sum_tel_rate").html(lbl_html);
                }else{
                    $("#lbl_sum_tel_rate").html("");
                }
                //$.messager.progress('close');
                 
            }  
        }); 

    }   

php

<?php 
//-------------------------------------------------------------------------------------
    $table_name="ast_cdr";  
    $arr_result = array(); //返回值  
    $where='';//查询条件  
        
        //启动 哪个日期条件    
        if($_POST['switch_date_entered']=='true'  ){ //WriteLog($_POST['switch_date_entered']
            //$where=" and date_entered >='{$_POST['start_date_entered']}' and date_entered <='{$_POST['stop_date_entered']}' ";  
            $where=" and StartTime >='{$_POST['start_date_entered']}' and StartTime <='{$_POST['stop_date_entered']}' ";  
        } 

        //其它条件
        if($_POST['callerid']!=''){  
            //$where .=" and CallerId = '{$_POST['callerid']}' ";
            $where .=" and CallerId like '%{$_POST['callerid']}%' "; 
        } 
        if($_POST['called']!=''){  
            //$where .=" and Called = '{$_POST['called']}' ";   
            $where .=" and Called like '%{$_POST['called']}%' ";   
        }
        //时长 三种可能 --------------------------------------------------------------  
        //不填 <= 时长 <= 10  5<= 时长<= 10
        if($_POST['time_len_start']!='' && $_POST['time_len_stop']!=''){  
            $where .=" and ( {$_POST['time_len_start']} <= BillableSeconds and BillableSeconds <= {$_POST['time_len_stop']} ) ";   
        }
        //不填 <= 时长 <= 10  5<= 时长<= 10
        if($_POST['time_len_start']=='' && $_POST['time_len_stop']!=''){  
            $where .=" and ( BillableSeconds <= {$_POST['time_len_stop']} ) ";   
        }
        //不填 <= 时长 <= 10  5<= 时长<= 10
        if($_POST['time_len_start']!='' && $_POST['time_len_stop']==''){  
            $where .=" and ( {$_POST['time_len_start']} <=  BillableSeconds ) ";   
        } 
        //------------------------------------------------------------------------------
        //呼叫类型
        if($_POST['call_type']!=''&& $_POST['call_type']!='0'){     
            switch ($_POST['call_type'])
            {
                case "呼出":
                 $where .=" and ( CallType = 'outgoing' or CallType = 'outbound' ) ";  
                  break;  
                case "呼入":
                  $where .=" and ( CallType = 'incoming' or CallType = 'inbound' ) ";  
                  break;
                case "内部":
                  $where .=" and ( CallType = 'internal' or  CallType = 'inter' ) ";  
                  break;
                default:
                  //表达式的值不等于 label1 及 label2 时执行的代码;
            }
        }  

        //分页  
        $page = isset($_POST['page']) ? intval($_POST['page']) : 1;   
        $rows = isset($_POST['rows']) ? intval($_POST['rows']) : 10;   
        $offset = ($page-1)* $rows;   
    
        $sql ="SELECT COUNT(id) AS total FROM {$table_name} where 1=1 " . $where ;   //WriteLog($sql);  
        $arr_result['total'] =$db->query_count($sql);//行数 
        //WriteLog($result['total'].'-----' );  
    
        //结果集      
        $items = array();
        $order_by=" order by StartTime desc ";//工号排序 office,user_num
        $sql = "select * from {$table_name} where 1=1 ".$where .$order_by." limit $offset,$rows ";   

        //导出
        if($_GET['target']=="ToExcel" || $_GET['target']=="Print" ){
            $arr_result['where'] = $where;      //将条件 传出
            $arr_result['order_by'] = $order_by;      //将条件 传出
            //WriteLog($where);  
            //WriteLog($order_by);  
            echo json_encode($arr_result);
            exit(0);
        }
        //WriteLog("target ".$_GET['target']);  

        $result_rows=$db->query($sql);  
        while($row=$db->fetch_array($result_rows))  
        {  
            //有中文的字段应转换为UTF-8   //$row['department']=iconv('GB2312','UTF-8',$row['department']);
            $select_title="放音";//iconv('GB2312','UTF-8','选择');  
            $row['select']='<a href="#"  style="text-decoration:none;" onclick="javascript:Play_Record(\''. $row['UniqueId']. '\',\''. $row['UserField'] .'\');"> ' . $select_title . ' </a>';
            //$row['record_file']='<a href="#"  style="text-decoration:none;" onclick="javascript:PlayWav(\''. $row['CallSheetID'] . '\',\''.$row['name'] . '\',\''. $row['Ring'] . '\',\''. $row['Agent'] . '\',\''. $row['State'] . '\',\''. $row['Queue']. '\',\''. $row['CallNo'] . '\',\'' .$record_file . '\');"> ' . $select_title . ' </a>';
            //呼叫类型
            switch ($row['CallType'])
            {
                case "outgoing": //sina
                  $row['CallType'] = "呼出";
                  break;
                case "outbound": //tina
                  $row['CallType'] = "呼出";
                  break; 
                case "incoming": //sina
                  $row['CallType'] = "呼入" ;
                  break;  
                case "inbound": //tina
                  $row['CallType'] = "呼入" ;
                  break;
                case "internal":
                  $row['CallType'] = "内部" ;
                  break;
                case "inter":
                  $row['CallType'] = "内部" ;
                  break;
                default:
                  //表达式的值不等于 label1 及 label2 时执行的代码;
            }
            array_push($items, $row);  
            //WriteLog($row['CallType']);  
        }  
        //WriteLog("总话费 金额 ".$sql);  
        $arr_result['rows'] = $items;       
        
        /**/
        //总话费 金额
        $sum_tel_rate=0;
        $sql = "select sum(CallFee) from {$table_name} where 1=1 ".$where ; 
        //WriteLog("总话费 金额 ".$sql);  
        $result_rows=$db->query($sql);  
        while($row=$db->fetch_array($result_rows))  
        {  
            if($row[0]!=""){
                $sum_tel_rate=$row[0];
            }
        }
        //WriteLog($sql);  
        //取小数点后两位?PHP保留两位小数的几种方法   round($num,2); 
        $arr_result['sum_tel_rate'] = round($sum_tel_rate,2);   

        //输出
        //WriteLog(json_encode($arr_result));  
        echo json_encode($arr_result);   
   
?>   

 

posted @ 2024-01-31 17:01  海乐学习  阅读(301)  评论(0编辑  收藏  举报