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

    评论

报告相同问题?