Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Filter with input box #35

Open
levipadre opened this issue Feb 2, 2015 · 6 comments
Open

Filter with input box #35

levipadre opened this issue Feb 2, 2015 · 6 comments

Comments

@levipadre
Copy link

Hi,

Thanks for the great library!
Does anybody know how can I filter properly with an input box?
The filtering is working almost perfectly, just when I delete some character (or everything) from the text from the search box, I didn't get back the proper/full list.
Here is my current code:

<div id="isotopeContainer" isotope-container>
   <div class="isotope-item {{x.class}}" ng-repeat="x in List | filter:search | orderBy: title " isotope-item>
      <p>{{x.title}}</p>
   </div>
</div>

<input type="text" ng-model="search.title" />

Thanks,
L

@joeldavuk
Copy link

I implemented it with the help of zurb's text change library and there is an existing example of search filtering on metafizzy demos, http://www.zurb.com/playground/jquery-text-change-custom-event

There is probably a cleaner way please comment if you do have an easier way of implementing this, I had the issue with the layout but after adding the debounce it worked fine.

    var $quicksearch = $('.quicksearch');

    $quicksearch.on('textchange',function() {
        if($(this).val().length == 0) {
            $scope.$emit('iso-option', {name: 'filter', params: '*'});
            $scope.$emit('iso-method', {name: 'reloadItems'});
            var s = angular.element('#isotopeContainer').scope();
            var items = [];
            s.$apply(s.posts = items);
        }
    });
    // use value of search field to filter
    $quicksearch.on('keyup', debounce(applySearch,100));

function applySearch() {

        qsRegex = new RegExp($quicksearch.val(), 'gi');

        $scope.$emit('iso-option', {
            name: 'filter', params: function () {
                return qsRegex ? $(this).text().match(qsRegex) : true;
            }
        });
        $scope.$emit('iso-method', {name: 'reloadItems'});
        var s = angular.element('#isotopeContainer').scope();
        var items = [];

        angular.forEach(orig_items, function (obj) {
            // obj.fields to search , combine into one string
            var content = obj.content.rendered + obj.title.rendered + obj.type;
            var qsRegex = new RegExp($quicksearch.val(), "gi");
            if (content.match(qsRegex)) {

                if(!obj.experience.indexOf(selectedKeyword) && selectedKeyword!= null) {
                    //do nothing
                } else {
                    items.push(obj);
                }


            }
        });
        s.apply(s.posts = items);
    }

 // debounce so filtering doesn't happen every millisecond
    function debounce(fn, threshold) {
        var timeout;
        return function debounced() {
            if (timeout) {
                clearTimeout(timeout);
            }
            function delayed() {
                fn();
                timeout = null;
            }

            timeout = setTimeout(delayed, threshold || 100);
        }
    }

@udayms
Copy link

udayms commented Nov 16, 2016

I am having a problem with this solution... Wondering how you got around this issue...

  1. I am getting the message - Uncaught TypeError: s.apply is not a function
  2. Sometimes the s.posts is missing in that object as well

Any ideas?

@joeldavuk
Copy link

Are you sure your element has the id #isotopeContainer or whatever you've named it is reflected in the js? @udayms

@udayms
Copy link

udayms commented Nov 16, 2016

Yes. and angular.element('#isotopeContainer').scope(); is returning an object.

s object:
screen shot 2016-11-16 at 12 27 58 pm

DOM
screen shot 2016-11-16 at 12 38 41 pm

@udayms
Copy link

udayms commented Nov 16, 2016

It's just the s.apply that doesn't work. I googled for this and came across angular/angular.js#9515 which says I should set $compileProvider.debugInfoEnabled(false) to true for that to work. Tried that. Didn't work.

@udayms
Copy link

udayms commented Nov 16, 2016

Even changing it to $apply didn't work. Changing to $apply stops the console errors. But, then the list is filtered.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants