Skip to content

Instantly share code, notes, and snippets.

@sbhimavarapuAppc
Created September 27, 2011 17:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sbhimavarapuAppc/1245756 to your computer and use it in GitHub Desktop.
Save sbhimavarapuAppc/1245756 to your computer and use it in GitHub Desktop.
SearchBar to search over a table - Not included as a table searchBar
var isSearchPresent = false;
var data = [
{title:'A - Server 1', header:'Servers'},
{title:'B - Server 2'},
{title:'C - Server 3'},
{title:'A - WorkStation 1', header:'WorkStations'},
{title:'B - WorkStation 2'},
{title:'C - WorkStation 3'},
{title:'D - WorkStation 4'},
{title:'E - WorkStation 5'},
]
var newdata = [];
var search = Titanium.UI.createSearchBar({
hintText:'search hostname',
top: 0,
height: 40
});
// create table view
var tableview = Titanium.UI.createTableView({
data:data,
filterAttribute:'title'
});
var win = Ti.UI.createWindow({
backgroundColor : 'white',
fullscreen: false,
activity : {
onCreateOptionsMenu : function(e) {
var menu = e.menu;
var m1 = menu.add({ title : 'Search' });
m1.addEventListener('click', function() {
if(isSearchPresent){
tableview.top = 0;
search.hide();
}
else{
tableview.top = 40;
search.show();
}
isSearchPresent = !isSearchPresent;
});
}
}
});
win.add(search);
win.add(tableview);
win.addEventListener('open', function(){
search.hide();
tableview.top = 0;
});
search.addEventListener('change', function(e){
if(isSearchPresent){
var tempstr = e.value;
newdata = [];
for (var i=0, il=data.length; i<il; i++){
Ti.API.info(data[i]['title'].indexOf(tempstr));
if(data[i]['title'].indexOf(tempstr) != -1){
newdata.push(data[i]);
}
}
tableview.data = newdata;
}
});
search.addEventListener('return', function(e){
Ti.API.info('Implement return');
});
win.open();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment