使用jQuery Autocomplete w / PHP,远程数据源和类别

时间:2017-06-15 05:01:42

标签: php jquery jquery-ui jquery-ui-autocomplete

我在实施jQuery's autocomplete的类别功能时遇到了问题。

这是我的剧本:

1.i want to change button name but when i am done changes in blockuser.tpl and nothing is change i am removing cache also but no output.

这是我的autocompleteAccount.php代码:

  $( function() {
    $.widget( "custom.catcomplete", $.ui.autocomplete, {
      _create: function() {
        this._super();
        this.widget().menu( "option", "items", "> :not(.ui-autocomplete-category)" );
      },
      _renderMenu: function( ul, items ) {
        var that = this,
          currentCategory = "";
        $.each( items, function( index, item ) {
          var li;
          if ( item.category != currentCategory ) {
            ul.append( "<li class='ui-autocomplete-category'>" + item.category + "</li>" );
            currentCategory = item.category;
          }
          li = that._renderItemData( ul, item );
          if ( item.category ) {
            li.attr( "aria-label", item.category + " : " + item.label );
          }
        });
      }
    }); 

    $( ".accountIdInput" ).catcomplete({
      delay: 0,
      source: 'autocompleteAccount.php'
    });
  } );

形式:

    $searchTerm = $_GET['term'];

$sql        = " SELECT groupName,groupId 
                    FROM groups  
                    WHERE groupName 
                    LIKE '%".$searchTerm."%' 
                    AND public = '1' 
                    ORDER BY groupName ASC";

    $result     = mysqli_query($conn,$sql) or die(mysqli_error($conn));             

    while($row = mysqli_fetch_assoc($result)) {

        $string = stripslashes($row['groupName']);
        $data[] = array("label" => $string,"category" => "Group");

    }           

    $newData = array_values($data);

    //return json data
    if(isset($newData)) {
        echo json_encode($newData);
    }

修改 以及直接运行php时的示例JSON输出:

<input type="text" id="accountQuery" class="form-control accountIdInput idInput formElement ui-autocomplete-input">

没有弹出结果,但我不确定是否因为JSON在返回时如何被解析(或者没有被解析)。我已经阅读了有关此问题的其他主题,但无法找到有效的解决方案。

有什么想法吗?

0 个答案:

没有答案