weixin_33725515 2016-03-18 08:07 采纳率: 0%
浏览 8

cakephp 2.6中的自动完成

I am trying to implement auto complete functionality in my application. But instead of getting suggestions it is showing java script lines of my code. For example: if I type 'n', then it shows all java script lines having 'n'as a character. I have pasted all the required .js and .css files in their respective folders. I don't where I am lacking. Please help me. I am pasting my controller and view part.

Controller:

 public function find_name() {
    if ($this->request->is('ajax')) {

        $this->autoRender = false;            
        $panel_name = $this->request->query('term');            
        $results = $this->Panel->find('all', array(
                                       'conditions' => array('Panel.name LIKE ' => '%' . $panel_name . '%'),
                                       'recursive'  => -1
                                       ));

        $resultArr = array();
        foreach($results as $result) {
           $resultArr[] = array('label' =>$result['Panel']['name'] , 'value' => $result['Panel']['name'] );
        }
        echo json_encode($resultArr);                                   
   }`

View:

$('.pchange').each(function()
{        
    var g=$(this).attr('id');
    //alert(g);
    var lastChar = g.replace ( /[^\d.]/g, '' );
    $('#name'+lastChar).autocomplete("PanelsController/find_name", 
    {
        minChars:       1,
        delay:          0,
        maxCacheLength: 100,
        onItemSelect: 
        function (item)
        {   
            alert(item);
            $("#name"+lastChar).val(item.data[0]);
            $("#phone"+lastChar).val(item.data[1]);
            $("#email"+lastChar).val(item.data[2]);
        }
    });  
});
  • 写回答

1条回答 默认 最新

  • weixin_33716557 2016-03-18 08:38
    关注

    Please try this . you have not exit the json data

    
        public function find_name() {
            if ($this->request->is('ajax')) {
    
                $this->autoRender = false;            
                $panel_name = $this->request->query('term');            
                $results = $this->Panel->find('all', array(
                                               'conditions' => array('Panel.name LIKE ' => '%' . $panel_name . '%'),
                                               'recursive'  => -1
                                               ));
    
                $resultArr = array();
                foreach($results as $result) {
                   $resultArr[] = array('label' =>$result['Panel']['name'] , 'value' => $result['Panel']['name'] );
                }
                echo json_encode($resultArr); 
               exit;                                  
        }
    
    
    
        $(".autocomplete").autocomplete({
            source: function( request, response ) {
                $.ajax({
                  url: "<?php echo $this->Html->url(array('action' => 'find_name')); ?&gt",
                  dataType: "json",
                  data: {
                    q: request.term
                  },
                  success: function( data ) {
                    response( data );
                  }
                });
              },
              minLength: 1,
              select: function( event, ui ) {
    
              },
              open: function() {
                $( this ).removeClass( "ui-corner-all" ).addClass( "ui-corner-top" );
              },
              close: function() {
                $( this ).removeClass( "ui-corner-top" ).addClass( "ui-corner-all" );
              }
        }).data( "ui-autocomplete" )._renderItem = function( ul, item ) {
    
             return $( "<li&gt" )
            .data( "ui-autocomplete-item", item )
            .append("<a href=javascript:void(0);&gt" + item.label + "</a&gt")
            .appendTo( ul );
        };
    
    
    

    Please also include jquery-ui.js and its css

    评论

报告相同问题?

悬赏问题

  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么