http://trirand.com/blog/jqgrid/jqgrid.html

http://trirand.com/blog/jqgrid/jqgrid.html

This example show how we can load JSON data from Server. Note that we
we can use a multiple indexes in col model to sort data.
 
JSON Example
 
Inv No
 
Date
 
Client
 
Amount
 
Tax
 
Total
 
Notes
 
             
13 2007-10-06 Client 3 1000.00 0.00 1000.00  
12 2007-10-06 Client 2 700.00 140.00 840.00  
11 2007-10-06 Client 1 600.00 120.00 720.00  
10 2007-10-06 Client 2 100.00 20.00 120.00  
9 2007-10-06 Client 1 200.00 40.00 240.00  
8 2007-10-06 Client 3 200.00 0.00 200.00  
7 2007-10-05 Client 2 120.00 12.00 134.00  
6 2007-10-05 Client 1 50.00 10.00 60.00  
5 2007-10-05 Client 3 100.00 0.00 100.00 no tax at all
4 2007-10-04 Client 3 150.00 0.00 150.00 no tax
 
      Page of 2      
View 1 - 10 of 13
HTML ...
 
Java Scrpt code ... jQuery("#list2").jqGrid({ url:'server.php?q=2', datatype: "json", colNames:['Inv No','Date', 'Client', 'Amount','Tax','Total','Notes'], colModel:[ {name:'id',index:'id', 55}, {name:'invdate',index:'invdate', 90}, {name:'name',index:'name asc, invdate', 100}, {name:'amount',index:'amount', 80, align:"right"}, {name:'tax',index:'tax', 80, align:"right"}, {name:'total',index:'total', 80,align:"right"}, {name:'note',index:'note', 150, sortable:false} ], rowNum:10, rowList:[10,20,30], pager: '#pager2', sortname: 'id', viewrecords: true, sortorder: "desc", caption:"JSON Example" }); jQuery("#list2").jqGrid('navGrid','#pager2',{edit:false,add:false,del:false}); PHP with MySQL ... $page = $_GET['page']; // get the requested page $limit = $_GET['rows']; // get how many rows we want to have into the grid $sidx = $_GET['sidx']; // get index row - i.e. user click to sort $sord = $_GET['sord']; // get the direction if(!$sidx) $sidx =1; // connect to the database $db = mysql_connect($dbhost, $dbuser, $dbpassword) or die("Connection Error: " . mysql_error()); mysql_select_db($database) or die("Error conecting to db."); $result = mysql_query("SELECT COUNT(*) AS count FROM invheader a, clients b WHERE a.client_id=b.client_id"); $row = mysql_fetch_array($result,MYSQL_ASSOC); $count = $row['count']; if( $count >0 ) { $total_pages = ceil($count/$limit); } else { $total_pages = 0; } if ($page > $total_pages) $page=$total_pages; $start = $limit*$page - $limit; // do not put $limit*($page - 1) $SQL = "SELECT a.id, a.invdate, b.name, a.amount,a.tax,a.total,a.note FROM invheader a, clients b WHERE a.client_id=b.client_id ORDER BY $sidx $sord LIMIT $start , $limit"; $result = mysql_query( $SQL ) or die("Couldn t execute query.".mysql_error()); $responce->page = $page; $responce->total = $total_pages; $responce->records = $count; $i=0; while($row = mysql_fetch_array($result,MYSQL_ASSOC)) { $responce->rows[$i]['id']=$row[id]; $responce->rows[$i]['cell']=array($row[id],$row[invdate],$row[name],$row[amount],$row[tax],$row[total],$row[note]); $i++; } echo json_encode($responce); ...
每一天都要行动,在前进中寻求卓越。
原文地址:https://www.cnblogs.com/wshsdlau/p/2863548.html