Grid sends pq_sort
parameter to the remote server as mentioned in the tutorial on remote requests.
ShipCountry | Customer Name | Shipping Via | Order ID | Order Date | Required Date | Shipped Date | Freight | Shipping Name | Shipping Address | Shipping City |
---|
x1
2<div id="grid_sorting_remote" style="margin: 5px auto;">
3</div>
4
761
2$(function () {
3var colM = [
4{ title: "ShipCountry", width: 100, dataIndx: "ShipCountry" },
5{ title: "Customer Name", width: 130, dataIndx: "ContactName" },
6{ title: "Shipping Via", width: 100, dataIndx: "ShipVia" },
7{ title: "Order ID", width: 100, dataIndx: "OrderID", dataType: "integer" },
8{ title: "Order Date", width: "100", dataIndx: "OrderDate", dataType: "date" },
9{ title: "Required Date", width: 100, dataIndx: "RequiredDate", dataType: "date" },
10{ title: "Shipped Date", width: 100, dataIndx: "ShippedDate", dataType: "date" },
11{ title: "Freight", width: 100, align: "right", dataType: "float", dataIndx: "Freight" },
12{ title: "Shipping Name", width: 160, dataIndx: "ShipName" },
13{ title: "Shipping Address", width: 200, dataIndx: "ShipAddress" },
14{ title: "Shipping City", width: 100, dataIndx: "ShipCity" },
15{ title: "Shipping Region", width: 130, dataIndx: "ShipRegion" },
16{ title: "Shipping Postal Code", width: 135, dataIndx: "ShipPostalCode" }
17];
18var dataModel = {
19location: "remote",
20method: "GET",
21url: "/pro/orders/get" //for ASP.NET
22//url: "orders.php", //for PHP
23}
24
25var gObj = {
26dataModel: dataModel,
27sortModel: {
28type: 'remote',
29single: false,
30sorter: [{ dataIndx: 'ShipCountry', dir: 'up' }, { dataIndx: 'ContactName', dir: 'down'}],
31space: true,
32multiKey: null
33},
34flex: {one: true},
35wrap: false,
36hwrap: false,
37showTitle: false,
38colModel: colM,
39resizable: true,
40virtualX: true, virtualY: true,
41freezeCols: 2,
42toolbar: {
43items: [
44{
45type: 'select',
46label: 'Sorting Type:',
47value: 'multi', //default value.
48options: [
49{'single': 'Single without shift key'},
50{'singlemulti': 'Single with shift key for multiple'},
51{'multi': 'Multiple columns'}
52],
53listener: function(evt){
54var val = $(evt.target).val(),
55single = true,
56multiKey = null;
57
58if(val == 'singlemulti'){
59multiKey = 'shiftKey';
60}
61else if(val == 'multi'){
62single = false;
63}
64
65this.option("sortModel.single", single);
66this.option("sortModel.multiKey", multiKey);
67this.sort(); //refresh sorting.
68}
69}
70]
71}
72};
73var $grid = $("#grid_sorting_remote").pqGrid(gObj);
74
75});
76
1001
2public class OrderCustomer
3{
4[Key]
5public int OrderID { get; set; }
6public String ContactName { get; set; }
7public int EmployeeID { get; set; }
8public DateTime? OrderDate { get; set; }
9public DateTime? RequiredDate { get; set; }
10public DateTime? ShippedDate { get; set; }
11public String ShipVia { get; set; }
12public Decimal Freight { get; set; }//money
13public String ShipName { get; set; }
14public String ShipAddress { get; set; }
15public String ShipCity { get; set; }
16public String ShipRegion { get; set; }
17public String ShipPostalCode { get; set; }
18public String ShipCountry { get; set; }
19public bool paid { get; set; }
20}
21public class SortHelper
22{
23private struct sortobj
24{
25public String dataIndx;
26public String dir;
27}
28public static String deSerializeSort(String pq_sort)
29{
30JavaScriptSerializer js = new JavaScriptSerializer();
31List<sortobj> sorters = js.Deserialize<List<sortobj>>(pq_sort);
32List<String> columns = new List<string>();
33String sortby = "";
34foreach (sortobj sorter in sorters)
35{
36var dataIndx = sorter.dataIndx;
37var dir = sorter.dir;
38if (dir == "up")
39{
40dir = "asc";
41}
42else
43{
44dir = "desc";
45}
46if (ColumnHelper.isValidColumn(dataIndx))
47{
48columns.Add(dataIndx + " " + dir);
49}
50//columns.Add(
51}
52
53if (columns.Count > 0)
54{
55sortby = " order by " + String.Join(", ", columns);
56}
57return sortby;
58}
59}
60public class ColumnHelper
61{
62public static bool isValidColumn(String dataIndx)
63{
64if (Regex.IsMatch(dataIndx, "^[a-z,A-Z]*$"))
65{
66return true;
67}
68else
69{
70return false;
71}
72}
73}
74public class ordersController : Controller
75{
76public ActionResult get(String pq_sort)
77{
78pqTestContext db = new pqTestContext();
79
80//sort
81String sortQuery = "";
82if (pq_sort != null && pq_sort.Length > 0)
83{
84sortQuery = SortHelper.deSerializeSort(pq_sort);
85}
86
87List<OrderCustomer> orders = db.Database.SqlQuery<OrderCustomer>(@"select * from ordercustomers " + sortQuery).ToList();
88
89StringBuilder sb = new StringBuilder(@"{""data"":");
90
91JavaScriptSerializer js = new JavaScriptSerializer();
92String json = js.Serialize(orders);
93sb.Append(json);
94
95sb.Append("}");
96
97return this.Content(sb.ToString(), "text/text");
98}
99}
100
731
2class ColumnHelper
3{
4public static function isValidColumn($dataIndx)
5{
6if (preg_match('/^[a-z,A-Z]*$/', $dataIndx))
7{
8return true;
9}
10else
11{
12return false;
13}
14}
15}
16class SortHelper
17{
18public static function deSerializeSort($pq_sort)
19{
20$sorters = json_decode($pq_sort);
21$columns = array();
22$sortby = "";
23foreach ($sorters as $sorter){
24$dataIndx = $sorter->dataIndx;
25$dir = $sorter->dir;
26if ($dir == "up")
27{
28$dir = "asc";
29}
30else
31{
32$dir = "desc";
33}
34if (ColumnHelper::isValidColumn($dataIndx))
35{
36$columns[] = $dataIndx . " " . $dir;
37}
38else{
39throw new Exception("invalid column ".$dataIndx);
40}
41}
42if (sizeof($columns) > 0)
43{
44$sortby = " order by " . join(", ", $columns);
45}
46return $sortby;
47}
48}
49
50//orders.php
51$sortQuery = "";
52if(isset($_GET["pq_sort"]))
53{
54$pq_sort = $_GET["pq_sort"];
55$sortQuery = SortHelper::deSerializeSort($pq_sort);
56}
57
58$sql = "Select * from ordercustomers ".$sortQuery;
59
60$dsn = 'mysql:host='.DB_HOSTNAME.';dbname='.DB_NAME;
61$options = array(
62PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8',
63);
64$dbh = new PDO($dsn, DB_USERNAME, DB_PASSWORD, $options);
65
66$stmt = $dbh->prepare($sql);
67
68$stmt->execute();
69
70$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
71
72echo "{\"data\":". json_encode( $rows ) ." }" ;
73
1321
2//ColumnHelper.java
3package helper;
4
5import java.util.regex.*;
6
7/**
8* @author paramvir
9*/
10public class ColumnHelper {
11
12public static boolean isValidColumn(String dataIndx) {
13String pattern = "^[a-z,A-Z]*$";
14
15Pattern r = Pattern.compile(pattern);
16
17Matcher m = r.matcher(dataIndx);
18if (m.find()) {
19return true;
20} else {
21return false;
22}
23}
24}
25//SortHelper.java
26package helper;
27
28import flexjson.JSONDeserializer;
29import java.util.ArrayList;
30import java.util.List;
31import java.util.Map;
32
33/**
34*
35* @author paramvir
36*/
37public class SortHelper {
38
39public static String deSerialize(String pq_sort){
40
41JSONDeserializer<ArrayList<Map>> deserializer = new JSONDeserializer<ArrayList<Map>>();
42ArrayList<Map> sorters = deserializer.deserialize(pq_sort);
43
44ArrayList<String> columns = new ArrayList<String>();
45String sortby = "";
46for (Map sorter : sorters)
47{
48String dataIndx = (String) sorter.get("dataIndx");
49String dir = (String) sorter.get("dir");
50if (dir.equals("up"))
51{
52dir = "asc";
53}
54else
55{
56dir = "desc";
57}
58if (ColumnHelper.isValidColumn(dataIndx))
59{
60columns.add(dataIndx + " " + dir);
61}
62}
63
64if (columns.size() > 0)
65{
66sortby = " order by " + stringJoin(", ", columns);
67}
68return sortby;
69}
70private static String stringJoin (String connector, List<String> list){
71String str = "";
72for(int i=0, len = list.size(); i< len; i++){
73str += list.get(i);
74if(i<len-1){
75str += connector;
76}
77}
78return str;
79}
80}
81//OrdersController.java
82package controller;
83
84import domain.OrderCustomer;
85import flexjson.JSONSerializer;
86import flexjson.transformer.DateTransformer;
87import helper.FilterHelper;
88import helper.SortHelper;
89import java.util.*;
90import org.apache.commons.logging.Log;
91import org.apache.commons.logging.LogFactory;
92import org.springframework.jdbc.core.BeanPropertyRowMapper;
93import org.springframework.jdbc.core.support.JdbcDaoSupport;
94import org.springframework.stereotype.Controller;
95import org.springframework.web.bind.annotation.*;
96
97/**
98* @author paramvir
99*/
100@Controller
101public class OrdersController extends JdbcDaoSupport{
102
103protected final Log logger = LogFactory.getLog(getClass());
104private String formatDate = "MM/dd/yyyy";
105
106@RequestMapping(value="/orders/get",method=RequestMethod.GET)
107public @ResponseBody String get(@RequestParam(required=false) String pq_sort)
108{
109String sortQuery = "";
110if (pq_sort != null && pq_sort.length() > 0)
111{
112sortQuery = SortHelper.deSerialize(pq_sort);
113}
114logger.info("pq_sort is " + pq_sort + "sortQuery is "+ sortQuery);
115
116List<OrderCustomer> orders = getJdbcTemplate().query(
117"Select * from ordercustomers " + sortQuery,
118new BeanPropertyRowMapper(OrderCustomer.class)
119);
120
121
122JSONSerializer serializer = new JSONSerializer().exclude("*.class");
123String json = serializer
124.transform(new DateTransformer(formatDate), Date.class)
125.serialize(orders);
126
127String sb="{\"data\":"+json+"}";
128
129return sb;
130}
131}
132