Titanium Community Questions & Answer Archive

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

Keep Searchbar at the top of the Tableview when Scrolling

I would like to keep the searchbar visible at the top of a tableview when users scroll, is this possible? Right now as you scroll down through the items, the searchbar at the top scrolls with the items, so if i have a long list and scroll to the bottom and want to search, i have to scroll all the way back up to the top to get to the search bar.

— asked March 18th 2011 by B Ben
  • searchbar
  • tableview
0 Comments

4 Answers

  • Just if anyone still needs this, (as i could'nt find an answer to that)

    You can set the searchHidden propertie of the TableView to true
    and instead add the SearchBar directly to the TableView with the searchBars top propertie set to 0

    This works for me, however it seems to be a bit buggy, sometimes rows show duplicated and/or the transparent view the searchbar adds on focus is moved down for a row ( which is then clickable)
    i also had many empty rows being displayed one time;

    anyway if someones going to give it a try

    Maybe someone finds a way round these bugs

    var win = Ti.UI.createWindow({
        title:"Search Test"
    })
    
    var table = Ti.UI.createTableView({})
    
    var search = Ti.UI.createSearchBar({})
    
    table.setData([{title:"A"},{title:"B"},{title:"C"},{title:"D"},{title:"E"},{title:"F"}])
    
    table.searchHidden = true;
    table.search = search;
    search.top = 0;
    table.add(search);
    
    win.add(table)
    win.open()
    
    — answered October 30th 2012 by Moritz Roessler
    permalink
    0 Comments
  • you could add the search bar to the tableViewHeader and then it will always be at the top of the table

    — answered March 18th 2011 by Aaron Saunders
    permalink
    2 Comments
    • Thanks Aaron, I will give this a shot!

      — commented March 18th 2011 by B Ben
    • Aaron, I got the search added to the viewHeader, however, it appears that the tableview viewHeader doesn't stay at the top. To confirm this, I ended up experimenting with a label as well, this label will be added to the viewHeader, but it scrolls with the tableview which I don't want…see here:

      var headerLabel = Ti.UI.createLabel({
          text:'Here is some text for the header label.',
          height:25,
          left:10,
          right:10
      });
      
      var tvHeader = Titanium.UI.createView({
          width: '100%',
          height: 30,
          top: 0
      });
      
      tvHeader.add(headerLabel);
      
      //Setup tableview
      var tv = Titanium.UI.createTableView({
          headerView:tvHeader,
          search:search,
          filterAttribute: 'filter',
          top:0,
          width:WindowWidth
      });
      win.add(tv);
      

      — commented March 21st 2011 by B Ben
  • I think this actually works, not using the headerView but creating the search (or for my example here, the label) in the view and then positioning it above the tableview.

    var headerLabel = Ti.UI.createLabel({
        text:'Here is some text for the header label.',
        height:25,
        left:10,
        right:10
    });
    
    
    //Setup view header where the searchbar will below
    var tvHeader = Titanium.UI.createView({
        width: '100%',
        height: 30,
        top: 0
    });
    tvHeader.add(headerLabel);
    win.add(tvHeader);
    
    //Setup tableview
    var tv = Titanium.UI.createTableView({
        //headerView:tvHeader,
        search:search,
        filterAttribute: 'filter',
        top:30,
        width:WindowWidth
    });
    win.add(tv);
    
    — answered March 21st 2011 by B Ben
    permalink
    0 Comments
  • I wouldn't want to open a new Question thread, as I have tried the above:
    1- Inserting a SearchBar in a View, then apply that to the headerView of the TableView.
    2- I tried playing with the top:0 of the searchBar.

    I cannot seem to find an example for Locking the search bar to the top of the TableView.

    Here is what I have:

        var tableData = [];
    
        var tableView = Ti.UI.createTableView({
            top:0, left: 0, right: 0, bottom: 0,
            backgroundColor: '#2B0014',
        });
    
        var searchBar = Titanium.UI.createSearchBar({
            hintText: "Search by Country",
            barColor:'#000', 
            showCancel:true,
            height:43,
            top:0,
        });
    
        var Cloud = require('ti.cloud');
    
        Cloud.Places.query({
            page: 1,
            per_page: 140,
            order: 'name'
        }, function (e) {
            if (e.success) {
                //~~~ LIST the Places in a Table
                for(var i = 0; i < e.places.length; i++){
                    var place = e.places[i];
                    var row = Titanium.UI.createTableViewRow(dataObject: place);
                    var label = Ti.UI.createLabel();
                    row.add(label);
    
                    tableData.push(row);
                }
                tableView.data = tableData;
    
                tableView.search = searchBar;
                tableView.searchHidden = false;
                tableView.filterAttribute = 'country';
    ...
    
    — answered October 27th 2012 by Joseph Sachs
    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.