使用JavaScript在yii中自动完成

使用JavaScript在yii中自动完成,javascript,php,jquery,yii,Javascript,Php,Jquery,Yii,我是Yii的新手。我从控制器获取值。我已将其存储在变量中,并执行了自动完成功能。如果我提醒变量,它将显示值。但在实际代码中,自动完成框表示为空。请帮我把这个修好 提前谢谢 我的观点: <!--Content--> <div id="content"> <div style="padding: 10px;"> <a href="<?php echo $this->createUrl('/Nimsoft/create')

我是Yii的新手。我从控制器获取值。我已将其存储在变量中,并执行了自动完成功能。如果我提醒变量,它将显示值。但在实际代码中,自动完成框表示为空。请帮我把这个修好

提前谢谢

我的观点:

<!--Content-->

<div id="content">
    <div style="padding: 10px;">
        <a href="<?php echo $this->createUrl('/Nimsoft/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('/Nimsoft/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}',)



),
)); 
$customer_names = array();

foreach ($user as $row) {
    $customer_names[] = array_values($row);
}

json_encode($customer_names);

?>

       </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 json_encode($customer_names);?>;

      /*"ActionScript",
      "AppleScript",
      "Asp",
      "BASIC",
      "C",
      "C++",
      "Clojure",
      "COBOL",
      "ColdFusion",
      "Erlang",
      "Fortran",
      "Groovy",
      "Haskell",
      "Java",
      "JavaScript",
      "Lisp",
      "Perl",
      "PHP",
      "Python",
      "Ruby",
      "Scala",
      "Scheme"*/
   alert(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>


要将json转换为js数组,请在代码中尝试下面的代码

availableTags = $.parseJSON(availableTags);

有人可以更新这个吗?js控制台中有错误吗?在变量中,我有所有的数组元素,但我的建议框显示空白,您是作为json传递的,但您的代码需要js数组。没有json对象。我尝试了很多把它转换成JS数组,但是我做不到