13693261870
2022-09-16 354b3dbfbffb3df45212a2a44dbbf48b4acc2594
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
Ext.define('KitchenSink.view.grid.MultipleSorting', {
    extend: 'Ext.grid.Panel',
    xtype: 'multi-sort-grid',
    title : 'Multiple Sort Grid',
 
    //<example>
    exampleDescription: [
        '<p>This example shows how to sort a grid by more than a single field.</p>',
        '<p>The store is initially sorted by Rating DESC then by Salary ASC, as indicated in the toolbar.</p>',
        '<p>Click a button to change sorting direction, drag buttons to reorder them.</p>',
        '<p>This example also uses the <tt>Ext.ux.ToolbarDroppable</tt> plugin to allow column headers to be dropped onto the toolbar. Try it with',
        'Name column header. Each column is only allowed one button, so Rating and Salary cannot be dropped in this example.</p>'
    ],
    //</example>
    requires: [
        'Ext.data.*',
        'Ext.grid.*',
        'Ext.util.*',
        'Ext.toolbar.*',
        'Ext.ux.ToolbarDroppable',
        'Ext.ux.BoxReorderer'
    ],
 
    columns: [{
        text: 'Name',
        flex:1 ,
        sortable: false,
        dataIndex: 'name'
    }, {
        text: 'Rating',
        width: 125,
        sortable: false,
        dataIndex: 'rating'
    }, {
        text: 'Salary',
        width: 125,
        sortable: false,
        dataIndex: 'salary',
        align: 'right',
        renderer: Ext.util.Format.usMoney
    }],
    stripeRows: true,
    height: 350,
    width : 600,
 
    initComponent: function () {
        var me = this;
 
        me.on({
            // wait for the first layout to access the headerCt (we only want this once):
            single: true,
            // tell the toolbar's droppable plugin that it accepts items from the columns' dragdrop group
            afterlayout: function(grid) {
                var headerCt = grid.child("headercontainer");
                droppable.addDDGroup(headerCt.reorderer.dragZone.ddGroup);
                me.doSort();
            }
        });
 
        var store = Ext.create('Ext.data.Store', {
            fields: [
               {name: 'rating', type: 'int'},
               {name: 'salary', type: 'float'},
               {name: 'name'}
            ],
            proxy: {
                type: 'memory',
                data: me.createFakeData(25),
                reader: {
                    type: 'array'
                }
            },
            autoLoad: true
        });
        this.store = store;
 
        var reorderer = Ext.create('Ext.ux.BoxReorderer', {
            listeners: {
                Drop: function(r, c, button) { //update sort direction when button is dropped
                    me.changeSortDirection(button, false);
                }
            }
        });
 
        var droppable = Ext.create('Ext.ux.ToolbarDroppable', {
            /**
             * Creates the new toolbar item from the drop event
             */
            createItem: function(data) {
                var header = data.header,
                    headerCt = header.ownerCt,
                    reorderer = headerCt.reorderer;
 
                // Hide the drop indicators of the standard HeaderDropZone
                // in case user had a pending valid drop in 
                if (reorderer) {
                    reorderer.dropZone.invalidateDrop();
                }
 
                return me.createSorterButtonConfig({
                    text: header.text,
                    sortData: {
                        property: header.dataIndex,
                        direction: "ASC"
                    }
                });
            },
 
            /**
             * Custom canDrop implementation which returns true if a column can be added to the toolbar
             * @param {Object} data Arbitrary data from the drag source. For a HeaderContainer, it will
             * contain a header property which is the Header being dragged.
             * @return {Boolean} True if the drop is allowed
             */
            canDrop: function(dragSource, event, data) {
                var sorters = me.getSorters(),
                    header  = data.header,
                    length = sorters.length,
                    entryIndex = this.calculateEntryIndex(event),
                    targetItem = this.toolbar.getComponent(entryIndex),
                    i;
 
                // Group columns have no dataIndex and therefore cannot be sorted
                // If target isn't reorderable it could not be replaced
                if (!header.dataIndex || (targetItem && targetItem.reorderable === false)) {
                    return false;
                }
 
                for (i = 0; i < length; i++) {
                    if (sorters[i].property == header.dataIndex) {
                        return false;
                    }
                }
                return true;
            },
 
            afterLayout: function () {
                me.doSort();
            }
        });
 
        //create the toolbar with the 2 plugins
        this.tbar = {
            itemId: 'tbar',
            items  : [{
                xtype: 'tbtext',
                text: 'Sorting order:',
                reorderable: false
            }, me.createSorterButtonConfig({
                text: 'Rating',
                sortData: {
                    property: 'rating',
                    direction: 'DESC'
                }
            }), me.createSorterButtonConfig({
                text: 'Salary',
                sortData: {
                    property: 'salary',
                    direction: 'ASC'
                }
            })],
            plugins: [reorderer, droppable]
        };
 
        this.callParent();
    },
 
    /**
     * Callback handler used when a sorter button is clicked or reordered
     * @param {Ext.Button} button The button that was clicked
     * @param {Boolean} changeDirection True to change direction (default). Set to false for reorder
     * operations as we wish to preserve ordering there
     */
    changeSortDirection: function (button, changeDirection) {
        var sortData = button.sortData,
            iconCls  = button.iconCls;
        
        if (sortData) {
            if (changeDirection !== false) {
                button.sortData.direction = Ext.String.toggle(button.sortData.direction, "ASC", "DESC");
                button.setIconCls(Ext.String.toggle(iconCls, "sort-direction-asc", "sort-direction-desc"));
            }
            this.store.clearFilter();
            this.doSort();
        }
    },
 
    doSort: function () {
        this.store.sort(this.getSorters());
    },
 
    /**
     * Returns an array of sortData from the sorter buttons
     * @return {Array} Ordered sort data from each of the sorter buttons
     */
    getSorters: function () {
        var sorters = [];
        var tbar = this.down('#tbar');
 
        Ext.each(tbar.query('button'), function(button) {
            sorters.push(button.sortData);
        }, this);
 
        return sorters;
    },
 
    /**
     * Convenience function for creating Toolbar Buttons that are tied to sorters
     * @param {Object} config Optional config object
     * @return {Object} The new Button configuration
     */
    createSorterButtonConfig: function (config) {
        var me = this;
        config = config || {};
        Ext.applyIf(config, {
            listeners: {
                click: function(button, e) {
                    me.changeSortDirection(button, true);
                }
            },
            iconCls: 'sort-direction-' + config.sortData.direction.toLowerCase(),
            reorderable: true,
            xtype: 'button'
        });
        return config;
    },
 
    /**
     * Returns an array of fake data
     * @param {Number} count The number of fake rows to create data for
     * @return {Array} The fake record data, suitable for usage with an ArrayReader
     */
    createFakeData: function (count) {
        var firstNames   = ['Ed', 'Tommy', 'Aaron', 'Abe', 'Jamie', 'Adam', 'Don', 'Evan', 'Phil', 'Nicolas', 'Nige'],
            lastNames    = ['Spencer', 'Maintz', 'Conran', 'Elias', 'Avins', 'Mishcon', 'Griffin', 'Trimboli', 'Guerrant', 'Ferrero', 'White'],
            ratings      = [1, 2, 3, 4, 5],
            salaries     = [100, 400, 900, 1500, 1000000];
 
        var data = [];
        for (var i = 0; i < (count || 25); i++) {
            var ratingId    = Math.floor(Math.random() * ratings.length),
                salaryId    = Math.floor(Math.random() * salaries.length),
                firstNameId = Math.floor(Math.random() * firstNames.length),
                lastNameId  = Math.floor(Math.random() * lastNames.length),
 
                rating      = ratings[ratingId],
                salary      = salaries[salaryId],
                name        = Ext.String.format("{0} {1}", firstNames[firstNameId], lastNames[lastNameId]);
 
            data.push([rating, salary, name]);
        }
        return data;
    }
});