How to correctly remove all colums from a Ti.UI.Picker?

You must Login before you can answer or comment on any questions.

Based on the documentation [http://docs.appcelerator.com/titanium/latest/#!/api/Titanium.UI.Picker], the object Ti.Ui:Picker as a method to set the colums called

Ti.UI.Picker.setColumns();
which recieve an array Ti.UI.PickerRow object as argument, or a single Ti.UI.pickerRow

So, to clear out the picker options, if I try

var pInput = Ti.UI.createPicker({   
});
 
var data = [];
data[0]=Ti.UI.createPickerRow({title:'Tdata1'});
data[1]=Ti.UI.createPickerRow({title:'Tdata2'});
data[2]=Ti.UI.createPickerRow({title:'Tdata3'});
data[3]=Ti.UI.createPickerRow({title:'Tdata4'});
 
pInput.add(data);
 
/**
* data should be an array of Ti.UI.PickerRow
**/
 
var setPInput = function (data){
    pInput.setColumns(null);
    pInput.add(data); 
};
You get an uncaugth exception:
ERROR][TiJSError( 1966)] (main) [1,20823] - Message: Uncaught Error: Java Exception occurred
[ERROR][TiJSError( 1966)] (main) [1,20824] - Source:        pInput.setColumns(null);;
So, I changed to pass an TI.UI.PickerRow as argument doing
var setPInput = function (data){
    pInput.setColumns(Ti.UI.createPickerRow({title:'Bananas'}));
    pInput.add(data); 
};
I got the colums cleared, but no "bananas" option, and the following warning
[WARN][PickerProxy( 2098)] Unexpected object type ignored for setColumns
Since I got this error, then I created and array and passed the array...
var setPInput = function (data){
    var bananas = []; 
    bananas[0]=Ti.UI.createPickerRow({title:'Bananas'})
    pInput.setColumns(bananas);
    pInput.add(data); 
};
again, got the columns cleared, no "bananas", and the same warning
[WARN][PickerProxy( 2098)] Unexpected object type ignored for setColumns
So, what would be the correct way to clear the columns? and what would be the correct parameter type to pass to setColumns?

1 Answer

Accepted Answer

Hi, Try this way:

var win = Ti.UI.createWindow({
  layout: 'vertical'
});
 
var picker = Ti.UI.createPicker({
  top:50
});
picker.selectionIndicator = true;
 
var fruit = [ 'Bananas', 'Strawberries', 'Mangos', 'Grapes' ];
var color = [ 'red', 'green', 'blue', 'orange' ];
 
var column1 = Ti.UI.createPickerColumn();
 
for(var i=0, ilen=fruit.length; i<ilen; i++){
  var row = Ti.UI.createPickerRow({
    title: fruit[i]
  });
  column1.addRow(row);
}
 
var column2 = Ti.UI.createPickerColumn();
 
for(var i=0, ilen=color.length; i<ilen; i++){
  var row = Ti.UI.createPickerRow({ title: color[i] });
  column2.addRow(row);
}
 
picker.add([column1,column2]);
 
win.add(picker);
 
win.open();

its a demo code for help.. modify as per your need.

— answered 7 months ago by Ashish Nigam
answer permalink
4 Comments
  • shame on me

    _ Parameters _

    columns : Titanium.UI.PickerColumn[]

    New value for the property.

    And I was using a rTi.UI.PickerRow! my mistake.

    Finished code:

    var setPInput=function (data)
    {
        var Column=Ti.UI.createPickerColumn();
        for(var i in data)
        Column.addRow(data[i]); 
        ColoniaInput.setColumns(Column);
        ColoniaInput.selectionIndicator = true;
        ColoniaInput.setSelectedRow(0, 0, false);
        view14.add(ColoniaInput); //if not added, doesn't refresh the screen.. 
     
    }

    — commented 7 months ago by Andrés Tello

  • Err.. ColoniaInput is my real PInput... XD

    — commented 7 months ago by Andrés Tello

  • if you have only one column, you can even skip the createPickerColumn() and do myPicker.add(data) (cf the doc " When this method is used to add a row or set of rows, a single-column picker is created." and so to clear the picker data : myPicker.columns =[],

    — commented 7 months ago by Vince

  • Show 1 more comment

Your Answer

Think you can help? Login to answer this question!