Created
August 17, 2014 09:25
-
-
Save zo0m/4705739bcd962ee53387 to your computer and use it in GitHub Desktop.
Titanium Appcelerator search drop down
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var win = $.index; | |
Alloy.Globals.ActiveWindow = win; | |
//for dropdown hints | |
Alloy.Globals.CurrentDropDown = null; | |
Alloy.Globals.removeCurrentDropDown = function() { | |
Alloy.Globals.ActiveWindow.remove(Alloy.Globals.CurrentDropDown); | |
Alloy.Globals.CurrentDropDown = null; | |
}; | |
Alloy.Globals.ActiveWindow.addEventListener('touchstart', function(e) { | |
if (Alloy.Globals.CurrentDropDown) | |
{ | |
Alloy.Globals.removeCurrentDropDown(); | |
} | |
}); | |
Alloy.Globals.createDropDown = function(sourceInput, dropDownData) { | |
Alloy.Globals.removeCurrentDropDown(); | |
var inputContainerView = sourceInput.getParent(); | |
var globalPoint = inputContainerView.convertPointToView ( | |
{x : sourceInput.rect.x, y : (sourceInput.rect.y + sourceInput.rect.height)}, | |
Alloy.Globals.ActiveWindow | |
); | |
var dropDownTable = Ti.UI.createTableView({ | |
data: dropDownData, | |
zIndex : 5, | |
top : globalPoint.y, | |
left: globalPoint.x, | |
height: Ti.UI.SIZE, | |
font: { | |
fontSize:"18dp" | |
}, | |
width: "280dp", | |
backgroundColor: '#FFFFFF' | |
}); | |
dropDownTable.addEventListener('touchstart', function(e) { | |
if(e.row) | |
{ | |
if (sourceInput.setChoosenData) | |
{ | |
sourceInput.setChoosenData(e); | |
} else { | |
sourceInput.value = e.row.title; | |
sourceInput.chosenId = e.index; | |
} | |
sourceInput.blur(); | |
Alloy.Globals.removeCurrentDropDown(); | |
} | |
}); | |
Alloy.Globals.CurrentDropDown = dropDownTable; | |
Alloy.Globals.ActiveWindow.add(dropDownTable); | |
}; | |
// somewhere in controller : | |
$.nationalityPicker.addEventListener('change', function(e) { | |
var input = $.nationalityPicker; | |
var inputTxt = input.value; | |
var countriesDictionary = Alloy.Globals.countriesDictionary; | |
// found counties that match | |
var dropDownData = []; | |
for (var i=0; i < countriesDictionary.length; i++) { | |
if (inputTxt.toUpperCase() === countriesDictionary[i].name.substr(0, inputTxt.length)) | |
{ | |
if (dropDownData.length < 5) | |
{ | |
dropDownData.push({title : countriesDictionary[i].name, code : countriesDictionary[i].code, id : i}); | |
} | |
} | |
}; | |
// creating drop down | |
Alloy.Globals.createDropDown($.nationalityPicker, dropDownData); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment