从服务端加载数据

将数据加载到 CollectionView 中是很简单的.

从Server端加载数据

如果数据在服务器上,可以使用 httpRequest 方法. 当您从服务器获取响应时,将 sourceCollection 数组设置为响应值,或将新数据附加到 sourceCollection 数组:

如果您的数据服务API支持诸如过滤,排序和分页之类的命令,则可以在 httpRequest 调用中添加参数以支持这些调用。 您甚至可以将服务器API封装到扩展 CollectionVew的自定义类中.

有关基于定制服务器的 CollectionView 类的简单示例 请参阅我们的 ServerCollectionView 示例.

有关更完整的示例, 并且支持完整CRUD操作, 请参阅 BreezeFireBase 示例 ,或查看 ODataCollectionView 类的源代码.

import 'bootstrap.css'; import '@grapecity/wijmo.styles/wijmo.css'; // import * as wijmo from '@grapecity/wijmo'; // document.readyState === 'complete' ? init() : window.onload = init; // function init() { // create an empty CollectionView let view = new wijmo.CollectionView(); view.collectionChanged.addHandler(() => { // show the view's data let list = document.querySelector('#list'); list.innerHTML = ''; view.items.forEach(item => { let html = wijmo.format('<li>{CategoryID} <b>{CategoryName}</b> {Description}</li>', item); list.appendChild(wijmo.createElement(html)); }); }); // // populate it with data from a server wijmo.httpRequest('https://services.odata.org/Northwind/Northwind.svc/Categories', { data: { $format: 'json', $select: 'CategoryID,CategoryName,Description' }, success: (xhr) => { // got the data, assign it to the CollectionView let response = JSON.parse(xhr.response); let data = response.d ? response.d.results : response.value; // // use the result as the sourceCollection view.sourceCollection = data; } }); } <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <title>GrapeCity Loading Data from Server</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"> <ul id="list"> </ul> </div> </body> </html> import 'bootstrap.css'; import '@grapecity/wijmo.styles/wijmo.css'; // import * as wijmo from '@grapecity/wijmo'; // import { Component, enableProdMode, NgModule } from '@angular/core'; import { platformBrowserDynamic } from '@angular/platform-browser-dynamic'; import { BrowserModule } from '@angular/platform-browser'; // @Component({ selector: 'app-component', templateUrl: 'src/app.component.html' }) export class AppComponent { view: wijmo.CollectionView; // constructor() { // create an empty CollectionView this.view = new wijmo.CollectionView(); // // populate it with data from a server wijmo.httpRequest('https://services.odata.org/Northwind/Northwind.svc/Categories', { data: { $format: 'json', $select: 'CategoryID,CategoryName,Description' }, success: (xhr: XMLHttpRequest) => { // got the data, assign it to the CollectionView let response = JSON.parse(xhr.response); let data = response.d ? response.d.results : response.value; // // use the result as the sourceCollection this.view.sourceCollection = data; } }); } } // @NgModule({ imports: [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 Loading Data from Server</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"> <ul> <ng-template ngFor let-item [ngForOf]="view.items"> <li> {{ item.CategoryID }} <b>{{ item.CategoryName }}</b> {{ item.Description }} </li> </ng-template> </ul> </div> <template> <div class="container-fluid"> <ul> <li v-for="item in data.items" :key="item.CategoryID"> {{ item.CategoryID }} <b>{{ item.CategoryName }}</b> {{ item.Description }} </li> </ul> </div> </template> <script> import 'bootstrap.css'; import '@grapecity/wijmo.styles/wijmo.css'; import Vue from 'vue'; import * as wijmo from '@grapecity/wijmo'; new Vue({ el: '#app', data: function () { return { data: _getData() } } }) function _getData(){ var view = new wijmo.CollectionView(); // // populate it with data from a server wijmo.httpRequest('https://services.odata.org/Northwind/Northwind.svc/Categories', { data: { $format: 'json', $select: 'CategoryID,CategoryName,Description' }, success: (xhr) => { // got the data, assign it to the CollectionView let response = JSON.parse(xhr.response); let data = response.d ? response.d.results : response.value; // // use the result as the sourceCollection view.sourceCollection = data; } }); return view; } </script> <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <title>GrapeCity Loading Data from Server</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 'bootstrap.css'; import '@grapecity/wijmo.styles/wijmo.css'; import * as React from 'react'; import * as ReactDOM from 'react-dom'; import * as wijmo from '@grapecity/wijmo'; class App extends React.Component { constructor(props) { super(props); this.state = { view: new wijmo.CollectionView() }; } ; componentWillMount() { wijmo.httpRequest('https://services.odata.org/Northwind/Northwind.svc/Categories', { data: { $format: 'json', $select: 'CategoryID,CategoryName,Description' }, success: (xhr) => { // got the data, assign it to the CollectionView let response = JSON.parse(xhr.response); let data = response.d ? response.d.results : response.value; // // use the result as the sourceCollection this.setState((state) => { state.view.sourceCollection = data; return state; }); } }); } render() { return <div className="container-fluid"> {this.state.view.items.map(item => <li key={item.CategoryID}> {item.CategoryID} <b>{item.CategoryName}</b> {item.Description} </li>)} </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>