简体   繁体   English

选择框中的名称顺序不适用于谷歌浏览器和IE浏览器

[英]order by name in select box is not working in google chrome and IE

I have a function which is returning the list of city names associated to city_id ordered by city name. 我有一个函数,它返回与城市名称排序的city_id相关联的城市名称列表。

I want to show them in a select box. 我想在选择框中显示它们。 In Firefox (23.0.1) it is working fine even with order. 在Firefox(23.0.1)中,即使订单也能正常工作。 But in case of IE (10) and chrome (29.0.1547.66 m) the order is not correct. 但是对于IE(10)和chrome(29.0.1547.66 m),订单不正确。 I am using PHP and zend framework. 我正在使用PHP和zend框架。 My code: 我的代码:

$cities = $countryCityModel->getCities($country);
 print json_encode(array('status'=>'Success',
                         'data'=>$cities)
                  );
 public function getCities($countryId){
    if(!$countryId)
    return false;
    $mdb = $this->getOldDbAdapter(); 
    $sql = $mdb->select()
               ->from('cities', array('city_id','city'))
               ->where('country_id = ?', $countryId)
               ->order("city");
    return $mdb->fetchPairs($sql);
 }
$.ajax({
 url : baseUrl + '/lead/index/get-cities',
 dataType : 'json',
 data : {
          country:country_id
        },
        beforeSend : function(){
          $("#holder_popup").show();
        },
        complete: function(){
          $("#holder_popup").hide();   
        },
        success : function(res) {
           var sbuOptions = "<option value=''>--SELECT--</option>"
           if(res.status == 'Success'){
             for(i in res.data){
               sbuOptions += "<option value='"+i+"'>"+res.data[i]+"</option>";
             }
             $("#city").html(sbuOptions);

           }else{
              $("#city").html(sbuOptions);
              alert(res.msg);
           }
        },
        error : function(jqXHR, exception) {                                
            }
        });

The returned value is like the following: 返回值如下所示:

{

    "status":"Success",
    "data":{
        "53029":"DURRES",
        "53331":"ELBASAN",
        "40239":"FIER",
        "16235":"KAMEZ",
        "42191":"KAVAJE",
        "41375":"KUKES",
        "53581":"PESHKOPI",
        "57686":"SHIJAK",
        "56756":"SHKODER",
         "4496":"TIRANA",
        "41342":"VLORE",
        "19454":"VORE"
    }

}

Please help me, how to resolve this issue? 请帮帮我,如何解决这个问题?

can you update your code to this 你可以更新你的代码吗?

public function getCities($countryId)
{
    $resultArrary = array();
    if($countryId)
    {
        $mdb = $this->getOldDbAdapter(); 
        $sql = $mdb->select()
                   ->from('cities', array('city_id','city'))
                   ->where('country_id = ?', $countryId)
                   ->order("city");
        $result = $mdb->fetchAll($sql);

        foreach($result as $key => $city )
        {
            $resultArrary[$key]['id'] =  $city['city_id'];
            $resultArrary[$key]['city'] =  $city['city'];
        }
    }

    return $resultArrary;
 }

for(i in res.data)
{
    cityData = res.data[i];
    sbuOptions += "<option value='"+cityData.id+"'>"+cityData.city+"</option>";
}

Seems like chrome is auto sorting the json object with the index. 看起来像chrome是用索引自动排序json对象。

REF: REF:
Chrome and IE sorts JSON Object automatically, how to disable this? Chrome和IE会自动排序JSON对象,如何禁用它?
https://code.google.com/p/chromium/issues/detail?id=37404 https://code.google.com/p/chromium/issues/detail?id=37404

您在第12行缺少ASC或DESC

->order("city ASC");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM