Refreshing a picker data on the event of another picker.

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

Hi all, I have two single column pickers one for Car makes and one for Car models. I want when a Car Make is selected its corresponding Models are loaded in the Car Models picker. e.g if I select Honda in the Car Makes picker then only Honda's models should appear in the Car Models picker and no other company's models should be there. Also the Models are coming from a nested JSON object. Here is how I am trying to do this but the Models picker is always blank with no rows:-

pkr_Make_btnDone.addEventListener('click',function() {      //When a value is selected in Makes Picker
        txt_VM.value =  pkr_VM.getSelectedRow(0).title;
        pkr_VM_view.animate(slide_out);
 
        Ti.App.fireEvent('make',{name: txt_VM.value})
    });
 
Ti.App.addEventListener('make',function(e)
{
    limit=models[e.name].length;  //models is the JSON object that contains Car Models
    var Mdl_data=[];
 
    for(var i=0;i<limit;i++)
    {
        //populate the dictionary for Models picker with selected Car Make's Models
        Mdl_data.push(Titanium.UI.createPickerRow({title: models[e.name][i]}));
    }
    pkr_Mdl.add(Mdl_data); //add the data to the models picker.
})
Is there a way to do this??? If its not possible then please suggest some alternate method to do this easily.Plz somebody help ASAP. I am stucked with it for last two days. Thanks in advance.

— asked 9 months ago by Muhammad Qasim Khan
12 Comments
  • it would help if there were a short example of what the JSON model object looked like?

    — commented 9 months ago by Aaron Saunders

  • also is ths specific to android or IOS?

    — commented 9 months ago by Aaron Saunders

  • Hi Aaron, here is the JSON object and its for both iOS and android but right now I am stucked in iOS. I want when Honda is selected its corresponding models are available in model picker from JSON object and so on.

    — commented 9 months ago by Muhammad Qasim Khan

  • Show 9 more comments

1 Answer

Accepted Answer

OK -- I think I have your answer. This was a strange one. The main problem seems to be that calling Ti.UI.Picker.add() after the picker has been added to the view doesn't seem to work. Note that it works if you add it to a view, but then don't add that view to the window (which your original code does with the pkr_VM, which is added to pkr_VM_view, but pkr_VM_view isn't added to the window until the timeout). But once you've added the picker to the window or a child view of the window, it doesn't seem like you can call add().

So what's the workaround? When you have new data for the picker, remove the picker from its parent view and create a new one. This is a good idea anyway, since there's not really a good way to remove the data from the picker. With your code, if the user chose a different Make, you'd be appending the models for that make to the picker (that is, if you were able to call add() after adding the picker to the window, which you can't anyway).

I've posted the modified code here.

I also found a few issues in your code that you should be aware of.

  • Avoid use of App-level events when control-level events would be perfectly serviceable; see my code

  • timeout is not a good solution -- set the controls disabled until the network results are back and the lists are populated; then enable them; see my code

  • naming conflicts: cancel, done, spacer, toolbar: use CommonJS modules and require() so that you don't have to worry about these sort of things; I made quick-and-dirty name changes for you

  • pkrModelData is loaded, but unused

Note that your code won't work on android, if that matters to you. My TitanUp library has a view called "SimplePicker" which behaves like yours on iOS but also works on android. Note that it will have trouble with dynamically updated pickers, too. I don't think the changes would be too hard; it would need a setValues() function, which would have to recreate the PickerPopup each time the rows are changed.

— answered 9 months ago by Jason Priebe
answer permalink
1 Comment
  • Awesome. Great!!! It took me a while to fix an issue in it (sorry for late) and now its working like a charm, as I wanted. Thanks again.

    — commented 9 months ago by Muhammad Qasim Khan

Your Answer

Think you can help? Login to answer this question!