On-Demand Sorting

The FlexGrid relies on the CollectionView class to perform data operations including sorting, filtering, grouping, paging, change tracking, etc. The CollectionView refreshes and re-sorts the data after any edits. This sample shows how you can override that behavior and sort the grid only when the user clicks a column header. Editing will not cause sort updates.

import 'bootstrap.css'; import '@grapecity/wijmo.styles/wijmo.css'; import * as wjGrid from '@grapecity/wijmo.grid'; // document.readyState === 'complete' ? init() : window.onload = init; // function init() { // // create some random data var countries = 'US,Germany,UK,Japan,Sweden,Norway,Denmark'.split(','), data = []; for (var i = 0; i < countries.length; i++) { data.push({ id: i, country: countries[i], active: i % 5 != 0, downloads: Math.round(Math.random() * 200000), sales: Math.random() * 100000, expenses: Math.random() * 50000 }); } // // create the grid with on-demand sorting var view; var theGrid = new wjGrid.FlexGrid('#theGrid', { itemsSourceChanged: function (s, e) { view = s.collectionView; view.canSort = false; // prevent sorting }, sortedColumn: function (s, e) { view.canSort = false; // prevent sorting view.sourceCollection = view.items; // copy sorted values }, alternatingRowStep: 0, itemsSource: data }); // // allow sorting by mouse click theGrid.hostElement.addEventListener('mousedown', function (e) { if (theGrid.hitTest(e).panel == theGrid.columnHeaders) { view.canSort = true; } }, true); } <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <title>GrapeCity Wijmo FlexGrid On-demand Sorting</title> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <!-- SystemJS --> <script src="node_modules/systemjs/dist/system.src.js"></script> <script src="systemjs.config.js"></script> <script> System.import('./src/app'); </script> </head> <body> <div class="container-fluid"> <div id="theGrid"> </div> </div> </body> </html> import 'bootstrap.css'; import '@grapecity/wijmo.styles/wijmo.css'; // import { Component, enableProdMode, NgModule } from '@angular/core'; import { platformBrowserDynamic } from '@angular/platform-browser-dynamic'; import { BrowserModule } from '@angular/platform-browser'; import * as wjcGrid from '@grapecity/wijmo.grid'; import { WjGridModule } from '@grapecity/wijmo.angular2.grid'; // @Component({ selector: 'app-component', templateUrl: 'src/app.component.html' }) export class AppComponent { data: any[]; constructor() { this.data = this._getData(); } initializeGrid(flex: wjcGrid.FlexGrid) { flex.itemsSourceChanged.addHandler(() => { flex.collectionView.canSort = false; // prevent sorting }); flex.sortedColumn.addHandler(() => { flex.collectionView.canSort = false; // prevent sorting flex.collectionView.sourceCollection = flex.collectionView.items; // copy sorted values }); flex.hostElement.addEventListener('mousedown', function(e) { if (flex.hitTest(e).panel == flex.columnHeaders) { flex.collectionView.canSort = true; } }, true); } private _getData() { let countries = 'US,Germany,UK,Japan,Italy,Greece'.split(','), data = []; for (let i = 0; i < countries.length; i++) { data.push({ id: i, country: countries[i], active: i % 5 != 0, downloads: Math.round(Math.random() * 20000), sales: Math.random() * 10000, expenses: Math.random() * 5000 }); } return data; } } // @NgModule({ imports: [WjGridModule, BrowserModule], declarations: [AppComponent], bootstrap: [AppComponent] }) export class AppModule { } // enableProdMode(); // Bootstrap application with hash style navigation and global services. platformBrowserDynamic().bootstrapModule(AppModule); <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <title>GrapeCity Wijmo FlexGrid On-demand Sorting</title> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <!-- Polyfills --> <script src="node_modules/core-js/client/shim.min.js"></script> <script src="node_modules/zone.js/dist/zone.min.js"></script> <!-- SystemJS --> <script src="node_modules/systemjs/dist/system.js"></script> <script src="systemjs.config.js"></script> <script> // workaround to load 'rxjs/operators' from the rxjs bundle System.import('rxjs').then(function (m) { System.set(SystemJS.resolveSync('rxjs/operators'), System.newModule(m.operators)); System.import('./src/app.component'); }); </script> </head> <body> <app-component></app-component> </body> </html> <div class="container-fluid"> <wj-flex-grid #flex [alternatingRowStep]="0" (initialized)="initializeGrid(flex)" [(itemsSource)]="data"> </wj-flex-grid> </div> <template> <div class="container-fluid"> <wj-flex-grid :alternatingRowStep="0" :initialized="initializeGrid" :itemsSource="data"> </wj-flex-grid> </div> </template> <script> import "@grapecity/wijmo.styles/wijmo.css"; import 'bootstrap.css'; import Vue from 'vue'; import '@grapecity/wijmo.vue2.core'; import '@grapecity/wijmo.vue2.grid'; let App = Vue.extend({ name: 'app', data: function(){ return { data: this._getData() } }, methods: { initializeGrid(flex) { flex.itemsSourceChanged.addHandler(() => { flex.collectionView.canSort = false; // prevent sorting }); flex.sortedColumn.addHandler(() => { flex.collectionView.canSort = false; // prevent sorting flex.collectionView.sourceCollection = flex.collectionView.items; // copy sorted values }); flex.hostElement.addEventListener('mousedown', function(e) { if (flex.hitTest(e).panel == flex.columnHeaders) { flex.collectionView.canSort = true; } }, true); }, _getData: function() { let countries = 'US,Germany,UK,Japan,Italy,Greece'.split(','), data = []; for (let i = 0; i < countries.length; i++) { data.push({ id: i, country: countries[i], active: i % 5 != 0, downloads: Math.round(Math.random() * 20000), sales: Math.random() * 10000, expenses: Math.random() * 5000 }); } return data; } } }); new Vue({ render: h => h(App) }).$mount('#app'); </script> <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <title>AutoComplete</title> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <!-- SystemJS --> <script src="node_modules/systemjs/dist/system.src.js"></script> <script src="systemjs.config.js"></script> <script> System.import('./src/app.vue'); </script> </head> <body> <div id="app"> </div> </body> </html> import "@grapecity/wijmo.styles/wijmo.css"; import "bootstrap.css"; // import * as React from 'react'; import * as ReactDOM from 'react-dom'; import * as wjcGrid from "@grapecity/wijmo.react.grid"; import { getData } from "./data"; class App extends React.Component { constructor(props) { super(props); this.state = { data: getData() }; } render() { return <div className="container-fluid"> <wjcGrid.FlexGrid showAlternatingRows={false} initialized={this.initializeGrid.bind(this)} itemsSource={this.state.data}></wjcGrid.FlexGrid> </div>; } initializeGrid(flex) { flex.itemsSourceChanged.addHandler(() => { flex.collectionView.canSort = false; // prevent sorting }); flex.sortedColumn.addHandler(() => { flex.collectionView.canSort = false; // prevent sorting flex.collectionView.sourceCollection = flex.collectionView.items; // copy sorted values }); flex.hostElement.addEventListener('mousedown', function (e) { if (flex.hitTest(e).panel == flex.columnHeaders) { flex.collectionView.canSort = true; } }, true); } } ReactDOM.render(<App />, document.getElementById('app')); <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <title>Grapecity Wijmo OLAP Pivot Chart Overview</title> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <!-- SystemJS --> <script src="node_modules/systemjs/dist/system.src.js"></script> <script src="systemjs.config.js"></script> <script> System.import('./src/app'); </script> </head> <body> <div id="app"></div> </body> </html> export function getData() { let countries = 'US,Germany,UK,Japan,Italy,Greece'.split(','), data = []; for (let i = 0; i < countries.length; i++) { data.push({ id: i, country: countries[i], active: i % 5 != 0, downloads: Math.round(Math.random() * 20000), sales: Math.random() * 10000, expenses: Math.random() * 5000 }); } return data; }