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?
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.
-
Thanks for your fast response Jeff, that's worked perfectly!
-
Thank you! I love how simple this is.
-
it worked like a magic, Thanks.
-
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 });