Titanium Community Questions & Answer Archive

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

How to clear/remove rows from a picker?

I have an app that has two picker controls in it, where selecting a value from the first picker should alter the rows in the second picker. However, I don't know how to remove existing rows from the second picker (or clear it out completely), I only know how to add new rows. So, each time the value is changed in the first picker, the new rows are tacked onto the second picker along with the old ones, instead of replacing them.

Anyone know how to clear out a picker? And please don't say I need to create a new picker each time I want to change the rows, because that seems pretty ridiculous.

— asked November 26th 2010 by Bill Labus
  • clear
  • data
  • delete
  • picker
  • remove
  • reset
  • rows
1 Comment
  • I updated my original response below…

    — commented November 26th 2010 by Aaron Saunders

8 Answers

  • I also recieved the error "Result of expression '_col' [undefined] is not an object.";

    but I figured out that I ran the code before I populated my picker. So I'm doing a check before if columns[0] exists.

    if(picker.columns[0]) {
        var _col = picker.columns[0];
            var len = _col.rowCount;
            for(var x = len-1; x >= 0; x-- ){
                    var _row = _col.rows[x];
                    _col.removeRow(_row);
            }
    //picker.reloadColumn(_col);
    }
    

    I also removed the reloadColumn, because it seemed to hang my app, and it's not needed afaik.

    — answered January 11th 2011 by Ted Sundin
    permalink
    5 Comments
    • thanks a lot
      it is working now :)

      — commented January 17th 2011 by Somesh pandit
    • Awesome….its working…thanks a lot !!

      — commented September 13th 2011 by Anika Kalia
    • This works perfectly fine, why this is not marked as best ?

      — commented March 25th 2013 by Nabeel Munawar
    • It's Working Perfect. Thanks a lot Mr.Ted

      — commented March 19th 2014 by Santhosekumar Subramanian
    • Thank you so much..it works fine..

      — commented June 2nd 2014 by Rama krishna
  • I saw the following comment :

    This is not working , i am getting below error message = "Result of expression '_col' [undefined] is not an object."; i am using Ti 1.5.1 SDK , iOS SDK 4.2

    This is because you are not binding the picker the correct way for that code to work. What you need to do is use PickerRows and PickerColumns. So create your picker as such:

    var picker = Ti.UI.createPicker(); 
    
    picker.add([Titanium.UI.createPickerColumn()]);
    
    picker.selectionIndicator = true;
    

    Then you need to bind your picker. Now.. I am not an Appcelerator expert so this may or may not be the right way to do it. Also the documentation is doodie and I am not a rich man to shell out 2k for the support. At any rate… I created the following two functions:

    function removeAllPickerRows(picker){
    
        var _col = picker.columns[0];
            var len = _col.rowCount;
            for(var x = len-1; x >= 0; x-- ){
                var _row = _col.rows[x]
                _col.removeRow(_row);
            }
            picker.reloadColumn(_col); 
    }
    
    function addPickerRows(_col,data){
    
        for(var x = 0; x < data.length; x++ ){
    
            _col.addRow(data[x]);
    
        }
    
    }
    

    I had a string array called [choices] and I wanted to turn that to rows in the picker. So I did this:

    //-- 
    
    for(i = 0; i < choices.length; i++){
    
        data[i] = Ti.UI.createPickerRow({title:choices[i].substring(0,20) + '...',value:choices[i]});
    
    }
    

    I suppose I could just pass the string array to the addPickerRow function however I wanted a more generic function for my future purposes. So that said.. I bound my picker like this:

    removeAllPickerRows(picker);
    
    var _col = picker.columns[0];
    
    addPickerRows(_col,data);
    

    Hope that helps and that I haven't missed anything.

    Good Luck…

    — answered February 2nd 2011 by Adrian Pavelescu
    permalink
    0 Comments
  • UPDATED: Sorry about the original answer… looping through the values and deleting the rows seems to work as expected

        var _col = picker.columns[0];
        var len = _col.rowCount;
        for(var x = len-1; x >= 0; x-- ){
            var _row = _col.rows[x]
            _col.removeRow(_row);
        }
        picker.reloadColumn(_col);
    
    — answered November 26th 2010 by Aaron Saunders
    permalink
    5 Comments
    • That just causes the app to crash when those statements are encountered.

      — commented November 26th 2010 by Bill Labus
    • post some code…

      — commented November 26th 2010 by Aaron Saunders
    • This is not working , i am getting below error message = "Result of expression '_col' [undefined] is not an object."; i am using Ti 1.5.1 SDK , iOS SDK 4.2

      Somesh

      — commented January 4th 2011 by Somesh pandit
    • hi.
      this is supposed to reload the previously loaded rows when the picker has been created?
      A.

      — commented February 18th 2011 by alberto monteiro
    • meanwhile, I've successfully done it:

      var _col = wpkTypesPicker.columns[0];
      var len = _col.rowCount;
      for(var x = len-1; x >= 0; x-- ){
          var _row = _col.rows[x]
          _col.removeRow(_row);
      }
      

      //—– wpkTypesPicker.reloadColumn(_col); (I had to remove this, so it works)
      wpkTypesPicker.add(getWandTypes()); // get rows data again

      — commented February 18th 2011 by alberto monteiro
  • I have been trying all day to do exactly the same thing Bill, but from reading similar posts on this forum it appears that there are bugs in the Picker API which make what ought to be simple into something darn near impossible.

    After spending the best part of 8 hours on it I gave up and went and did something useful like watching the TV.

    Come on Accelerator, these bugs make the product look shoddy when we know it has so much potential.

    — answered November 26th 2010 by Patrick Mounteney
    permalink
    2 Comments
    • Patrick, the code below works, I edited the post instead of posting a new answer which I guess is kind of confusing to new users coming to look at the question.

      var _col = picker.columns[0];
          var len = _col.rowCount;
          for(var x = len-1; x >= 0; x-- ){
              var _row = _col.rows[x]
              _col.removeRow(_row);
          }
          picker.reloadColumn(_col);
      

      — commented November 26th 2010 by Aaron Saunders
    • Most odd Aaron, as when I try the code it just throws an exception with an error message of:
      "Result of expression 'picker.reloadColumn' [undefined] is not a function.";
      I've check we are dealing with a TiUIPickerColumn object up to just before the last line - we are - and it still won't oblige! I wonder if you are on a different build of Titanium SDK? - I'm on 1.4.2 and iOS 4.1 SDK.

      — commented November 27th 2010 by Patrick Mounteney
  • I have been trying all day to do exactly the same thing Bill, but from reading similar posts on this forum it appears that there are bugs in the Picker API which make what ought to be simple into something darn near impossible.

    After spending the best part of 8 hours on it I gave up and went and did something useful like watching the TV.

    Come on Accelerator, these bugs make the product look shoddy when we know it has so much potential.

    — answered November 27th 2010 by Patrick Mounteney
    permalink
    0 Comments
  • doublepost, see below

    — answered January 11th 2011 by Ted Sundin
    permalink
    0 Comments
  • This works for simple pickers in iPhone Simulator. See my answer here:

    Clear Rows from Picker

    — answered May 10th 2011 by David Wilson
    permalink
    0 Comments
  • I just tested Aaron's remove/reload code in Ti SDK 1.5.1 – and it works! Thanks Titanium guys!

    Mark

    — answered December 30th 2010 by Mark Pemburn
    permalink
    1 Comment
    • this is still not working for me
      i am getting below error
      message = "Result of expression '_col' [undefined] is not an object.";
      i am using Ti 1.5.1 SDK
      Please tell me is anything else needs to be done to run the code

      Somesh

      — commented January 2nd 2011 by Somesh pandit
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.