Ver Mensaje Individual
  #3 (permalink)  
Antiguo 26/07/2011, 06:14
dcm1987
 
Fecha de Ingreso: julio-2011
Mensajes: 12
Antigüedad: 12 años, 9 meses
Puntos: 0
Respuesta: autocomplete <select>

muchas gracias por la información en todo, sobre el código y sobre el como manejarme mejor en le foro.
Con respecto al autocomplete, estuve documentandome un poco, y vi un plugin, jquery ui, que en la seccion autocomplete hay una pestaña que pone combobox. es la que he acabado utilizando aqui pongo parte del codigo:

codigo javascript:

Código Javascript:
Ver original
  1. (function( $ ) {
  2.     $.widget( "ui.combobox", {
  3.         _create: function() {
  4.             var self = this,
  5.                 select = this.element.hide(),
  6.                 selected = select.children( ":selected" ),
  7.                 value = selected.val() ? selected.text() : "";
  8.             var input = this.input = $( "<input>" )
  9.                 .insertAfter( select )
  10.                 .val( value )
  11.                 .autocomplete({
  12.                     delay: 0,
  13.                     minLength: 0,
  14.                     source: function( request, response ) {
  15.                         var matcher = new RegExp( $.ui.autocomplete.escapeRegex(request.term), "i" );
  16.                         response( select.children( "option" ).map(function() {
  17.                             var text = $( this ).text();
  18.                             if ( this.value && ( !request.term || matcher.test(text) ) )
  19.                                 return {
  20.                                     label: text.replace(
  21.                                         new RegExp(
  22.                                             "(?![^&;]+;)(?!<[^<>]*)(" +
  23.                                             $.ui.autocomplete.escapeRegex(request.term) +
  24.                                             ")(?![^<>]*>)(?![^&;]+;)", "gi"
  25.                                         ), "<strong>$1</strong>" ),
  26.                                     value: text,
  27.                                     option: this
  28.                                 };
  29.                         }) );
  30.                     },
  31.                     select: function( event, ui ) {
  32.                         ui.item.option.selected = true;
  33.                         self._trigger( "selected", event, {
  34.                             item: ui.item.option
  35.                         });
  36.                     },
  37.                     change: function( event, ui ) {
  38.                         if ( !ui.item ) {
  39.                             var matcher = new RegExp( "^" + $.ui.autocomplete.escapeRegex( $(this).val() ) + "$", "i" ),
  40.                                 valid = false;
  41.                             select.children( "option" ).each(function() {
  42.                                 if ( $( this ).text().match( matcher ) ) {
  43.                                     this.selected = valid = true;
  44.                                     return false;
  45.                                 }
  46.                             });
  47.                             if ( !valid ) {
  48.                                 // remove invalid value, as it didn't match anything
  49.                                 $( this ).val( "" );
  50.                                 select.val( "" );
  51.                                 input.data( "autocomplete" ).term = "";
  52.                                 return false;
  53.                             }
  54.                         }
  55.                     }
  56.                 })
  57.                 .addClass( "ui-widget ui-widget-content ui-corner-left" );
  58.                 input.data( "autocomplete" )._renderItem = function( ul, item ) {
  59.                 return $( "<li></li>" )
  60.                     .data( "item.autocomplete", item )
  61.                     .append( "<a>" + item.label + "</a>" )
  62.                     .appendTo( ul );
  63.             };
  64.                 this.button = $( "<button type='button'>&nbsp;</button>" )
  65.                 .attr( "tabIndex", -1 )
  66.                 .attr( "title", "Ver todos los elementos" )
  67.                 .insertAfter( input )
  68.                 .button({
  69.                     icons: {
  70.                         primary: "ui-icon-triangle-1-s"
  71.                     },
  72.                     text: false
  73.                 })
  74.                 .removeClass( "ui-corner-all" )
  75.                 .addClass( "ui-corner-right ui-button-icon" )
  76.                 .click(function() {
  77.                     // close if already visible
  78.                     if ( input.autocomplete( "widget" ).is( ":visible" ) ) {
  79.                         input.autocomplete( "close" );
  80.                         return;
  81.                     }
  82.                         // work around a bug (likely same cause as #5265)
  83.                     $( this ).blur();
  84.                         // pass empty string as value to search for, displaying all results
  85.                     input.autocomplete( "search", "" );
  86.                     input.focus();
  87.                 });
  88.         },
  89.  
  90.         destroy: function() {
  91.             this.input.remove();
  92.             this.button.remove();
  93.             this.element.show();
  94.             $.Widget.prototype.destroy.call( this );
  95.         }
  96.     });
  97. })( jQuery );
  98.     $(function() {
  99.     $( "#idbuscarproyecto" ).combobox();
  100. });
Código HTML:
Ver original
  1. <select id="idbuscarproyecto" name="idbuscarproyecto">
  2.     <option value="">seleccione un proyecto</option>
  3.         <?php
  4.        require_once "funciones.php";
  5.     proyectos_bd();
  6.        ?>


codigo php:

Código PHP:
Ver original
  1. function proyectos_bd(){
  2.         $link=mysql_connect();
  3.         mysql_select_db();
  4.         $sql="";
  5.         $result=mysql_query($sql,$link);
  6.         while($row=mysql_fetch_assoc($result)){
  7.             $value = $row["pm_number"].", ".$row["pm_title"];
  8.             $id=$row["pm_id"];
  9.             echo "<option value='$id'>$value</option>";
  10.         }
  11.         mysql_close($link);
  12.     }