This example shows combined editing, filtering and sorting.
Batch Editing & Filtering & Sorting Combined
 
Product ID
Product Name
Quantity Per Unit
Unit Price
Units In Stock
Discontinued
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
No rows to display.
Loading...


171
 
1
2
    $(function () {
3
        //called when save changes button is clicked.
4
        function saveChanges() {
5
            var grid = $grid.pqGrid('getInstance').grid;
6
            
7
            //attempt to save editing cell.
8
            if (grid.saveEditCell() === false) {
9
                return false;
10
            }
11
            
12
            if (grid.isDirty() && grid.isValidChange({ focusInvalid: true }).valid) {
13
14
                var changes = grid.getChanges({ format: "byVal" });
15
16
                //post changes to server 
17
                $.ajax({
18
                    dataType: "json",
19
                    type: "POST",
20
                    async: true,
21
                    beforeSend: function (jqXHR, settings) {
22
                        grid.showLoading();
23
                    },
24
                    url: "/pro/products/batch", //for ASP.NET, java   
25
                    //url:                                              
26
                    data: { list: JSON.stringify(changes) },
27
                    success: function (changes) {
28
                        //debugger;
29
                        grid.commit({ type: 'add', rows: changes.addList });
30
                        grid.commit({ type: 'update', rows: changes.updateList });
31
                        grid.commit({ type: 'delete', rows: changes.deleteList });
32
33
                        grid.history({ method: 'reset' });
34
                    },
35
                    complete: function () {
36
                        grid.hideLoading();
37
                    }
38
                });
39
            }
40
        }
41
        var obj = {
42
            hwrap: false,
43
            resizable: true,
44
            rowBorders: false,
45
            virtualX: true,
46
            filterModel: { header: true, type: 'local' },
47
            trackModel: { on: true }, //to turn on the track changes.            
48
            toolbar: {
49
                items: [
50
                    { type: 'button', icon: 'ui-icon-plus', label: 'New Product', listener: function () {
51
                            //append empty row at the end.                            
52
                            var rowData = { ProductID: 34, UnitPrice: 0.2 }; //empty row
53
                            var rowIndx = $grid.pqGrid("addRow", { rowData: rowData, checkEditable: true });
54
                            $grid.pqGrid("goToPage", { rowIndx: rowIndx });
55
                            $grid.pqGrid("editFirstCellInRow", { rowIndx: rowIndx });                        
56
                        }
57
                    },
58
                    { type: 'separator' },
59
                    { type: 'button', icon: 'ui-icon-disk', label: 'Save Changes', cls: 'changes', listener: function () {
60
                            saveChanges();                        
61
                        },
62
                        options: { disabled: true }
63
                    },
64
                    { type: 'button', icon: 'ui-icon-cancel', label: 'Reject Changes', cls: 'changes', listener: function () {
65
                            $grid.pqGrid("rollback");
66
                            $grid.pqGrid("history", { method: 'resetUndo' });                        
67
                        },
68
                        options: { disabled: true }
69
                    },
70
                    { type: 'separator' },
71
                    { type: 'button', icon: 'ui-icon-arrowreturn-1-s', label: 'Undo', cls: 'changes', listener: function () {
72
                            $grid.pqGrid("history", { method: 'undo' });                        
73
                        },
74
                        options: { disabled: true }
75
                    },
76
                    { type: 'button', icon: 'ui-icon-arrowrefresh-1-s', label: 'Redo', listener: function () {
77
                            $grid.pqGrid("history", { method: 'redo' });                        
78
                        },
79
                        options: { disabled: true }
80
                    }
81
                ]
82
            },
83
            scrollModel: { autoFit: true },
84
            swipeModel: { on: false },
85
            editModel: {
86
                saveKey: $.ui.keyCode.ENTER
87
            },
88
            editor: { select: true },
89
            title: "<b>Batch Editing & Filtering & Sorting Combined</b>",
90
            history: function (evt, ui) {
91
                var $grid = this.widget();
92
                if (ui.canUndo != null) {
93
                    $("button.changes", $grid).button("option", { disabled: !ui.canUndo });
94
                }
95
                if (ui.canRedo != null) {
96
                    $("button:contains('Redo')", $grid).button("option", "disabled", !ui.canRedo);
97
                }
98
                $("button:contains('Undo')", $grid).button("option", { label: 'Undo (' + ui.num_undo + ')' });
99
                $("button:contains('Redo')", $grid).button("option", { label: 'Redo (' + ui.num_redo + ')' });
100
            },
101
            colModel: [
102
                { title: "Product ID", dataType: "integer", dataIndx: "ProductID", editable: false, width: 80 },
103
                { title: "Product Name", width: 165, dataType: "string", dataIndx: "ProductName",
104
                    filter: { type: 'textbox', condition: 'begin', listeners: ['keyup'] },
105
                    validations: [
106
                        { type: 'minLen', value: 1, msg: "Required" },
107
                        { type: 'maxLen', value: 40, msg: "length should be <= 40" }
108
                    ]
109
                },
110
                { title: "Quantity Per Unit", width: 140, dataType: "string", dataIndx: "QuantityPerUnit",
111
                    validations: [
112
                        { type: 'minLen', value: 1, msg: "Required." },
113
                        { type: 'maxLen', value: 20, msg: "length should be <= 20" }
114
                    ]
115
                },
116
                { title: "Unit Price", width: 100, dataType: "float", dataIndx: "UnitPrice", format: '$##,###.00',
117
                    validations: [{ type: 'gt', value: 0.5, msg: "should be > 0.5"}]
118
                },
119
                //{ hidden: true },
120
                { title: "Units In Stock", width: 100, dataType: "integer", dataIndx: "UnitsInStock",
121
                    validations: [
122
                        { type: 'gte', value: 1, msg: "should be >= 1" },
123
                        { type: 'lte', value: 1000, msg: "should be <= 1000" }
124
                    ]
125
                },
126
                { title: "Discontinued", width: 100, dataType: "bool", align: "center", dataIndx: "Discontinued",
127
                    editor: { type: "checkbox", subtype: 'triple',
128
                        style: "margin:3px 5px;"
129
130
                    },
131
                    validations: [{ type: 'nonEmpty', msg: "Required"}]
132
                },
133
                { title: "", editable: false, minWidth: 83, sortable: false,
134
                    render: function (ui) {
135
                        return "<button type='button' class='delete_btn'>Delete</button>";
136
                    },
137
                    postRender: function (ui) {
138
                        var rowIndx = ui.rowIndx,
139
                            grid = this,
140
                            $cell = grid.getCell(ui);
141
142
                        $cell.find("button").button({ icons: { primary: 'ui-icon-scissors'} })
143
                        .bind("click", function () {
144
145
                            grid.addClass({ rowIndx: ui.rowIndx, cls: 'pq-row-delete' });
146
147
                            var ans = window.confirm("Are you sure to delete row No " + (rowIndx + 1) + "?");
148
                            grid.removeClass({ rowIndx: rowIndx, cls: 'pq-row-delete' });
149
                            if (ans) {
150
                                grid.deleteRow({ rowIndx: rowIndx });
151
                            }
152
                        });
153
                    }
154
                }
155
            ],
156
            postRenderInterval: -1, //call postRender synchronously.
157
            pageModel: { type: "local", rPP: 20 },
158
            dataModel: {
159
                dataType: "JSON",
160
                location: "remote",
161
                recIndx: "ProductID",
162
                url: "/pro/products/get", //for ASP.NET
163
                //url: "/pro/products.php", //for PHP
164
                getData: function (response) {
165
                    return { data: response.data };
166
                }
167
            }
168
        };
169
        var $grid = $("#grid_editing").pqGrid(obj);
170
    });
171