2014-02-17 42 views
-3

我是Yii的新手,我想從我的控制器傳遞PHP變量值來查看。有人可以幫助這一點。我被困在這裏。我必須使用JavaScript在自動完成字段中計算此變量。提前致謝。通過PHP變量值在YII中查看頁面

我的控制器動作:

public function actionIndex() 
    { 

      $user = Yii::app()->db->createCommand() 
    ->select('cust_name') 
    ->from('mst_customers') 
    ->queryAll(); 

     $dataProvider=new CActiveDataProvider('model_name'); 

       $this->render('index',array(
      'dataProvider'=>$dataProvider, 



     )); 

    } 

我的觀點:

<!--Content--> 

<div id="content"> 
    <div style="padding: 10px;"> 
     <a href="<?php echo $this->createUrl('/controller_name/create');?>" title="Create New Host" class="btn btn-primary circle_ok" style="text-decoration: none;" >Add New Host to Customer</a> 

    <div style="float:right"> 
         <?php 
          echo CHtml::link('Upload Customer CSV', array('/controller_name/uploadCustomers'), array(
          'onclick'=>'return hs.htmlExpand(this, { objectType: "iframe", wrapperClassName: "full-size", align: "center" })', 
          'class'=>'btn btn-primary', 
          'id'=>'upload_link', 
          )); 
         ?>       
        </div> 
    </div> 
    <h3><?php echo htmlspecialchars($title); ?></h3> 
    <h3><?php echo $title; ?></h3> 


    <div class="innerLR"> 
     <div class="row-fluid"> 
<?php 

$obj=$this->widget('zii.widgets.grid.CGridView', array(
    'dataProvider'=>$dataProvider, 


    //'afterAjaxUpdate'=>'\'changeTRColor()\'', 
    //'itemView'=>'_view', 
    'columns'=>array(

       array(   // display 'create_time' using an expression 
          'name'=>'name', 
              'value'=>'$data->host_name', 
       ), 
       array(
          'name'=>'serviceId', 
          'value'=>'$data->host_serviceid', 
       ), 

       array(
              'name'=>'customer', 
              'value'=>'$data->customers->cust_name', 
       ), 
       array(
            'class'=>'CButtonColumn', 
            'template'=>'{delete}{update}',) 



), 
)); 

?> 

     </div> 

<html lang="en"> 
<head> 
    <meta charset="utf-8"> 
    <title>jQuery UI Autocomplete - Default functionality</title> 
    <link rel="stylesheet" href="//code.jquery.com/ui/1.10.4/themes/smoothness/jquery-ui.css"> 
    <script src="//code.jquery.com/jquery-1.9.1.js"></script> 
    <script src="//code.jquery.com/ui/1.10.4/jquery-ui.js"></script> 
    <link rel="stylesheet" href="/resources/demos/style.css"> 
    <script type="text/javascript"> 
//autocomplete 
    $(function() { 
    var availableTags =<?php echo CJSON::encode($user);?>; 

     /*"ActionScript", 
     "AppleScript", 
     "Asp", 
     "BASIC", 
     "C", 
     "C++", 
     "Clojure", 
     "COBOL", 
     "ColdFusion", 
     "Erlang", 
     "Fortran", 
     "Groovy", 
     "Haskell", 
     "Java", 
     "JavaScript", 
     "Lisp", 
     "Perl", 
     "PHP", 
     "Python", 
     "Ruby", 
     "Scala", 
     "Scheme"*/ 

    console.log(availableTags); 
    $("#Search").autocomplete({ 
     source: availableTags 
    }); 
    }); 
    </script> 
</head> 
<body> 

<div class="ui-widget"> 
    <label for="Search">Search Customer: </label> 
    <input id="Search"> 
</div> 


</body> 
</html> 

     <div class="separator bottom"></div> 
    </div> 
</div> 
<!-- // Content END --> 
<div class="clearfix"></div> 
<!-- // Sidebar menu & content wrapper END --> 

<div id="footer" class="hidden-print"> 
<?php $this->renderPartial('application.views.layouts._footer_inc'); ?> 
</div> 
+0

你想傳遞給你的視圖文件是什麼? –

回答

1
$this->render('index',array(
     'dataProvider'=>$dataProvider, 
     'user' => $user, 
)); 
+0

接受我的兩個答案:D – tinybyte

+0

我可以得到數組元素,但主要的威脅是我必須使用javascript自動完成這些元素,我該怎麼做? – user3107044

+0

我認爲你必須創建一個沒有鍵索引的數組,如果你var_dump()該數組,你將會看到你擁有什麼樣的數組,只需運行一個foreach循環並且創建一個沒有鍵索引的新數組。 – tinybyte