Titanium Community Questions & Answer Archive

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

Search + Pulled JSON Data?

So I am pulling data from a remote MySQL Database through a .PHP file that parses the data as a JSON String through the App. What I need is a SearchBar that will search through the ENTIRE DATABASE through several fields (Plain text fields) and display whatever returns. Here is what I have so far:

http://pastie.org/private/kjsgqbsdqg2idradjnacw

Right now it's in a TableView that displays the 8 last rows in the table, and if you scroll down you can see the next 8 and then the next 8 and so on so forth. What is happening is that if you use the SearchBar, it will only search the rows that are showing up. So if there are 8, it will only search those 8. What I want is it to search the entire database (400+ rows) no matter how many rows are showing.

— asked November 1st 2010 by Colton Arabsky
  • searchbar
0 Comments

6 Answers

  • I assume you could one of two things. Hook the blur event and grab the search string and initiate a call to your server and refill the table. Or you could hook the change event and do the same thing. Depending on the number of rows, indices and type of data it may be better to catch the blur since it would be called less often than change.

    Your code looks pretty good, should have looked at it first ;) Going to see what you are doing and if I have any suggestions.

    Yes. Nicely done. I think you can do what I mentioned above. You should be able to trigger off of a combination of blur/change/return and refill the table with the search results. Where it may get tricky is undoing/canceling the search. You'd probably want to keep the results of standard scrolling in an array and append the search results to the previous list results and that would let you undo by popping the array back into the table.

    — answered November 1st 2010 by John McKnight
    permalink
    0 Comments
  • Hey John, sorry, but could you give me an example of what you are saying? I understand what you're saying 100%, it's just I work better with an example of it, rather than just an explanation. If you know what you mean? :)

    — answered November 1st 2010 by Colton Arabsky
    permalink
    1 Comment
    • Sure. Let me get some coffee and scratch my head for a few moments :)

      — commented November 1st 2010 by John McKnight
  • John, I think you have been scratching your head and drinkin' coffee for long enough! Haha ;)

    — answered November 1st 2010 by Colton Arabsky
    permalink
    1 Comment
    • haha - ok. I'll see what I can do.

      — commented November 1st 2010 by John McKnight
  • Here is the basic idea in code, sorta.

    var lastSearchTerm = '';
    
    function doSearch(search) {
      var xhr = Ti.Network.createHTTPClient();
      xhr.setTimeout(60000);
    
      xhr.onerror = function(e) { 
        Ti.API.info(e); 
      };
    
      xhr.onload = function(){
        var json = this.responseText;
        var response = JSON.parse(json);
    
        /* By this point you have some new data to append to your table */
        // You could push the data into the table or merge it with an array of previous fetched data.
      }
    
      xhr.open('POST', service_url /* this is your url that would respond with search data */);
    
      var parms = {
        searchTerm: search
      }; 
    
      xhr.send(parms);
    }
    
    
    // This gets called when a key was pressed or enter or blur
    function searchChanged(search) {
    
      // If the search changed, we do our call to the server.
      if (search != lastSearchTerm) {
        doSearch(search);
      }
    
      lastSearchTerm = search;
    }
    
    
    
    // These are your listeners with a change to call the search
    searchbar.addEventListener('change', function(e)
    {
        Titanium.App.Properties.setBool('if_searched', true);
        searchChanged(e.value);
    
        return e.value;
    });
    searchbar.addEventListener('return', function(e)
    {
        searchbar.blur();
        Titanium.App.Properties.setBool('if_searched', true);
        searchChanged(e.value);
    
        return e.value;
    });
    
    searchbar.addEventListener('blur', function(e)
    {
        searchChanged(e.value);
    }
    

    In your PHP you could expect to see a new variable $_POST['searchTerm'] that you can use to do a search through your data.

    — answered November 1st 2010 by John McKnight
    permalink
    0 Comments
  • Hey John. Is it possible to put a HTTP client inside another one? I've never tried it before, would it cause any lag?

    — answered November 2nd 2010 by Colton Arabsky
    permalink
    1 Comment
    • Can you be more specific? What are you trying to do? Are you wanting to send a request after the first request completed?

      — commented November 2nd 2010 by John McKnight
  • Alright John, I really don't understand what I am supposed to do with that.

    — answered November 4th 2010 by Colton Arabsky
    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.