Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- puedes complementar este ejemplo con:
- http://trucosdeprogramacionmovil.blogspot.com/2012/12/script-vs-cclientscript-yiiframework.html
- ----------- ProductoController.php ----------
- public function actionListadoAjax(){
- // funciona via POST a proposito para saber.
- $idproducto = $_POST['idproducto'];
- $listado = Producto::model()->findAllByAttributes(
- array('idproducto'=>$idproducto));
- if($listado == null){
- header("Content-type: application/json");
- echo CJSON::encode(array('ok'=>false,
- 'mensaje'=>'idproducto no tiene items',
- 'listado'=>null));
- }
- else {
- header("Content-type: application/json");
- echo CJSON::encode(array('ok'=>true,
- 'mensaje'=>'', 'listado'=>$listado));
- }
- // si ocurre una excepcion o si se emite una
- // entonces caera en la seccion "error" del
- // cuerpo de las opciones del elemento $.ajax:
- // throw new Exception('pruebame');
- }
- ---------------- index.html -----------------
- <div>
- <input type='button' id='miboton' value='Probar'></input>
- <div id='listado-va-aqui'>...</div>
- </div>
- <script>
- $.fn.imprimeListado = function(listado, selector){
- selector.html('Listado:');
- selector.append('<UL></UL>');
- var ul = selector.find('ul');
- $.each(listado, function(key,item){
- // item dependera de tu modelo Producto.php
- var li = "<li alt='"+item.id+"'>"+item.nombreProducto+"</li>";
- ul.append(li);
- li = ul.find("[alt='"+item.id+"']");
- li.click(function(){
- var _item = $(this).attr('alt');
- alert("hola hiciste click en el item: "._item);
- });
- });
- }
- $.fn.leerProductos = function(idproducto, selector){
- selector.html("<img src='images/loading.gif'/>");
- $.ajax({
- url: 'index.php?r=/producto/listadoajax/',
- type: 'post',
- cache: false,
- data: { idproducto: idproducto },
- success: function(resp){
- if(resp.ok == true){
- $.fn.imprimeListado(resp.listado, selector);
- }else{
- selector.html(resp.mensaje);
- }
- },
- error: function(e){
- selector.html(e.responseText);
- }
- });
- }
- $('#miboton').click(function(){
- $.fn.leerProductos(123, $('#listado-va-aqui'));
- });
- </script>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement