【问题标题】:Json encode autocomplete sourceJson 编码自动完成源
【发布时间】:2014-01-11 11:13:07
【问题描述】:

我通常有这样的数组

(int) 0 => abc,
(int) 1 => def,
(int) 2 => ghi

如果我使用 json_encode 它将变成

["abc", "def", "ghi"]

这对于 jquery 自动完成非常有用

$(function() {
    var availableTags = <?php echo json_encode($companyList); ?>;
    $("#CompanyName").autocomplete({
        source: availableTags,
        delay: 10
    });
}); 

但是现在,我需要更多数据,我看一下 jquery 自动完成示例,数据必须看起来像这样

var company = [
  {
    value: "jquery",
    label: "jQuery",
    name: "the write less, do more, JavaScript library",
    address: "jquery_32x32.png",
    city: "xxxxx",
  },
  {
    value: "jquery-ui",
    label: "jQuery UI",
    name: "the write less, do more, JavaScript library",
    address: "jquery_32x32.png",
    city: "xxxxx",
  },
  {
    value: "sizzlejs",
    label: "Sizzle JS",
    name: "the write less, do more, JavaScript library",
    address: "jquery_32x32.png",
    city: "xxxxx",
  }
];

我该如何做 json_encode(或任何其他方式)来更改这样的 PHP 数组

array(
    (int) 0 => array(
        'Company' => array(
            'id' => '19',
            'group_id' => '1',
            'name' => 'Harts Harts',
            'address' => 'xxx NE xxxth Street',
            'city' => 'Shoreline',
            'state' => 'WA',
            'zip' => '98155',
            'country' => '',
        )
    ),
    (int) 1 => array(
        'Company' => array(
            'id' => '21',
            'group_id' => '1',
            'name' => 'Andy Robin',
            'address' => 'xxx xxxth Ave NE',
            'city' => 'Bellevue',
            'state' => 'WA',
            'zip' => '98004',
            'country' => '',
        )
    )
)

变成类似于jquery自动完成源的东西,因为如果我直接使用json_encode($company),它会变成对象,我不能用它来自动完成。

这个数组将有大约 2500 个数据 谢谢

【问题讨论】:

    标签: php jquery json cakephp-2.0 jquery-autocomplete


    【解决方案1】:
    $return_arr = array();
    
    $company_arr = array(
        (int) 0 =>  array(
            'id' => '19',
            'group_id' => '1',
            'name' => 'Harts Harts',
            'address' => 'xxx NE xxxth Street',
            'city' => 'Shoreline',
            'state' => 'WA',
            'zip' => '98155',
            'country' => '',
        ),
        (int) 1 => array(
            'id' => '21',
            'group_id' => '1',
            'name' => 'Andy Robin',
            'address' => 'xxx xxxth Ave NE',
            'city' => 'Bellevue',
            'state' => 'WA',
            'zip' => '98004',
            'country' => '',
        )
    );
    
    foreach ($company_arr as &$company) {
        array_push($return_arr,$company);
    }
    
    
    echo json_encode($return_arr);
    

    输出:

    [{"id":"19","group_id":"1","name":"Harts Harts","address":"xxx NE xxxth Street","city":"Shoreline","state":"WA","zip":"98155","country":""},{"id":"21","group_id":"1","name":"Andy Robin","address":"xxx xxxth Ave NE","city":"Bellevue","state":"WA","zip":"98004","country":""}]
    

    http://codepad.org/dQ8r52Eq

    注意:要用于 jquery 自动完成,您需要在返回的 json 中添加 valuelabel 字段。您目前没有这两个字段。

    【讨论】:

    • 这种方式我觉得它会由于大量数据(2500ish数据)而导致问题。页面加载时会很慢
    • @Hart 我使用相同的代码,美国邮政编码一次拉几千,但如果您在页面上运行其他内容,那么可能是这样。
    • @Harts 如果您从数据库中提取自动竞争,您可以在此处使用我的帖子示例:jensbits.com/2010/03/29/…
    【解决方案2】:

    使用 jQuery parseJSON 方法解析你从 PHP 得到的 json。

    var obj = jQuery.parseJSON( 'json_string_here' );
    

    【讨论】:

      【解决方案3】:

      api jquery-ui:autocomplete

      读取自动完成的api后:

      $companyList = array();
      foreach($companies as $company){
          array_push( $companyList, $company["Company"]["name"]);
      }
      

      返回:

      ["Harts Harts","Andy Robin"]
      

      自动完成功能将与此计划旧数组或对象属性数组 labelvalue 一起使用,如下所示:

      [{label:"Harts Harts",value:"Harts Harts"},{label:"Andy Robin",value:"Andy Robin"}]
      

      如果您还有其他问题,请随时提出。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2012-03-10
        • 2011-02-15
        • 1970-01-01
        • 2013-07-11
        • 2019-02-13
        • 2011-12-04
        • 1970-01-01
        相关资源
        最近更新 更多