如何在Ionic 2中实现jQuery列表过滤器

问题描述 投票:0回答:1

我有这个jQuery函数,用于从HTML过滤数据。我已经从网上找到了这段代码。我需要在Ionic的应用程序中实现类似的功能。问题是我不知道我需要在list-search-min.js中进行哪些更改。

下面是我的index.html代码:

<!doctype html>
<html>
<head>

    <link href="http://www.jqueryscript.net/css/jquerysctipttop.css" rel="stylesheet" type="text/css">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/materialize/0.97.6/css/materialize.min.css">
    <link rel="stylesheet" href="app.css">

    </head>

<body>

    <div class="container" style="margin-top:150px;">

        <h1>jQuery list-search Plugin Demo</h1>

        <input id="search-box" type="text" class="validate" autofocus autocomplete="off">

        <ul class="collection">

        <li class="collection-item">
                <a href="">
                    saurabh
                </a>
            </li>


            <li class="collection-item">
                <a href="http://500px.com">
                    500px
                </a>
            </li>
            <li class="collection-item">
                <a href="http://deviantart.com">
                    DeviantArt
                </a>
            </li>
            <li class="collection-item">
                <a href="http://soundcloud.com">
                    SoundCloud
                </a>
            </li>
            <li class="collection-item">
                <a href="http://store.steampowered.com">
                    Steam
                </a>
            </li>
            <li class="collection-item">
                <a href="http://unsplash.com">
                    Unsplash!
                </a>
            </li>
            <li class="collection-item">
                <a href="http://materializecss.com">
                    Materialize
                </a>
            </li>
            <li class="collection-item">
                <a href="https://coolors.co/">
                    Coolers
                </a>
            </li>
            <li class="collection-item">
                <a href="https://www.google.com/fonts">
                    Google Fonts
                </a>
            </li>
            <li class="collection-item">
                <a href="https://nofilmschool.com">
                    No Film School
                </a>
            </li>
            <li class="collection-item">
                <a href="http://videocopilot.net">
                    Video Copilot
                </a>
            </li>
            <li class="collection-item">
                <a href="http://fractalsponge.net">
                    Fractalsponge 3D Art
                </a>
            </li>
            <li class="collection-item">
                <a href="http://wikipedia.org">
                    Wikipedia
                </a>
            </li>
            <li class="collection-item">
                <a href="http://starwars.wikia.com/">
                    Wookiepedia
                </a>
            </li>
            <li class="collection-item">
                <a href="http://jedipedia.wikia.com/">
                    Jedipedia
                </a>
            </li>
            <li class="collection-item">
                <a href="http://oliverschwendener.ch">
                    Oliver Schwendener (Website)
                </a>
            </li>
            <li class="collection-item">
                <a href="http://github.com/oliverschwendener">
                    Oliver Schwendener (GitHub)
                </a>
            </li>
            <li class="collection-item">
                <a href="http://google.com">
                    Google
                </a>
            </li>
            <li class="collection-item">
                <a href="http://youtube.com">
                    YouTube
                </a>
            </li>
        </ul>
        <div class="no-apps-found">
            No Apps Found
        </div>

    </div>

    <script src="https://code.jquery.com/jquery-1.12.4.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/materialize/0.97.6/js/materialize.min.js"></script>
    <script src="js/list-search-min.js"></script>
    <script>
        initializeListSearch({
            toggleAnimationSpeed: 0,
            openLinkWithEnterKey: true,
            itemSelector: '.collection-item',
            searchTextBoxSelector: '#search-box',
            noItemsFoundSelector: '.no-apps-found'
        });
    </script>
</body>
</html>

list-search-min.js代码:

function initializeListSearch(e) {
    return void 0 === e || null === e ? void setDefaultValues() : (toggleAnimationSpeed = "undefined" != typeof e.toggleAnimationSpeed ? e.toggleAnimationSpeed : 250, cssActiveClass = "undefined" != typeof e.cssActiveClass ? e.cssActiveClass : "active", itemSelector = "undefined" != typeof e.itemSelector ? e.itemSelector : ".collection-item", openLinkWithEnterKey = "undefined" != typeof e.openLinkWithEnterKey && e.openLinkWithEnterKey, searchTextBoxSelector = "undefined" != typeof e.searchTextBoxSelector ? e.searchTextBoxSelector : "#search-box", void(noItemsFoundSelector = "undefined" != typeof e.noItemsFoundSelector ? e.noItemsFoundSelector : ".no-apps-found"))
}

function setDefaultValues() {
    toggleAnimationSpeed = 250, itemSelector = ".collection-item", cssActiveClass = "active", openLinkWithEnterKey = !1, searchTextBoxSelector = "#search-box", noItemsFoundSelector = ".no-apps-found"
}

function searchListItems(e) {
    return "" === e ? (resetSearch(), void $(noItemsFoundSelector).hide()) : (foundItems = findItemsInList(e), void(foundItems.length > 0 && openLinkWithEnterKey ? (foundItems[0].addClass(cssActiveClass), $(noItemsFoundSelector).hide()) : $(noItemsFoundSelector).show()))
}

function resetSearch() {
    $(itemSelector).slideDown(toggleAnimationSpeed), $(itemSelector).removeClass(cssActiveClass), foundItems = $(itemSelector)
}

function findItemsInList(e) {
    for (var t = $(itemSelector), s = [], o = 0; o < t.length; o++) {
        var n = t[o];
        $(n).removeClass(cssActiveClass), $(n).children("a").html().toLowerCase().indexOf(e.toLowerCase()) < 0 ? $(n).slideUp(toggleAnimationSpeed) : (s.push($(n)), $(n).slideDown(toggleAnimationSpeed))
    }
    return s
}

function selectNextItem(e) {
    if (openLinkWithEnterKey) {
        var t = $(itemSelector + "." + cssActiveClass);
        if (0 === t.length) "next" === e && $(foundItems[0]).addClass(cssActiveClass), "prev" === e && $(foundItems[foundItems.length - 1]).addClass(cssActiveClass);
        else {
            var s = $(itemSelector + "." + cssActiveClass);
            s.removeClass(cssActiveClass), "next" === e && s.nextAll(itemSelector + ":visible").first().addClass(cssActiveClass), "prev" === e && s.prevAll(itemSelector + ":visible").first().addClass(cssActiveClass)
        }
    }
}

function openLink() {
    if (openLinkWithEnterKey) {
        var e = $(itemSelector + "." + cssActiveClass).first().children("a").attr("href");
        void 0 === e && null !== e || (document.location.href = e)
    }
}
var toggleAnimationSpeed, itemSelector, foundItems, cssActiveClass, openLinkWithEnterKey, searchTextBoxSelector, noItemsFoundSelector;
$(document).ready(function() {
    resetSearch(), $(searchTextBoxSelector).bind("input propertychange", function() {
        searchListItems($(this).val())
    }), $(searchTextBoxSelector).keydown(function(e) {
        40 === e.keyCode && selectNextItem("next"), 38 === e.keyCode && selectNextItem("prev"), 13 === e.keyCode && openLink()
    })
});

我希望它从以下位置搜索数据:

<ion-list>
<ion-item> product 1 <ion-item>
<ion-item> product 2 <ion-item>
<ion-item> product 3 <ion-item>
...so on
</ion-list>

我需要在list-search-min.js中进行什么更改,以便它从离子项目而不是从离子项目中搜索数据?

<li class="collection-item">
                <a href="">
                    saurabh
                </a>
            </li>

脚本的实时工作链接-https://www.jqueryscript.net/demo/Easy-jQuery-Client-side-List-Filtering-Plugin-list-search/

javascript jquery angular ionic-framework ionic2
1个回答
1
投票

这是完全定制的

((您可以使用所需的任何Ionic元素)+(您可以根据需要修改搜索过滤器)

模板侧:

<ion-list>
    <ion-item>
        <ion-label floating>Username</ion-label>
        <ion-input type="text" [(ngModel)]='searchString'></ion-input>
    </ion-item>
</ion-list>
<ion-list *ngFor='let user of ( users|myfilter : searchString)'>
    {{ user.name }}
</ion-list>

自定义管道:

import {Injectable, Pipe , PipeTransform} from '@angular/core';

@Pipe({ name: 'myfilter' })
export class MyFilterPipe implements PipeTransform {
    transform(users: any[], args): any {
        return users.filter(user => user.name.toLowerCase().includes(args.toLowerCase()) );
    }
}

请查看工作演示:

https://stackblitz.com/edit/ionic-search-list

© www.soinside.com 2019 - 2024. All rights reserved.