Ver Mensaje Individual
  #8 (permalink)  
Antiguo 02/02/2011, 10:37
Fenris
 
Fecha de Ingreso: agosto-2008
Mensajes: 271
Antigüedad: 15 años, 8 meses
Puntos: 2
Respuesta: [problema] ZF y AJAX

Hola masterpuppet

search.phtml es solo una vista dentro de un layout pero de hecho en el action desactivo el layout

search.phtml
Código HTML:
Ver original
  1. <?php echo $this->doctype() ?>
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
  4.  
  5. <?php $this->headLink()->appendStylesheet('/css/global.css') ?>
  6. <?php $this->headLink()->appendStylesheet('/css/master.css') ?>
  7. <?php echo $this->headLink() ?>
  8. <script type="text/javascript" src="/js/funciones.js"></script>
  9. <script type="text/javascript" src="/js/jquery-1.4.4.min.js"></script>
  10.  
  11. <script type="text/javascript">
  12.  
  13. var httpReq;    //objeto para realizar peticiones HTTP
  14. var items;  //Contendrá el XML obtenido para los elementos de la lista
  15. var ids;    //Contendrá el XML obtenido para los elementos de la lista
  16. var tipo;  //Contendra el XML obtenido para el elemento TIPO
  17. var valores;    //Contendrá el XML obtenido para los elementos de la lista
  18.  
  19. //Se encarga de cargar los items de la lista secundaria
  20. function loadItems(searchCriteria)
  21. {
  22.     var filtro;
  23.  
  24.     if (document.frm_buscar_clientes.filtros[0].checked){
  25.         filtro='apellidoPaterno';
  26.     } else {
  27.         filtro='rut';
  28.     }
  29.     ///filtro/"+filtro+"/criterio/"+searchCriteria
  30.     getXML("/clientes/search/format/xml/filtro/"+filtro+"/criterio/"+searchCriteria);
  31. }
  32.  
  33. //Carga el RSS en el objeto rssXML
  34. function getXML(url)
  35. {
  36.     //Si es Mozilla, Opera o safari (entre otros que lo soportan como objeto nativo del navegador)
  37.     if (window.XMLHttpRequest)
  38.     {
  39.         httpReq = new XMLHttpRequest();
  40.     }
  41.     else    //Internet Explorer lo expone como control Active X
  42.     {
  43.         httpReq = new ActiveXObject("Microsoft.XMLHTTP");
  44.     }
  45.     //Ya debería tener una referencia al objeto
  46.     if (httpReq != null)
  47.     {
  48.         httpReq.open("GET", url, true)  //El true del final es para que lo solicite de forma asíncrona
  49.         httpReq.onreadystatechange = finCarga;
  50.  
  51.         httpReq.send(null); //al ser una petición GET no va nada en el cuerpo de la petición, de ahí el null
  52.     }
  53. }
  54.  
  55. //Función que se ejecuta cuando cambia el estado de la carga del objeto httpReq
  56. function finCarga()
  57. {
  58.     if (httpReq.readyState == 4)    //4: completado, 3: en curso, 1:cargado, 0: no iniciado
  59.     {
  60.         if (httpReq.status == 200)  //200: OK (código HTTP, podría haber sido 404 (no encontrado), 500 (error), etc...)
  61.         {
  62.             ids= httpReq.responseXML.getElementsByTagName("RUT");  
  63.             items = httpReq.responseXML.getElementsByTagName("NOMBRES");   
  64.             ProcesaItems();
  65.         }
  66.         else    //Se produjo un error
  67.         {
  68.             alert("No se pudo recuperar la información de la lista secundaria: " + httpReq.statusText);
  69.         }
  70.     }
  71. }
  72.  
  73. //Procesa los items del RSS
  74. function ProcesaItems()
  75. {
  76.     listaSec = document.getElementById("mnuSecundario");
  77.     if (listaSec != null)
  78.     {
  79.         listaSec.options.length = 0;    //Vacía la lista de opciones
  80.  
  81.         //Cargamos las nuevas opciones
  82.         for(i=0; i<ids.length; i++)
  83.         {
  84.             var opc = document.createElement("OPTION");
  85.             opc.text = items[i].childNodes[0].nodeValue;
  86.             opc.value = ids[i].childNodes[0].nodeValue;
  87.  
  88.             listaSec.options.add(opc);
  89.             if (ids[i].childNodes[0].nodeValue == 0 || items[i].childNodes[0].nodeValue == 'No se encontraron coincidencias')
  90.             {          
  91.                 listaSec.options[0] = new Option('paso por aqui','0');
  92.                 listaSec.disabled=true; //PARA EL CASO DE NO HABER RESULTADOS INABILITO EL SELECTOR            
  93.             } else {
  94.                 listaSec.disabled=false;   
  95.                 listaSec.options[0].selected = true; //si se encontraron coincidencias marco la primera opcion de la lista como seleccionada               
  96.                
  97.             }
  98.         }
  99.     }
  100. }
  101.  
  102. function CargaPagina(dato){
  103.  
  104.         listaSec = document.getElementById("mnuSecundario");
  105.         var textoSel = '0';
  106.         var valorSel = '0';
  107.  
  108.         if(document.frm_buscar_clientes.mnuSecundario.selectedIndex != -1 && listaSec.value != ''){
  109.                 textoSel = document.frm_buscar_clientes.mnuSecundario.options[document.frm_buscar_clientes.mnuSecundario.selectedIndex].text;
  110.                 valorSel = document.frm_buscar_clientes.mnuSecundario.value;
  111.                 parent.infFrame.location.href = '/clientes/edit/rut/'+valorSel;
  112.             }
  113.  
  114. }
  115. </head>
  116. <div class="outerbox" >
  117.  
  118.     <form
  119.          name="frm_buscar_clientes"
  120.              id="frm_buscar_clientes"
  121.                 method="post"
  122.                     action="/clientes/search/" >
  123.  
  124.         <div class="mainHeading"><h4>Buscador de clientes</h4></div>
  125.            
  126.                 <table cellspacing="0" cellpadding="0" class="data-table" id="clientes-table" >
  127.                     <tr >
  128.                                                 <td scope="col">
  129.                             Filtros
  130.  
  131.                         </td>
  132.                                                 <td scope="col">
  133.                             <input name="filtros" type="radio" value="apellidoPaterno" checked>&nbsp;&nbsp;Apellido Paterno
  134.                         </td>
  135.  
  136.                                                 <td scope="col">
  137.                             <input name="filtros" type="radio" value="rut">&nbsp;&nbsp;Rut            
  138.                         </td>
  139.  
  140.                     </tr>
  141.  
  142.                     <tr>
  143.                         <td colspan="3" style="height:2px"></td>
  144.                     </tr>
  145.                    
  146.                     <tr >
  147.                                                 <td scope="col">
  148.                             Texto a Buscar
  149.  
  150.                         </td>
  151.                                                 <td scope="col" colspan="2">
  152. <input name="mnuPrimario" type="text" id="mnuPrimario" onBlur="javascript:loadItems(this.value)" size="50">
  153. <!--onKeyUp="this.value=this.value.toUpperCase(); if (this.value.length>2){loadItems(this.value);};"-->
  154.                             </td>
  155.  
  156.                     </tr>
  157.                    
  158.                     <tr>
  159.                         <td colspan="3" style="height:2px"></td>
  160.                     </tr>
  161.  
  162.                     <tr >
  163.                                                 <td scope="col">
  164.                             Coincidencias
  165.  
  166.                         </td>
  167.                                                 <td scope="col" colspan="2">
  168.                                                 <select name="mnuSecundario" style="width:300px;" size="5" id="mnuSecundario" onclick="LoadClientesInfo(this.value);" disabled="disabled">
  169.           <option value="">[Ingrese texto a buscar en la caja superior]</option>
  170.         </select>   <input name="hayseleccion" type="hidden" value="0">                     </td>
  171.  
  172.                     </tr>
  173.                    
  174.                 </table>
  175.     </form>
  176.  
  177. </div>
  178. </body>
  179. </html>
  180. <script type="text/javascript">
  181.     <!--
  182.         if (document.getElementById && document.createElement) {
  183.             roundBorder('outerbox');
  184.         }
  185.    -->

ClientesController
Código PHP:
Ver original
  1. public function searchAction()
  2.     {
  3.         $this->_helper->layout->disableLayout();   
  4.  
  5.         $_request = $this->getRequest();
  6.  
  7.         if ($_request->isPost() || $this->getRequest()->isXmlHttpRequest() || $_request->isGet())
  8.         {
  9.             $filtro = $_request->getParam('filtro');
  10.             $criterio = $_request->getParam('criterio');
  11.                
  12.             if (null !== $filtro && null !== $criterio)
  13.             {
  14.                 //instancio el modelo para invocar al metodo
  15.                 $clientes= new Application_Model_Clientes();
  16.                 $this->view->clientes= $clientes->search($filtro, $criterio);
  17.             }
  18.         }
  19.        
  20.     }

ClientesModel
Código PHP:
Ver original
  1. public function search($p1, $p2)
  2.      {
  3.         return $this->getMapper()->search($p1, $p2);
  4.      }

ClientesMapper
Código PHP:
Ver original
  1. public function search($p1, $p2, Application_Model_Pacientes $pacientes)
  2.     {
  3.         $select = $this->getDbTable()->select()
  4.                                     ->from(array('pac' => 'pacientes'),
  5.                                            array('rut'=>'pac.rut',
  6.                                                  'nombre'=>'CONCAT(pac.nombres," ",pac.apellidoPaterno)'))
  7.                                     ->where($p1.' LIKE ?', '%'.$p2.'%')
  8.                                     ->order('apellidoPaterno ASC');
  9.  
  10. /*      $resultSet = $this->getDbTable()->fetchAll($select);                                   
  11.         if (0 == count($resultSet)) {
  12.             return;
  13.         }      
  14.         $row = $resultSet->current();
  15.         $pacientes->setRut($row->rut);
  16.         $pacientes->setNombres($row->nombre);*/
  17.        
  18.        return $this->getDbTable()->fetchAll($select);
  19.     }

No me funciona.

Última edición por Fenris; 02/02/2011 a las 10:45