Como montar uma rotina de Pesquisa Dinamica numa DropDownList ASP.NET
Como montar uma rotina de Pesquisa Dinamica numa DropDownList ASP.NET
Tenho uma relação de 200 clientes que preciso abrir numa DROPDOWLIST, sendo que fica inviável.
Existe alguma maneira de implementar um filtro através da DROPDOWNLIST dinamicamente?
Atte
Sérgio
Sergio Santos
Curtidas 0
Respostas
[devmedia .net]
13/02/2009
Opa,
Segue um código com uma biblioteca,
onde você seleciona o dropdown e vai digitando o nome do cliente,
no dropdown vai aparecendo os filtros e no label o que você digitou...
Fácil de implementar e funcionando 100%
Segue o código que deverá colocar na página.
<script type="text/javascript" language="JavaScript"> function inicializar() { if (document.getElementById("ddlAgencia") != null) //ddlAgência é o id do DropDownList cc1 = new ComboCompletion(document.getElementById("ddlAgencia"), document.getElementById("divAgencia")); //divAgência é o id da Div onde vai aparecer o que você digitou ao colocar o foco no dropdown if (document.getElementById("ddlFuncionario") != null) cc1 = new ComboCompletion(document.getElementById("ddlFuncionario"), document.getElementById("divFuncionario")); if (document.getElementById("ddlProduto") != null) cc1 = new ComboCompletion(document.getElementById("ddlProduto"), document.getElementById("divProduto")); if (document.getElementById("ddlSegmento") != null) cc1 = new ComboCompletion(document.getElementById("ddlSegmento"), document.getElementById("divSegmento")); } window.onload = inicializar; </script> Esse é o método que faz a busca no dropdown function ComboCompletion(object, informationArea) { this.object = object; this.searchPattern = ""; this.informationArea = informationArea; this.onChanged = null; this.updateInformationArea = function() { if (this.informationArea != null) { this.informationArea.innerText = this.searchPattern; } } this.updateToFirstMatch = function() { var i; var options = this.object.options; for(i = 0, l = options.length; i < l; i++) { if (options[i].text.toUpperCase().indexOf(this.searchPattern) == 0) { this.object.selectedIndex = i; return; } } } this.keyUp = function() { if ((event.keyCode == 37) || (event.keyCode == 38) || (event.keyCode == 39) || (event.keyCode == 40)) { this.comboCompletionObject.searchPattern = ""; this.comboCompletionObject.updateInformationArea(); } return true; } this.keyDown = function() { if (event.keyCode == 8) { this.comboCompletionObject.searchPattern = this.comboCompletionObject.searchPattern.substring(0, this.comboCompletionObject.searchPattern.length - 1); this.comboCompletionObject.updateInformationArea(); this.comboCompletionObject.updateToFirstMatch(); return false; } return true; } this.blur = function() { this.comboCompletionObject.searchPattern = ""; this.comboCompletionObject.updateInformationArea(); if (typeof(this.comboCompletionObject.onChanged) == "function") { this.comboCompletionObject.onChanged(this); } } this.keyPress = function() { var key = String.fromCharCode(event.keyCode).toUpperCase(); if (/[a-zA-Z0-9\s.\/]/.test(key)) { this.comboCompletionObject.searchPattern += key; this.comboCompletionObject.updateInformationArea(); this.comboCompletionObject.updateToFirstMatch(); return false; } return true; } this.object.comboCompletionObject = this; this.object.onkeypress = this.keyPress; this.object.onkeyup = this.keyUp; this.object.onkeydown = this.keyDown; this.object.onblur = this.blur; } Abraços Carlos Jr
<script type="text/javascript" language="JavaScript"> function inicializar() { if (document.getElementById("ddlAgencia") != null) //ddlAgência é o id do DropDownList cc1 = new ComboCompletion(document.getElementById("ddlAgencia"), document.getElementById("divAgencia")); //divAgência é o id da Div onde vai aparecer o que você digitou ao colocar o foco no dropdown if (document.getElementById("ddlFuncionario") != null) cc1 = new ComboCompletion(document.getElementById("ddlFuncionario"), document.getElementById("divFuncionario")); if (document.getElementById("ddlProduto") != null) cc1 = new ComboCompletion(document.getElementById("ddlProduto"), document.getElementById("divProduto")); if (document.getElementById("ddlSegmento") != null) cc1 = new ComboCompletion(document.getElementById("ddlSegmento"), document.getElementById("divSegmento")); } window.onload = inicializar; </script> Esse é o método que faz a busca no dropdown function ComboCompletion(object, informationArea) { this.object = object; this.searchPattern = ""; this.informationArea = informationArea; this.onChanged = null; this.updateInformationArea = function() { if (this.informationArea != null) { this.informationArea.innerText = this.searchPattern; } } this.updateToFirstMatch = function() { var i; var options = this.object.options; for(i = 0, l = options.length; i < l; i++) { if (options[i].text.toUpperCase().indexOf(this.searchPattern) == 0) { this.object.selectedIndex = i; return; } } } this.keyUp = function() { if ((event.keyCode == 37) || (event.keyCode == 38) || (event.keyCode == 39) || (event.keyCode == 40)) { this.comboCompletionObject.searchPattern = ""; this.comboCompletionObject.updateInformationArea(); } return true; } this.keyDown = function() { if (event.keyCode == 8) { this.comboCompletionObject.searchPattern = this.comboCompletionObject.searchPattern.substring(0, this.comboCompletionObject.searchPattern.length - 1); this.comboCompletionObject.updateInformationArea(); this.comboCompletionObject.updateToFirstMatch(); return false; } return true; } this.blur = function() { this.comboCompletionObject.searchPattern = ""; this.comboCompletionObject.updateInformationArea(); if (typeof(this.comboCompletionObject.onChanged) == "function") { this.comboCompletionObject.onChanged(this); } } this.keyPress = function() { var key = String.fromCharCode(event.keyCode).toUpperCase(); if (/[a-zA-Z0-9\s.\/]/.test(key)) { this.comboCompletionObject.searchPattern += key; this.comboCompletionObject.updateInformationArea(); this.comboCompletionObject.updateToFirstMatch(); return false; } return true; } this.object.comboCompletionObject = this; this.object.onkeypress = this.keyPress; this.object.onkeyup = this.keyUp; this.object.onkeydown = this.keyDown; this.object.onblur = this.blur; } Abraços Carlos Jr
GOSTEI 0
Sergio Santos
13/02/2009
Ok.
Carlos
Obrigado mais uma vez. Vou estudar este código e te retorno.
Abs
Sérgio
GOSTEI 0
[devmedia .net]
13/02/2009
Opa, esse código eu fiz aqui antes de lhe enviar e funcionou normalmente...
Não tem segredos , irá funcionar.
Fico no aguardo do seu retorno.
Abraços
Carlos Jr
GOSTEI 0
[devmedia .net]
13/02/2009
Opa blz ?
Aguardo seu retorno para darmos continuidade ao atendimento
ou fechamento do chamado.
Abraços
Carlos Jr
GOSTEI 0