Last active
August 29, 2015 14:05
-
-
Save comtom/17d2643ef25848c95a84 to your computer and use it in GitHub Desktop.
dropdown - ajax
This file contains hidden or 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
| /* | |
| ---------------------------- | |
| PARTIDOS | |
| ---------------------------- | |
| */ | |
| // trae partidos segun el departamento | |
| case 'partidos': | |
| $departamento = (!empty($_GET['departamento'])) ? ms_escape_string($_GET['departamento']) : "0"; | |
| $sql = "SELECT distinct P.PARTIDO, P.DESCRIPCION | |
| FROM PARTIDO P | |
| INNER JOIN Juzgado J on P.Partido=J.idPartido | |
| WHERE idDepartamento=". $departamento ." | |
| ORDER BY P.DESCRIPCION"; | |
| @$rs = mssql_query($sql); | |
| $i = 1; | |
| $lista_partidos = '[ '; | |
| while ( @$val = mssql_fetch_assoc($rs) ) { | |
| /* | |
| $seleccionado = ''; | |
| if ($val['PARTIDO'] == $juzgado ) { | |
| $seleccionado = ' selected '; | |
| } | |
| echo '<option ' . $seleccionado . ' value="' . $val['PARTIDO'] . '">' . $val['DESCRIPCION'] . '</option>'; | |
| */ | |
| if ($i <> 1) { | |
| $lista_partidos.=','; | |
| } | |
| $lista_partidos.= '{ "label": "'. utf8_encode(ucwords(strtolower(utf8_decode($val['DESCRIPCION'])))) .'", "value": "'. $val['PARTIDO'] .'" }'; | |
| $i++; | |
| } | |
| $lista_partidos.= ']'; | |
| header('Content-type: application/json; charset=utf-8'); | |
| echo $lista_partidos; | |
| break; | |
| /* | |
| ---------------------------- | |
| JUZGADOS | |
| ---------------------------- | |
| */ | |
| // trae juzgados segun el departamento y partido | |
| case 'juzgados': | |
| $departamento = (!empty($_GET['departamento'])) ? ms_escape_string($_GET['departamento']) : "0"; | |
| $partido = (!empty($_GET['partido'])) ? ms_escape_string($_GET['partido']) : "0"; | |
| $sql = "SELECT ID, NOMBRE | |
| FROM JUZGADO | |
| where idDepartamento=". $departamento ." | |
| and idPartido=". $partido ." | |
| order by NOMBRE"; | |
| @$rs = mssql_query($sql); | |
| $i = 1; | |
| $lista_juzgados = '['; | |
| while (@$val = mssql_fetch_assoc($rs)) { | |
| if ($i <> 1) { | |
| $lista_juzgados.=','; | |
| } | |
| $lista_juzgados.= '{ "label": "'. $val['NOMBRE'] .'", "value": "'. $val['ID'] .'" }'; | |
| $i++; | |
| } | |
| $lista_juzgados.= ']'; | |
| header('Content-type: application/json; charset=utf-8'); | |
| echo $lista_juzgados; | |
| break; | |
| //-------------------------------- | |
| <script type="text/javascript" src="include/jquery.js"></script> | |
| <script type="text/javascript" src="include/bootstrap.min.js"></script> | |
| <script type="text/javascript" src="include/jquery.cascadingdropdown.js"></script> | |
| $('#cascada').cascadingDropdown({ | |
| usePost: true, | |
| useJson: true, | |
| selectBoxes: [ | |
| { | |
| selector: '.step1', | |
| source: lista_deptos | |
| }, | |
| { | |
| selector: '.step2', | |
| requires: ['.step1'], | |
| source: function(request, response) { | |
| $.getJSON('/admin/ajax.php?op=partidos', request, function(data) { | |
| var selectOnlyOption = data.length <= 1; | |
| response($.map(data, function(item, index) { | |
| return { | |
| label: item.label, | |
| value: item.value, | |
| selected: selectOnlyOption | |
| }; | |
| })); | |
| }); | |
| } | |
| }, | |
| { | |
| selector: '.step3', | |
| requires: ['.step1', '.step2'], | |
| requireAll: true, | |
| source: function(request, response) { | |
| $.getJSON('/admin/ajax.php?op=juzgados', request, function(data) { | |
| var selectOnlyOption = data.length <= 1; | |
| response($.map(data, function(item, index) { | |
| return { | |
| label: item.label, | |
| value: item.value, | |
| selected: selectOnlyOption | |
| }; | |
| })); | |
| }); | |
| }, | |
| onChange: function(event, value, requiredValues) { | |
| // do stuff | |
| } | |
| } | |
| ] | |
| }); | |
| $('.step3').click( function() { | |
| var partido; | |
| partido = $('.step2').val(); | |
| //console.log( partido ); | |
| if ( $('.step2 option').size() == 1 ) { | |
| // trae lista de partidos | |
| $('.step1').trigger('change'); | |
| // selecciona el partido que tenia | |
| $('.step2 option:selected').removeAttr('selected'); | |
| $('.step2 option[value='+ partido +']').prop('selected', true); | |
| if ( $('.step3 option').size() == 1 ) { | |
| // trae juzgados | |
| $('.step2').trigger('change'); | |
| // cierra el desplegable | |
| $('.step3').blur(); | |
| } | |
| } | |
| }); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment