分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 前端开发

ionic 实现类似于JQuery的AutoComplete

发布时间:2023-09-06 02:01责任编辑:白小东关键词:暂无标签

该实例以 MongoDB + Node.js  的 Restful API Service为后台。    目前仅仅放一些代码片段,后期会将整个Demo Code上传到GitHub,待上传之后,会在本文中更新链接。

1. 后台实现:

‘use strict‘;

var mongoose = require(‘mongoose‘),

  Community = mongoose.model(‘community‘);

  var communityCtrl = require(‘../controllers/communityController‘);

exports.find_communities = function (req, res) {

  Community.find({ name: communityCtrl.fuzzyMatch(req.query.name) }, function (err, task) {

    if (err)

      res.send(err);

    res.json(task);

  });

};

  //使用正则表达式模糊匹配

  exports.fuzzyMatch =    function(source) {

    let regexSearchString = ‘\.*‘ + source + ‘\.*‘;

    return new RegExp(regexSearchString, ‘i‘);

  }

2.前端

2.1  RestSerice.ts 中调用后台 

import { HttpClient, HttpParams } from ‘@angular/common/http‘;

import { AppSettings } from ‘../../settings/app-settings‘;

import { Injectable } from ‘@angular/core‘;

@Injectable()

export class AutoCompleteServiceProvider {

  apiUrl: string;

  constructor(public http: HttpClient) {

    this.apiUrl = AppSettings.getAPIServiceURL();

  }

  getResults(keyword) {

    const params = new HttpParams().append("name", keyword);

    return new Promise(resolve => {

      this.http.get(this.apiUrl + ‘/findcommunity‘, { params: params }).subscribe(data => {

        resolve(data);

      }, err => {

        console.log(‘findcommunity‘ + err.message);

      });

    });

  }

}

2.2  前端页面

community-select.html 代码中,使用 ion-searchbar 组件

< ion-searchbar (ionInput)="searchTextChange($event)" [placeholder]="promptmsg"

name="searchQuery" [(ngModel)]="searchQuery" #fieldsearch="ngModel" required > </ion-searchbar>

community-select.ts 代码片段。

  hideList: boolean;

  coms: any;

  scrollClass: string;

searchTextChange(ev: any) {

    if (ev.target.value.length > 1) {

      this.scrollClass = "scroll-max scroll-y";

      this.hideList = false;

      this.autoService.getResults(ev.target.value).then(x => {

        this.coms = x;

        this.coms.forEach(element => {

          JSON.stringify(element);

        });

      });

    } else {

      this.scrollClass = "scroll-min";

    }

  }

ionic 实现类似于JQuery的AutoComplete

原文地址:https://www.cnblogs.com/caiyaming/p/9222228.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved