【问题标题】:Implementing Search in a Pre-Populated database with Ionic 2使用 Ionic 2 在预先填充的数据库中实现搜索
【发布时间】:2017-02-16 02:45:54
【问题描述】:

我目前正在开发一个带有缩写词搜索功能工具的应用程序。我已经成功地显示了预填充的数据库,因为它应该使用 SQLite-Ext 插件但在如何实现所需的搜索功能方面遇到了困难。在 Ionic 2 网站上,有关于如何使用内置“离子搜索栏”https://ionicframework.com/docs/v2/components/#searchbar 的说明,但我看不出它如何连接到应用程序上的预填充数据库。

home.html-

<ion-content padding>
    <ion-searchbar (ionInput)="queryAcronyms($event)"></ion-searchbar>
    <ion-list>
        <ion-item *ngFor="let name of acronyms; let nIndex = index">
        <h2>{{ name.name }}</h2>
        <p>{{ name.meaning }}</p>
    </ion-item>
    </ion-list>
</ion-content>

home.ts-

import { Component } from '@angular/core';

import { NavController } from 'ionic-angular';
import { SQLite } from 'ionic-native';

@Component({
  selector: 'page-home',
  templateUrl: 'home.html'
})
export class HomePage {
  public storage: SQLite;
  private options = { name: "data7.db", location: 'default', createFromLocation: 1 };
  private queryAcronyms = "SELECT * FROM acronymsFinal";
  public acronyms: any = [];

  constructor(public navCtrl: NavController) {
    this.storage = new SQLite();
    this.storage.openDatabase(this.options).then((success) => {
      this.storage.executeSql(this.queryAcronyms, {}).then((data) => {
        let rows = data.rows;
        for (let i = 0; i < rows.length; i++) {
            this.acronyms.push({
                name: rows.item(i).name,
                meaning: rows.item(i).meaning
            });
        }
      });
    });
  }
}

理想情况下,我需要一个位于页面顶部的搜索栏(正如我已经输入的那样),它允许您按首字母缩略词名称过滤数据。我也尝试过浏览其他各种搜索教程,但这些似乎都是针对 Ionic 1 或您在代码中填充的数据库。

我对 Ionic 和数据库控制还很陌生,因此非常感谢任何帮助!

【问题讨论】:

    标签: database sqlite cordova ionic2


    【解决方案1】:
    Create a new function with queryAcronyms name like below and filter your data according search query and reinitialize your array
    
    public queryAcronyms(){
        let temparray  =  [];
        temparray =  this.acronyms.filter((item) => {
            return item.name.toLowerCase().indexOf(this.queryText.toLowerCase()) > -1;
        });
        this.acronyms = temparray;
    }
    

    【讨论】:

    • 我不确定这是不是没有解释的东西,但我无法让您的代码正常工作。它不断提出“预期的声明或声明”并且无法构建。另外,考虑到我从未定义过,它是否意味着“queryText”?
    • 很抱歉没有提到queryText,你可以在你的HomePage类中定义它,比如公共首字母缩略词:any = [];查询文本 = '';然后将此 queryText 与您的视图模板文件中的搜索栏绑定 searchbar> 希望对你有帮助
    • 感谢您迄今为止的所有帮助。我们现在已经到了这一点,它可以毫无错误地构建,但搜索仍然没有做任何事情。我怀疑这只是与代码的放置有关。
    • 我无法理解的一件事是我应该如何将它与前面的代码一起使用。我不能像它所说的那样重复使用“public queryAcronyms”吗?
    猜你喜欢
    • 2018-02-01
    • 1970-01-01
    • 2021-06-30
    • 1970-01-01
    • 2016-10-24
    • 2022-07-07
    • 1970-01-01
    • 2013-03-08
    • 1970-01-01
    相关资源
    最近更新 更多