你好,textChanged的确不行,首先如你所说它的触发顺序在itemsSource之前,其次这个示例是远程加载数据,所以这里还有一个异步请求。因此要实现这个功能,只能在异步请求内部来做。
参考以下完整代码:
- import 'bootstrap.css';
- import '@grapecity/wijmo.styles/wijmo.css';
- import './styles.css';
- //
- import * as wijmo from '@grapecity/wijmo';
- import * as input from '@grapecity/wijmo.input';
- //
- document.readyState === 'complete' ? init() : window.onload = init;
- //
- function init() {
- // AutoComplete with async search using OData source
- let theAutoComplete = new input.AutoComplete('#theAutoComplete', {
- placeholder: 'Product Name',
- displayMemberPath: 'ProductName',
- itemsSourceFunction: (query, max, callback) => {
- if (!query) {
- callback(null);
- return;
- }
- //
- wijmo.httpRequest('https://services.odata.org/Northwind/Northwind.svc/Products', {
- data: {
- $format: 'json',
- $select: 'ProductID,ProductName',
- $filter: 'indexof(ProductName, \'' + query + '\') gt -1'
- },
- success: (xhr) => {
- let response = JSON.parse(xhr.response);
- let value = response.value;
- if(value){
- value.forEach(function(val){
- if(query == val.ProductName){
- setTimeout(function(){
- theAutoComplete.selectedItem = val;
- if(theAutoComplete.isDroppedDown){
- theAutoComplete.isDroppedDown = false;
- }
- }, 10);
- }
- });
- }
- callback(response.d ? response.d.results : response.value);
- }
- });
- },
- selectedIndexChanged: function () {
- let product = theAutoComplete.selectedItem;
- document.querySelector('#msg').textContent = product
- ? wijmo.format('{ProductName} (ID: {ProductID})', product)
- : 'None';
- },
- lostFocus: function (e) {
- if(!e.text || e.text.length == 0){
- e.listBox.itemsSource.items.splice(0)
- e.listBox.itemsSource.refresh()
- }
- }
- });
- }
复制代码
示例地址:
https://demo.grapecity.com.cn/wi ... AsyncLoading/purejs |