链接

CollectionView.filter属性允许您为集合指定一个过滤功能.

在一些特殊情况下,您可能需要使用两个或多个独立的过滤器功能。 例如,您可能需要对传入的数据应用过滤器,并让FlexGridFilter对数据应用第二级过滤.

为了实现这一点,您可以链接多个CollectionView对象,使得一个集合的输出用作下一个的输入.

此示例使用基于输入字段的过滤器和基于第一集合的输出的第二个 view2 集合创建一个view集合。 它将网格绑定到具有附加的独立过滤器的第二集合:

import 'bootstrap.css'; import '@grapecity/wijmo.styles/wijmo.css'; import './styles.css'; // import * as wijmo from '@grapecity/wijmo'; import * as grid from '@grapecity/wijmo.grid'; import * as gridFilter from '@grapecity/wijmo.grid.filter'; import { getData } from './data'; // document.readyState === 'complete' ? init() : window.onload = init; // function init() { // create a CollectionView based on the raw data let view = new wijmo.CollectionView(getData()); // // create a second CollectionView based on the first one let view2 = new wijmo.CollectionView(view.items, { collectionChanged: (sender) => { let cnt = document.querySelector('#cnt'); cnt.textContent = wijmo.format('{length:n0}', sender.items); } }); // // bind a grid to the second CollectionView let theGrid = new grid.FlexGrid('#theGrid', { itemsSource: view2, alternatingRowStep: 0, headersVisibility: 'Column' }); // // add a filter to the grid (will act on the second CollectionView) var filter = new gridFilter.FlexGridFilter(theGrid); // // update the filter on the first CollectionView when the text changes document.querySelector('#theFilter').addEventListener('input', (e) => { // update first CollectionView's filter let filterText = e.target.value.toLowerCase(); // view.filter = (item) => { return filterText ? item.country.toLowerCase().indexOf(filterText) > -1 : true; }; // // update second collection view's sourceCollection view2.sourceCollection = view.items; }); } <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <title>GrapeCity CollectionView Chaining Filters</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 class="form-group row"> <div class="col-xs-2"> <input id="theFilter" name="filter" class="form-control" placeholder="country filter" /> </div> </div> <p> Result (<span id="cnt"></span> items): </p> <div id="theGrid"> </div> </div> </body> </html> // export function getData() { let countries = ['US', 'Germany', 'UK', 'Japan', 'Italy', 'Greece'], data = []; // for (let i = 0; i < 1000; i++) { data.push({ id: i, country: countries[i % countries.length], downloads: Math.round(Math.random() * 20000), sales: Math.random() * 10000, expenses: Math.random() * 5000 }); } // return data; } .wj-flexgrid { max-height: 250px; } label { width: 100% } import 'bootstrap.css'; import '@grapecity/wijmo.styles/wijmo.css'; import './styles.css'; // import { CollectionView } from '@grapecity/wijmo'; // import { Component, Inject, enableProdMode, NgModule } from '@angular/core'; import { platformBrowserDynamic } from '@angular/platform-browser-dynamic'; import { BrowserModule } from '@angular/platform-browser'; import { WjGridModule } from '@grapecity/wijmo.angular2.grid'; import { WjGridFilterModule } from '@grapecity/wijmo.angular2.grid.filter'; import { DataService, TDataItem } from './app.data'; // @Component({ selector: 'app-component', templateUrl: 'src/app.component.html' }) export class AppComponent { private view: CollectionView; view2: CollectionView; // constructor(@Inject(DataService) private dataService: DataService) { this.view = new CollectionView(dataService.getData()); this.view2 = new CollectionView(this.view.items); } // updateFilter(value: string) { // update first CollectionView's filter this.view.filter = (item: TDataItem) => { return value ? item.country.toLowerCase().indexOf(value) > -1 : true; } // // update second collection view's sourceCollection this.view2.sourceCollection = this.view.items; } } // @NgModule({ imports: [WjGridModule, WjGridFilterModule, BrowserModule], declarations: [AppComponent], providers: [DataService], 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 CollectionView Chaining Filters</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"> <div class="form-group row"> <div class="col-xs-2"> <input #theFilter class="form-control" placeholder="country filter" (input)="updateFilter(theFilter.value.toLowerCase())" /> </div> </div> <p> Result ({{ view2.items.length | number:0 }} items): </p> <wj-flex-grid [itemsSource]="view2" [alternatingRowStep]="0" [headersVisibility]="'Column'"> <wj-flex-grid-filter></wj-flex-grid-filter> </wj-flex-grid> </div> import { Injectable } from '@angular/core'; // export type TDataItem = { id: number; country: string; downloads: number; sales: number; expenses: number; } // @Injectable() export class DataService { getData() { let countries = ['US', 'Germany', 'UK', 'Japan', 'Italy', 'Greece'], data: TDataItem[] = []; // for (let i = 0; i < 1000; i++) { data.push({ id: i, country: countries[i % countries.length], downloads: Math.round(Math.random() * 20000), sales: Math.random() * 10000, expenses: Math.random() * 5000 }); } // return data; } } .wj-flexgrid { max-height: 250px; } label { width: 100% } <template> <div class="container-fluid"> <div class="form-group row"> <div class="col-xs-2"> <input class="form-control" placeholder="country filter" v-on:input="updateFilter($event)" /> </div> </div> <p> Result ({{ view2.items.length }} items): </p> <wj-flex-grid :items-source="view2" :show-alternating-rows=false headers-visibility="Column"> <wj-flex-grid-filter></wj-flex-grid-filter> </wj-flex-grid> </div> </template> <script> import "bootstrap.css"; import "@grapecity/wijmo.styles/wijmo.css"; import Vue from 'vue'; import * as wjCore from '@grapecity/wijmo'; import { getData } from './data'; import { WjGridModule } from '@grapecity/wijmo.vue2.grid'; import { WjGridFilterModule } from '@grapecity/wijmo.vue2.grid.filter'; new Vue({ el: '#app', data:{ view: new wjCore.CollectionView(getData()) }, computed:{ view2: function(){ return new wjCore.CollectionView(this.view.items); } }, methods:{ updateFilter(e) { // update first CollectionView's filter this.view.filter = (item) => { return e.srcElement.value ? item.country.toLowerCase().indexOf(e.srcElement.value.toLowerCase()) > -1 : true; } // // update second collection view's sourceCollection this.view2.sourceCollection = this.view.items; } } }) </script> <style> .container-fluid .wj-flexgrid { max-height: 250px; } label { width: 100% } </style> <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <title>GrapeCity CollectionView Chaining Filters</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> export function getData(callback, delay) { let countries = ['US', 'Germany', 'UK', 'Japan', 'Italy', 'Greece'], data = []; // for (let i = 0; i < 1000; i++) { data.push({ id: i, country: countries[i % countries.length], downloads: Math.round(Math.random() * 20000), sales: Math.random() * 10000, expenses: Math.random() * 5000 }); } // return data; } import 'bootstrap.css'; import '@grapecity/wijmo.styles/wijmo.css'; import './app.css'; import * as React from 'react'; import * as ReactDOM from 'react-dom'; import * as wjCore from '@grapecity/wijmo'; import * as wjGrid from '@grapecity/wijmo.react.grid'; import { getData } from './data'; class App extends React.Component { constructor(props) { super(props); this.updateFilter = (target) => { // update first CollectionView's filter this.setState(Object.assign(this.state.view, { filter: (item) => { return target.value ? item.country.toLowerCase().indexOf(target.value.toLowerCase()) > -1 : true; } })); // update second collection view's sourceCollection this.setState(Object.assign(this.state.view2, { sourceCollection: this.state.view.items })); }; this.state = { view: new wjCore.CollectionView(getData()), view2: null }; } ; componentWillMount() { this.setState({ view2: new wjCore.CollectionView(this.state.view.items) }); } render() { return <div className="container-fluid"> <div className="form-group row"> <div className="col-xs-2"> <input className="form-control" placeholder="country filter" onInput={e => this.updateFilter(e.target)}/> </div> </div> <p> Result ({this.state.view2.items.length} items): </p> <wjGrid.FlexGrid showAlternatingRows={false} headersVisibility="Column" itemsSource={this.state.view2}> </wjGrid.FlexGrid> </div>; } } 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>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'); </script> </head> <body> <div id="app"></div> </body> </html> .wj-flexgrid { max-height: 250px; } label { width: 100% } export function getData() { let countries = ['US', 'Germany', 'UK', 'Japan', 'Italy', 'Greece'], data = []; // for (let i = 0; i < 1000; i++) { data.push({ id: i, country: countries[i % countries.length], downloads: Math.round(Math.random() * 20000), sales: Math.random() * 10000, expenses: Math.random() * 5000 }); } // return data; }