无限列表框

此示例显示当用户向下滚动到列表底部附近时,如何使用本机HTML滚动事件将项添加到ListBox。

要查看其工作原理,请使用鼠标或键盘向下滚动列表:

import 'bootstrap.css'; import '@grapecity/wijmo.styles/wijmo.css'; import './styles.css'; // import * as wjInput from '@grapecity/wijmo.input'; import { getData } from './data'; // document.readyState === 'complete' ? init() : window.onload = init; // function init() { // the ListBox let theListBox = new wjInput.ListBox('#theListBox', { itemsSource: getData() }); // // add elements when the user scrolls to the bottom of the list theListBox.hostElement.addEventListener('scroll', e => { let el = e.target; // // near the bottom? if (el.scrollTop + el.clientHeight > el.scrollHeight - 20) { // // add five elements to the list let arr = theListBox.itemsSource; for (let i = 0; i < 5; i++) { arr.push('item ' + arr.length); } // // refresh the list and keep the scroll top let top = el.scrollTop; theListBox.collectionView.refresh(); el.scrollTop = top; // // show how many items we have let cnt = document.querySelector('#count'); cnt.textContent = `The list now has ${arr.length} items.`; } }); } <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <title>GrapeCity Infinite ListBox</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="theListBox"></div> <p id="count"> </p> </div> </body> </html> // get some data export function getData() { let arr = []; for (let i = 0; i < 20; i++) { arr.push('item ' + i); } return arr; } .wj-listbox { height: 200px; width: 200px; margin-left: 2em; } .wj-listbox-item { line-height: 2.5em; } body { margin-bottom: 24pt; } import 'bootstrap.css'; import '@grapecity/wijmo.styles/wijmo.css'; import './styles.css'; // import * as input from '@grapecity/wijmo.input'; // import { Component, enableProdMode, NgModule, Inject } from '@angular/core'; import { platformBrowserDynamic } from '@angular/platform-browser-dynamic'; import { BrowserModule } from '@angular/platform-browser'; import { WjInputModule } from '@grapecity/wijmo.angular2.input'; import { DataService } from './app.data'; // @Component({ selector: 'app-component', templateUrl: 'src/app.component.html' }) export class AppComponent { data: any; // constructor(@Inject(DataService) private dataService: DataService) { this.data = dataService.getData(); } // setupScrolling(ctrl: input.ListBox) { // add elements when the user scrolls to the bottom of the list ctrl.hostElement.addEventListener('scroll', e => { let el = <Element>e.target; // // near the bottom? if (el.scrollTop + el.clientHeight > el.scrollHeight - 20) { // // add five elements to the list let arr = ctrl.itemsSource; for (let i = 0; i < 5; i++) { arr.push('item ' + arr.length) } // // refresh the list and keep the scroll top let top = el.scrollTop; ctrl.collectionView.refresh(); el.scrollTop = top; // // show how many items we have let cnt = document.querySelector('#count'); cnt.textContent = `The list now has ${arr.length} items.`; } }); } } // @NgModule({ imports: [WjInputModule, 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 Infinite ListBox</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-list-box #theListBox [itemsSource]="data" (initialized)="setupScrolling(theListBox)"></wj-list-box> <p id="count"> </p> </div> import { Injectable } from '@angular/core'; // @Injectable() export class DataService { getData() { let arr = []; for (let i = 0; i < 20; i++) { arr.push('item ' + i); } return arr; } } .wj-listbox { height: 200px; width: 200px; margin-left: 2em; } .wj-listbox-item { line-height: 2.5em; } body { margin-bottom: 24pt; } <template> <div class="container-fluid"> <wj-list-box :itemsSource="data" :initialized="setupScrolling"></wj-list-box> <p id="count"> </p> </div> </template> <script> import 'bootstrap.css'; import '@grapecity/wijmo.styles/wijmo.css'; import Vue from 'vue'; import '@grapecity/wijmo.vue2.core'; import '@grapecity/wijmo.vue2.input'; import { getData } from './data'; let App = Vue.extend({ name: 'app', data: function () { return { data: getData() } }, methods: { setupScrolling: function(ctrl){ // add elements when the user scrolls to the bottom of the list ctrl.hostElement.addEventListener('scroll', e => { let el = e.target; // // near the bottom? if (el.scrollTop + el.clientHeight > el.scrollHeight - 20) { // // add five elements to the list let arr = ctrl.itemsSource; for (let i = 0; i < 5; i++) { arr.push('item ' + arr.length) } // // refresh the list and keep the scroll top let top = el.scrollTop; ctrl.collectionView.refresh(); el.scrollTop = top; // // show how many items we have let cnt = document.querySelector('#count'); cnt.textContent = `The list now has ${arr.length} items.`; } }); } } }) let vm = new Vue({ render: h => h(App) }).$mount('#app'); </script> <style> .container-fluid .wj-listbox { height: 200px; width: 200px; margin-left: 2em; } .container-fluid .wj-listbox-item { line-height: 2.5em; } body { margin-bottom: 24pt; } </style> <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <title>GrapeCity Infinite ListBox</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> // get some data export function getData() { let arr = []; for (let i = 0; i < 20; i++) { arr.push('item ' + i); } return arr; } 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 wjInput from '@grapecity/wijmo.react.input'; import { getData } from './data'; // class App extends React.Component { constructor(props) { super(props); this.setupScrolling = (ctrl) => { // add elements when the user scrolls to the bottom of the list ctrl.hostElement.addEventListener('scroll', e => { let el = e.target; // // near the bottom? if (el.scrollTop + el.clientHeight > el.scrollHeight - 20) { // // add five elements to the list let arr = ctrl.itemsSource; for (let i = 0; i < 5; i++) { arr.push('item ' + arr.length); } // // refresh the list and keep the scroll top let top = el.scrollTop; ctrl.collectionView.refresh(); el.scrollTop = top; // // show how many items we have let cnt = document.querySelector('#count'); cnt.textContent = `The list now has ${arr.length} items.`; } }); }; this.state = { data: getData() }; } ; render() { return <div className="container-fluid"> <wjInput.ListBox initialized={this.setupScrolling} itemsSource={this.state.data}> </wjInput.ListBox> <p id="count"></p> </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-listbox { height: 200px; width: 200px; margin-left: 2em; } .wj-listbox-item { line-height: 2.5em; } body { margin-bottom: 24pt; } export function getData() { let arr = []; for (let i = 0; i < 20; i++) { arr.push('item ' + i); } return arr; }