2011-10-09 3 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 

またにlastNameのフィールドを追加する必要があります。

'fields' => array(
        'firstName', 
        'lastName', 
       ) 
私は、この仮想フィールドに適用すべきである
関連する問題