YII AJAX registerScript

1.YII封装的CHtml::ajaxLink,QuoteController.php

Java代码  

  1. <?php  
  2. class QuoteController extends Controller  
  3. {  
  4.     private $quotes = array(  
  5.         array('Walking on water and developing software from a specification are easy if both are frozen.', 'Edward V Berard'),  
  6.         array('It always takes longer than you expect, even when you take into account Hofstadter&rsquo;s Law.', 'Hofstadter&rsquo;sLaw'),  
  7.         array('Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live.','Rick Osborn'),  
  8.         array('I have always wished for my computer to be as easy to use as my telephone; my wish has come true because I can no longer figure out how to use my telephone.', 'Bjarne Stroustrup'),  
  9.         array('Java is to JavaScript what Car is to Carpet.', 'Chris Heilmann'),  
  10.     );  
  11.     private function getRandomQuote()  
  12.     {  
  13.         return $this->quotes[array_rand($this->quotes, 1)];  
  14.     }  
  15.     function actionIndex()  
  16.     {  
  17.         $this->render('index', array(  
  18.         'quote' => $this->getRandomQuote()  
  19.         ));  
  20.     }  
  21.     function actionGetQuote()  
  22.     {  
  23.         $this->renderPartial('_quote', array(  
  24.         'quote' => $this->getRandomQuote(),  
  25.         ));  
  26.     }  
  27. }  

view/index.php

Java代码  

  1. <h2>Quote of the day</h2>  
  2. <div id="quote-of-the-day">  
  3. &ldquo;<?php echo $quote[0]?>&rdquo;, <?php echo $quote[1]?>  
  4. </div>  
  5. <?php Yii::app()->clientScript->registerCoreScript('jquery'); ?>  
  6. <?php echo CHtml::ajaxLink('Next quote', array('getQuote'),  
  7. array('update' => '#quote-of-the-day'))?>  

 ajaxLink($text,$url ,$ajaxOptions=array(),$htmlOptions=array())

$text 链接内容

以下是调试.也可以用常用的jQuery.ajax

Java代码  

  1. <?php echo CHtml::ajaxLink('Next quote', array('getQuote'),  
  2. array('success' => 'js:function(data){  
  3. alert(data);  
  4. }'))?>  

 

2.将php数据转化成JavaScript数据

protected/config/main.php:

Java代码  

  1. 'params'=>array(  
  2.     // this is used in contact page  
  3.     'adminEmail'=>'webmaster@example.com',  
  4.     'alert' => array(  
  5.         'enabled' => true,  
  6.         'message' => 'Hello there!',  
  7.     ),  
  8. ),  

protected/controllers/AlertController.php

Java代码  

  1. <?php  
  2. class AlertController extends Controller  
  3. {  
  4.     function actionIndex()  
  5.     {  
  6.         $config = CJavaScript::encode(Yii::app()->params->toArray());  
  7.         //$config:{'adminEmail':'webmaster@example.com','alert':{'enabled':true,'message':'Hello there!'}}  
  8.         Yii::app()->clientScript->registerScript('appConfig', "var config = ".$config.";",CClientScript::POS_HEAD);  
  9.         $this->render('index');  
  10.     }  
  11. }  

registerScript第二个参数是显示js代码。

Java代码  

  1. <?php  
  2. Yii::app()->clientScript->registerScript('search', "  
  3. $('.search-button').click(function(){  
  4.     $('.search-form').toggle();  
  5.     return false;  
  6. });  
  7. $('.search-form form').submit(function(){  
  8.     $.fn.yiiGridView.update('project-grid', {  
  9.         data: $(this).serialize()  
  10.     });  
  11.     return false;  
  12. });  
  13. ");  
  14. ?>  

protected/views/alert/index.php

Java代码  

  1. <script>  
  2. if(config && config.alert && config.alert.enabled &&  
  3. config.alert.message){  
  4. alert(config.alert.message);  
  5. }  
  6. </script>  

CJSON::encode()=CJavaScript::encode   生成json格式
CJSON::decode()

时间: 2024-08-27 23:35:35

YII AJAX registerScript的相关文章

Yii框架结合sphinx,Ajax实现搜索分页功能示例_php实例

本文实例讲述了Yii框架结合sphinx,Ajax实现搜索分页功能的方法.分享给大家供大家参考,具体如下: 效果图: 控制器: <?php namespace backend\controllers; use Yii; use yii\web\Controller; use yii\data\Pagination; use SphinxClient; use yii\db\Query; use yii\widgets\LinkPager; use backend\models\Goods; cl

Yii使用ajax验证显示错误messagebox的解决方法_php实例

本文实例讲述了Yii使用ajax验证显示错误messagebox的解决方法.分享给大家供大家参考.具体方法如下: yii 自带了ajax 表单验证 这个可能有些朋友不知道了,但我今天在使用yii 自带的ajax 表单验证 时碰到一些问题,下面我来整理例子与大家参考一下. 在Yii中,可以利用ajax执行一个action,但是这个action有时候会有弹出错误讯息的需求,这时候的处理方式如下 基本思想 利用exception,比如: 复制代码 代码如下: throw new CHttpExcept

PHP开发框架Yii Framework教程(41) Zii组件-Tabs示例

CJuiTabs 显示分页UI组件,和Yii Framework 开发教程(17) UI 组件 TabView示例功能类似,它封装了 JUI tabs插件. 前基本用法如下: <?php $this->widget('zii.widgets.jui.CJuiTabs', array( 'tabs'=>array( 'Static tab'=>'Static content', 'Render tab'=>$this->renderPartial('pages/_cont

tabpanel-怎么把extjs 的TabPanel加入到自定义的div中

问题描述 怎么把extjs 的TabPanel加入到自定义的div中 我自己在主页面jsp上布局了格式,想把他的TabPanel加到自己定义的div中.给出详细代码!谢谢 Ext.onReady(function(){ var centerRegion = new Ext.TabPanel({ region : 'center' margins : '3 3 3 0'//距离top.right.bottom.left边界的距离单位为像素 activeTab : 0 defaults : { au

ajax翻页效果模仿yii框架自己写的

 这篇文章主要介绍了自己写的ajax翻页效果,模仿yii框架,需要的朋友可以参考下  代码如下: <!DOCTYPE html>  <html>  <head>  <title>ajax分页</title>  <script src="http://code.jquery.com/jquery-1.11.0.min.js"></script>  <script>  function load

yii ajax-yii框架里怎么实现Jquery Ajax方法传值到控制器action方法里

问题描述 yii框架里怎么实现Jquery Ajax方法传值到控制器action方法里 $.ajax({ type:'post', url:"/www/index.php?r=home/CkUser",//地址写什么才是对的 data:{"name":val}, success:function(msg){ alert("正确"); $("#nameinfo").html(msg); }, 解决方案 地址直接写根下的控制器下的

使用Yii整合的pjax(pushstate+ajax)实现无刷新加载页面_AJAX相关

Pjax是啥? Pjax = history.pushState + Ajax = history.pushState + Async JS + XML(xhr?) BOM对象history被增强了一波,主要是对历史栈的操作,以前只有 replace , go 之类的,都会跳转并刷新整个页面,现在有了 pushState , replaceState 等等单纯操作历史栈的方法,只是单纯修改历史栈里的内容,没有副作用(页面不会跳转刷新) PJAX效果 通过url可以跟踪ajax的动态加载内容.这种

yii gridview 内容怎样通过ajax请求获取

问题描述 yii gridview 内容怎样通过ajax请求获取 yii gridview 内容怎样通过ajax请求获取 yii gridview 内容怎样通过ajax请求获取

Yii基于CActiveForm的Ajax数据验证用法示例_php实例

本文实例讲述了Yii基于CActiveForm的Ajax数据验证用法.分享给大家供大家参考,具体如下: 1. 视图定义form表单开启ajax验证 $form = $this->beginWidget('CActiveForm', array( 'id'=>'zdzone-form', 'enableAjaxValidation'=>true,//开启ajax验证 'enableClientValidation'=>true,//开启客户端验证,生成js 'action'=>