Advertisement
christiansalazarh

usando ajax

Jan 16th, 2013
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.36 KB | None | 0 0
  1. puedes complementar este ejemplo con:
  2. http://trucosdeprogramacionmovil.blogspot.com/2012/12/script-vs-cclientscript-yiiframework.html
  3.  
  4. ----------- ProductoController.php ----------
  5. public function actionListadoAjax(){
  6.   // funciona via POST a proposito para saber.
  7.   $idproducto = $_POST['idproducto'];
  8.   $listado = Producto::model()->findAllByAttributes(
  9.      array('idproducto'=>$idproducto));
  10.   if($listado == null){
  11.     header("Content-type: application/json");
  12.     echo CJSON::encode(array('ok'=>false,
  13.       'mensaje'=>'idproducto no tiene items',
  14.          'listado'=>null));
  15.   }
  16.   else {
  17.     header("Content-type: application/json");
  18.     echo CJSON::encode(array('ok'=>true,
  19.       'mensaje'=>'', 'listado'=>$listado));
  20.   }
  21.   // si ocurre una excepcion o si se emite una
  22.   // entonces caera en la seccion "error" del
  23.   // cuerpo de las opciones del elemento $.ajax:
  24.   // throw new Exception('pruebame');
  25. }
  26.  
  27. ---------------- index.html -----------------
  28. <div>
  29.   <input type='button' id='miboton' value='Probar'></input>
  30.   <div id='listado-va-aqui'>...</div>
  31. </div>
  32. <script>
  33.    $.fn.imprimeListado = function(listado, selector){
  34.       selector.html('Listado:');
  35.       selector.append('<UL></UL>');
  36.       var ul = selector.find('ul');
  37.       $.each(listado, function(key,item){
  38.          // item dependera de tu modelo Producto.php
  39.          var li = "<li alt='"+item.id+"'>"+item.nombreProducto+"</li>";
  40.          ul.append(li);
  41.          li = ul.find("[alt='"+item.id+"']");
  42.          li.click(function(){
  43.              var _item = $(this).attr('alt');
  44.              alert("hola hiciste click en el item: "._item);
  45.          });
  46.       });
  47.    }
  48.    
  49.    $.fn.leerProductos = function(idproducto, selector){
  50.        selector.html("<img src='images/loading.gif'/>");
  51.        $.ajax({
  52.           url: 'index.php?r=/producto/listadoajax/',
  53.           type: 'post',
  54.           cache: false,
  55.           data: { idproducto: idproducto },
  56.           success: function(resp){
  57.              if(resp.ok == true){
  58.                $.fn.imprimeListado(resp.listado, selector);
  59.              }else{
  60.                selector.html(resp.mensaje);
  61.              }
  62.           },
  63.           error: function(e){
  64.              selector.html(e.responseText);
  65.           }
  66.        });
  67.    }
  68.    $('#miboton').click(function(){
  69.        $.fn.leerProductos(123, $('#listado-va-aqui'));
  70.    });
  71. </script>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement