简体   繁体   English

使用JavaScript在yii中自动完成

[英]auto-complete in yii using JavaScript

I am new to Yii. 我是Yii的新手。 I get the values from the Controller. 我从控制器获取值。 I have stored it in a variable and have performed auto complete function. 我将其存储在变量中,并执行了自动完成功能。 If I alert the variable, it is showing the values. 如果我警告变量,它将显示值。 But in the real code, the auto complete box suggests empty. 但是在实际代码中,“自动完成”框建议为空。 Please help me to fix this. 请帮助我解决此问题。

Thanks in advance. 提前致谢。

My view: 我的观点:

<!--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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM