Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7667741
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T15:10:56+00:00 2026-05-31T15:10:56+00:00

I have a simple model object with one array property. Example: {name: ‘Foo’, tags

  • 0

I have a simple model object with one array property.

Example:

{name: 'Foo', tags = ['fun', 'cool', 'geek']};

I add more one of these models to an obervableArray in my view model.

//pseudo code
oa.add({name: 'Foo', tags: ['fun', 'cool', 'geek']});
oa.add({name: 'Bar', tags: ['sad', 'dorky', 'uncool']});
oa.add({name: 'Qwerty', tags: ['keys', '101', 'geek']});

Now when I filter an item based on a tag I would like to display a message that there are no more items with a certain tag.

Filter code:

// self = this;
self.filter = ko.observable('');
self.filterItems = ko.dependentObservable (function() {
    var filter = this.filter();
    if (!filter) {
        return this.items();
    } else {
        return ko.utils.arrayFilter(this.items(), function(item) {
                try {
                    if (compareAssociativeArrays(item.tags, filter)) {
                        return true;
                    }
                } catch (e) {}
                self.items.remove(item);
        });
    }
}, this);

Is it possible to data-bind a given length of items with an indexOf a tag value ?

UPDATE

I did come up with a solution, but not sure if best. With it I can modify and retrieve totals as well:

self.hasGeek = ko.computed(function () {
    var sum = 0;
    var item;
    for (var i=0; i<self.items().length; i++) {
        var item = self.items()[i];
        if (item.tags().indexOf('geek') != -1) {
            sum++;
        }
    }
    return (sum > 0) ? true : false;
});
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T15:10:57+00:00Added an answer on May 31, 2026 at 3:10 pm

    I am not sure about your exact structure, but I would setup a computed observable to represent your filtered items. Then, you can include a section that has its visibility controlled by the length of the filtered items computed observable.

    Would be like: http://jsfiddle.net/rniemeyer/aVtpc/

    Tag Filter: <input data-bind="value: tagFilter" />
    
    <hr/>
    
    <div data-bind="visible: !filteredItems().length">
        No items found
    </div>
    
    <ul data-bind="foreach: filteredItems">
        <li data-bind="text: name"></li>
    </ul>​
    

    js:

    var ViewModel = function() {
        this.tagFilter = ko.observable();
        this.items = ko.observableArray([
            {name: 'Foo', tags: ['fun', 'cool', 'geek']},
            {name: 'Bar', tags: ['sad', 'dorky', 'uncool']},
            {name: 'Qwerty', tags: ['keys', '101']}
        ]);
    
        this.filteredItems = ko.computed(function() {
            var filter = this.tagFilter();
    
            if (!filter) {
               return this.items();   
            }
    
            return ko.utils.arrayFilter(this.items(), function(item) {       
                return ko.utils.arrayFirst(item.tags, function(tag) {
                      return tag === filter;         
                });                
            });
        }, this);
    };
    
    ko.applyBindings(new ViewModel());
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a simple model like this one: class Artist(models.Model): surname = models.CharField(max_length=200) name
I have an object model like that: Folder - simple structure with name etc.
I have a simple question regarding accessing member variables of a model object. I
Supose we have a very simple model: Station has at least one Train Train
For example, if I have a simple model with a time attribute named start_time
I have a simple model, consisting of a document that references one or more
I have a simple model class (Part), which pulls from it's information from a
I have a simple model containing a FileField among others. When I use a
I have a simple model from simulink and I would like to generate code
I have a simple data model of two tables, email and recipients, email can

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.