2012-08-29 30 views
1

我正在使用symfony sfFormExtra插件附帶的jquery自動完成插件。symfony 1.4 sformextra自動完成排序順序

/* 
* jQuery Autocomplete plugin 1.1 
* 
* Copyright (c) 2009 Jörn Zaefferer 
* 
* Dual licensed under the MIT and GPL licenses: 
* http://www.opensource.org/licenses/mit-license.php 
* http://www.gnu.org/licenses/gpl.html 
* 
* Revision: $Id: jquery.autocomplete.js 15 2009-08-22 10:30:27Z joern.zaefferer $ 
*/ 

它似乎根據關鍵字排序json結果,但我找不到在代碼中的排序選項的任何提及。

我想從服務器返回排序結果。

的結果是建立這樣

public function doMemberSelectForSelectById($q, $limit){ 

    $query = $this->retrieveMemberOrganisations($this->createUnrestrictedQuery('o'))-> 
     andWhere("o.name like '%".$q."%'")-> 
     orderBy("o.name")-> 
     limit(intval($limit)); 

    $org_data = $query->execute(); 
    $orgs = array(); 
    foreach ($org_data as $org){ 
     $orgs[$org->getId()] = $org->getName(); 
    } 
    return $orgs; 
} 

$orgs = Doctrine_Core::getTable('Organisation')-> 
    doMemberSelectForSelectById($request->getParameter('q'), $request->getParameter('limit')); 
echo $this->renderText(json_encode($orgs)); 
    } 
return sfView::NONE; 

在Chrome瀏覽器開發工具的結果(按名稱排序)

{ 
    "1781": "1st Mechanical \/ 1st Maintenance", 
    "1771": "Acco Building Ltd", 
    "203": "Active Welding Limited", 
    "443": "Aircon Commissioning & Services Ltd", 
    "588": "Akon Electrical Engineering Limited", 
    "625": "Alaska Interiors Ltd", 
    "796": "Alutech Windows & Doors Ltd", 
    "584": "Arrow International Ltd".... 
} 

觸發它是在這裏

jQuery(document).ready(function() { 

    jQuery("#autocomplete_rsvpCompany1").focus(function({ 
     jQuery(this).val('');}); 
      jQuery("#autocomplete_rsvpCompany1").autocomplete(
       'http://www.nzgbc.org.nz/index.php?option=com_nzgbc_member&uri=%2Forganisation%2FjsonListMember%2Faction', 
       jQuery.extend({}, { 
        dataType: 'json', 
        minChars: 0, 
        delay:0, 
      max:700, 
        scroll: true, 
        parse: function(data) { 
       var parsed = []; 
       for (key in data) { 
          parsed[parsed.length] = { data: [ data[key], key ], value: data[key], result: data[key] }; 
       } 
       return parsed; 
       } 
     }, { }) 
     ).result(function(event, data) { 
      jQuery('#rsvpCompany1').val(data[1]); 
     }); 

的JavaScript我怎樣才能做到這一點?

+1

您可以添加更多信息,例如您給autocompleter的結果,autocompleter發送給用戶的結果。對我來說不是很清楚。 – j0k

+0

已更新的問題代碼 – jdog

回答

1

你應該改變「解析」的方法如下:

parse: function(data) { 
    var parsed = []; 
    for (key in data) { 
    parsed[parsed.length] = { 
     data: [ data[key], key ], 
     value: data[key], 
     result: data[key] 
    }; 
    } 
    parsed.sort(function (a, b){ 
    var aKey = a.value; 
    var bKey = b.value; 
    return ((aKey < bKey) ? -1 : ((aKey > bKey) ? 1 : 0)); 
    }); 
    return parsed; 
} 

的問題是「數據」數組有一個數字鍵,所以當你遍歷所有的「數據」中的元素你的「對於「循環」,您將以數字順序(例如:203,443,584,588等)將元素添加到「解析」數組中,因此在將所有元素添加到「解析」數組後,您必須按「數據[鍵]」排序,而不是按「鍵」。

+0

感謝 - 不感興趣是否有辦法強制密鑰爲字符串,即使它包含數字? – jdog

+0

在我看來不行,但我不是100%肯定的。也許你應該爲所有的鍵添加一個字母前綴(例如:「1000」=>「X1000」),但是你也應該刪除收到請求服務器端的前綴。 – 2012-09-14 06:30:55