Titanium Community Questions & Answer Archive

We felt that 6+ years of knowledge should not die so this is the Titanium Community Questions & Answer Archive

No search results in Tableview

I'm having a little trouble searching a table with the iPhone simulator, using code based upon the Kitchen Sink example.

The table displays using data loaded remotely from my website and displays a tableview. The search bar displays at the top and behaves as I would expect, except that it matches nothing, no results show up, and when I clear the field the results return as I would expect.

I have pasted my code below…

var data = [];

var xhr = Ti.Network.createHTTPClient();
xhr.open("GET","http://api.example.com/products/");
xhr.onload = function() {
    try {
        var doc = this.responseXML.documentElement;
        var items = doc.getElementsByTagName("product");
        var x = 0;

        for (var c=0;c<items.length;c++) {
            var item = items.item(c);
            var widgetname = item.getElementsByTagName("name").item(0).text;

            var row = Ti.UI.createTableViewRow({
                height:40,
                borderWidth:1,
                hasChild:1
            });

            var label = Ti.UI.createLabel({
                text:widgetname,
                left:5,
                top:5,
                bottom:5,
                right:5
            });

            row.add(label);

            data[x++] = row;
            row.url = "http://www.example.com/iphone/" + item.getElementsByTagName("urlformat").item(0).text;
        }

        var search = Titanium.UI.createSearchBar(
            {
                barColor:'#BBB',
                showCancel:false
            }
        );

        search.addEventListener('change', function(e) {
           e.value; // search string as user types
        });
        search.addEventListener('return', function(e) {
           search.blur();
        });
        search.addEventListener('cancel', function(e) {
           search.blur();
        });

        var tableview = Titanium.UI.createTableView(
            {
                data:data,
                search:search,
                filterAttribute:'widgetname'
            }
        );

        Titanium.UI.currentWindow.add(tableview);

        tableview.addEventListener('click',function(e) {
            var w = Ti.UI.createWindow({title:e.row.title});
            var wb = Ti.UI.createWebView({url:e.row.url});

            w.add(wb);
            var b = Titanium.UI.createButton({
                title:'Back',
                style:Titanium.UI.iPhone.SystemButtonStyle.PLAIN
            });
            w.setLeftNavButton(b);
            b.addEventListener('click',function() {
                w.close();
            });
            w.open({modal:true});
        });
    }
    catch(E) {
        alert(E);
    }
};

xhr.send();

…can anyone suggest what I'm missing? I'm sure it's something simple that I've overlooked! The filterAttribute value in createTableView is my first suspect, but the documentation on what this should equal is a bit limited.

Does anyone have any suggestions?

— asked March 12th 2010 by Chris Tebbit
  • searchbar
  • tableview
0 Comments

5 Answers

  • Accepted Answer

    You need to set the filterAttribute property to the value of the row property you want to search against.

    In your code above, you could add a property named widgetname to the row like this:

    var row = Ti.UI.createTableViewRow({
            height:40,
            borderWidth:1,
            hasChild:1,
            widgetname:widgetname
        });
    

    That will allow us to find the row property passed in and use it for filtering.

    — answered March 12th 2010 by Jeff Haynie
    permalink
    3 Comments
    • Hi jeff.. I have a query. As i can see we've used filterAttribute in a tableView for search. I wanted to know, can this property be changed to some other value on any kind of event, say a click of a button…??
      Like we click on a button and set " tableView.filterAttribute = 'someOtherValue' " so that now the search will be for 'someOtherValue' instead of the previous value.

      — commented August 31st 2012 by Anuj Vashistha
    • I think, It would not work.

      — commented August 31st 2012 by Shalini Kushwaha
    • Hello Shalini, thanks for the response…but, i would like to tell you that this thing does work in iphone but not in android. I've already tried to do the same thing in android a lot of times adopting many alternatives but it is not working. Any information regarding the same would be a great help.. :)

      — commented September 3rd 2012 by Anuj Vashistha
  • Thanks for your fast response Jeff, that's worked perfectly!

    — answered March 12th 2010 by Chris Tebbit
    permalink
    0 Comments
  • Thank you! I love how simple this is.

    — answered July 27th 2011 by Nick Robillard
    permalink
    0 Comments
  • it worked like a magic, Thanks.

    — answered February 20th 2013 by mandar ramdas
    permalink
    0 Comments
  • Because we are using the searchbar to initiate search, not filter existing rows, what worked great for me was:

    var searchBar = Titanium.UI.createSearchBar({….});

    …add searchBar event handlers

    var tv = Titanium.UI.createTableView({…., filterAttribute:'myfiltervalue' });

    ….query remote data.

    var tvRow = Titanium.UI.createTableViewRow({…., myfiltervalue:searchBar.value });

    — answered October 3rd 2013 by Joe Newport
    permalink
    0 Comments
The ownership of individual contributions to this community generated content is retained by the authors of their contributions.
All trademarks remain the property of the respective owner.