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.
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.
-
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…
-
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);
-
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.
-
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.
-
doublepost, see below
-
This works for simple pickers in iPhone Simulator. See my answer here:
-
I just tested Aaron's remove/reload code in Ti SDK 1.5.1 – and it works! Thanks Titanium guys!
Mark