easyui datagrid合計行の追加

3702 ワード

easyui datagrid追加合計行1、footerバー追加合計行(1)、PHPコード
/**
 * @todo  
*/
public function reportAction(){
    if( $this->getRequest()->isPost() ){
        $where = [];
        $search = isset($_POST['report']) ? $_POST['report'] : [];
        //$where = [ 'uid'=>$uid ];
        // 
        if(isset($search['start_time']) && $search['start_time']){
            $where[] = ['report_time', '=', $search['start_time']];
        }
                    
        /** */
        $usort = 'report_time';
        $uorder = 'DESC';    // 
                    
        if( isset($_POST['sort']) )     $usort = $_POST['sort'];
        if( isset($_POST['order']) )    $uorder = $_POST['order'];
        
        $mReport = new ReportModel();
        
        $rows = $mReport->getList( $where,'id,report_time,upload_num,recomme_num', (int) $_POST['page'], (int)$_POST['rows'], [$usort, $uorder] );
        if($rows){
            $upload_num = $recomme_num = 0;
            foreach ($rows as $k => $v){
                $upload_num += $v['upload_num'];
                $recomme_num += $v['recomme_num'];
            }
        }
        $total = $mReport->count( $where );
        $footer = ['report_time'=>' ', 'upload_num'=>''.$upload_num.'', 'recomme_num'=>''.$recomme_num.''];
        
        $data['rows'] = $rows;
        $data['total'] = $total;
        $data['footer'] = [ $footer ];
        die( json_encode($data) );
    }
}

(2)、返されるデータフォーマット:footer行の追加
result = ['rows' => [    0 => ['upload_num' => 0,'recomme_num' => 0,'report_time' => '2018-06-20',],
                         1 => ['upload_num' => 1,'recomme_num' => 0,'report_time' => '2018-06-19',],
                  ...
                      ],
           'total' => 80,
           'footer' => [ [ 'upload_num' => 6,'recomme_num' => 0,'report_time' => ' ',] ]    
            // ,'' 
         ]

(3)、datagrid設定パラメータshowFooter:true,2、rowsに1行追加(1)、バックグラウンド戻りデータは変わらず、フロントjsに1行データ1を組み立てる)、datagridテーブルにonLoadSuccessイベントを追加
onLoadSuccess:function(data){
    $('#easyuiTable_id').datagrid('appendRow', {
        report_time: ' ',
        upload_num: getTotal("upload_num"),
        recomme_num:getTotal("recomme_num"),
    });
},

2)、getTotalメソッド合計数の計算
function getTotal(colName) {
    var rows = $('#easyuiTable_id').datagrid('getRows');
    var total = 0;
    for (var i = 0; i < rows.length; i++) {
        total += parseFloat(rows[i][colName]);
    }
    return ''+total+'';
}

(2)、フロントは変わらず、バックグラウンドに合計データを追加する
if($rows){
    $upload_num = $recomme_num = 0;
    foreach ($rows as $k => $v){
        $upload_num += $v['upload_num'];
        $recomme_num += $v['recomme_num'];
    }
    $temp = ['report_time'=>" ", 'upload_num'=>''.$upload_num.'', 
             'recomme_num'=>''.$recomme_num.'' ];
                
    //$rows[] = $temp;            //(1)、 
    //array_push($rows, $temp);    //(2)、 
    array_unshift($rows, $temp);    // 
}