3 * Copyright(c) 2006-2010 Sencha Inc.
5 * http://www.sencha.com/license
7 Ext.onReady(function() {
8 // create the data store
9 var store = new Ext.data.ArrayStore({
11 {name: 'rating', type: 'int'},
12 {name: 'salary', type: 'float'},
17 // manually load local fake data
18 store.loadData(createFakeData(25));
20 var reorderer = new Ext.ux.ToolbarReorderer();
21 var droppable = new Ext.ux.ToolbarDroppable({
23 * Creates the new toolbar item from the drop event
25 createItem: function(data) {
26 var column = this.getColumnFromDragDrop(data);
28 return createSorterButton({
31 field: column.dataIndex,
38 * Custom canDrop implementation which returns true if a column can be added to the toolbar
39 * @param {Object} data Arbitrary data from the drag source
40 * @return {Boolean} True if the drop is allowed
42 canDrop: function(dragSource, event, data) {
43 var sorters = getSorters(),
44 column = this.getColumnFromDragDrop(data);
46 for (var i=0; i < sorters.length; i++) {
47 if (sorters[i].field == column.dataIndex) return false;
56 * Helper function used to find the column that was dragged
57 * @param {Object} data Arbitrary data from
59 getColumnFromDragDrop: function(data) {
60 var index = data.header.cellIndex,
61 colModel = grid.colModel,
62 column = colModel.getColumnById(colModel.getColumnId(index));
68 //create the toolbar with the 2 plugins
69 var tbar = new Ext.Toolbar({
70 items : ['Sorting order:', '-'],
71 plugins: [reorderer, droppable],
74 reordered: function(button) {
75 changeSortDirection(button, false);
81 var grid = new Ext.grid.GridPanel({
86 {header: 'Name', width: 160, sortable: false, dataIndex: 'name', id:'company'},
87 {header: 'Rating', width: 125, sortable: false, dataIndex: 'rating'},
88 {header: 'Salary', width: 125, sortable: false, dataIndex: 'salary', renderer: 'usMoney'}
90 autoExpandColumn: 'company',
100 //here we tell the toolbar's droppable plugin that it can accept items from the columns' dragdrop group
102 var dragProxy = grid.getView().columnDrag,
103 ddGroup = dragProxy.ddGroup;
105 droppable.addDDGroup(ddGroup);
110 tbar.add(createSorterButton({
118 tbar.add(createSorterButton({
126 // render the grid to the specified div in the page
127 grid.render('grid-example');
130 //The following functions are used to get the sorting data from the toolbar and apply it to the store
133 * Tells the store to sort itself according to our sort data
136 store.sort(getSorters(), "ASC");
140 * Callback handler used when a sorter button is clicked or reordered
141 * @param {Ext.Button} button The button that was clicked
142 * @param {Boolean} changeDirection True to change direction (default). Set to false for reorder
143 * operations as we wish to preserve ordering there
145 function changeSortDirection(button, changeDirection) {
146 var sortData = button.sortData,
147 iconCls = button.iconCls;
149 if (sortData != undefined) {
150 if (changeDirection !== false) {
151 button.sortData.direction = button.sortData.direction.toggle("ASC", "DESC");
152 button.setIconClass(iconCls.toggle("sort-asc", "sort-desc"));
161 * Returns an array of sortData from the sorter buttons
162 * @return {Array} Ordered sort data from each of the sorter buttons
164 function getSorters() {
167 Ext.each(tbar.findByType('button'), function(button) {
168 sorters.push(button.sortData);
175 * Convenience function for creating Toolbar Buttons that are tied to sorters
176 * @param {Object} config Optional config object
177 * @return {Ext.Button} The new Button object
179 function createSorterButton(config) {
180 config = config || {};
182 Ext.applyIf(config, {
184 click: function(button, e) {
185 changeSortDirection(button, true);
188 iconCls: 'sort-' + config.sortData.direction.toLowerCase(),
192 return new Ext.Button(config);
196 * Returns an array of fake data
197 * @param {Number} count The number of fake rows to create data for
198 * @return {Array} The fake record data, suitable for usage with an ArrayReader
200 function createFakeData(count) {
201 var firstNames = ['Ed', 'Tommy', 'Aaron', 'Abe', 'Jamie', 'Adam', 'Dave', 'David', 'Jay'],
202 lastNames = ['Spencer', 'Maintz', 'Conran', 'Elias', 'Avins', 'Mishcon', 'Kaneda', 'Davis', 'Robinson'],
203 ratings = [1, 2, 3, 4, 5],
204 salaries = [100, 400, 900, 1500, 1000000];
207 for (var i=0; i < (count || 25); i++) {
208 var ratingId = Math.floor(Math.random() * ratings.length),
209 salaryId = Math.floor(Math.random() * salaries.length),
210 firstNameId = Math.floor(Math.random() * firstNames.length),
211 lastNameId = Math.floor(Math.random() * lastNames.length),
213 rating = ratings[ratingId],
214 salary = salaries[salaryId],
215 name = String.format("{0} {1}", firstNames[firstNameId], lastNames[lastNameId]);
217 data.push([rating, salary, name]);