2011-10-09 30 views
0

目前我的自動完成的作品中顯示用戶的名字,但我想連接的第一個名字,姓氏等如何在下面的代碼實現這一點?連接多個領域CakePHP中使用AJAX的自動完成功能顯示

<script type="text/javascript"> 
    $(function() { 
     $(".suggest").autocomplete({ 
      source: function(request, response) { 
       $.ajax({ 
        url: '<?php echo $this->Html->url(array('controller' => 'searches', 'action' => 'suggestUser')); ?>', 
        dataType: "json", 
        data: { 
         //request.term is the value of the current textbox. 
         term: request.term 
        }, 
        success: function(data) { 
         response($.map(data, function(item) { 
          return { 
           label: item.User.firstName, 
           value: item.User.firstName 

          } 
         })); 
        } 
       }); 
      }, 
      minLength : 1 
     }); 
    }); 

在我的控制器,以下代碼下面是我的邏輯來搜索字段。

function suggestUser() { 

     if (isset($_GET["term"])) { 
      $term = $_GET["term"]; 

      $result = $this->User->find('all', array(
       'conditions' => array(
        'User.firstName LIKE' => $term . '%' 
       ), 
       'fields' => array(
        'firstName' 
       ) 
        )); 
      if ($term) { 
       $this->set('results', $result); 
       $this->view = 'Json'; 
       $this->set('json', 'results'); 
      } 
     } 
    } 

回答

0

這是非常簡單的,我意識到。我設法解決它。

label: item.User.firstName + " " + item.User.lastName, 
    value: item.User.firstName + " " + item.User.lastName 

你還需要將姓氏字段追加英寸

'fields' => array(
        'firstName', 
        'lastName', 
       ) 
相關問題