duanke1286 2010-01-23 12:30
浏览 50
已采纳

使用jquery和codeigniter php MVC自动完成

I'm trying to build an autocomplete using jquery plugin autocomplete from this site.

Now I managed to achieve autocomplete using local results pre-loaded into website as a part of document ready function, its quite easy doing it localy.

Here is with what I struggle with, pulling results from php file. Here is how I tried:

$("#post_tags").autocomplete("http://localhost/tags/filter", {
    width: 260,
    selectFirst: false,
    highlight: false,
    multiple: true,
    multipleSeparator: " ",
    scroll: true,
    formatItem: function (row, position, totalReturned, searchTerm){
      return row[0];
    },
    formatResult: function(row, position, totalReturned){
      return row[0].replace(/(<.+?>)/gi, '');
    }
 }).result(function(event, data, formatted){
    $("<li>").html( !data ? "No match!" : "Selected: " + formatted)
     .appendTo("#result");
 });

When I manually go to http://localhost/tags/filter/p I get results php,asp because they contain letter p .

How can I make this work with autocomplete, I mean its quite easy using native php see this file , meaning I'm only passing something to the search file like this search?q=p and I get back the results. Since I'm using codeigniter the things are a bit different and I don't retrieve any results using jquery code above.

I'm stuck with this since last night I cannot yet offer a bounty and I really need it for monday. Thank you

  • 写回答

1条回答 默认 最新

  • dousa2794 2010-01-23 13:06
    关注

    In your jquery.autocomplete.js file, at around line 360 (as of the latest version), add one line to make the section look like this:

    $.ajax({ 
        type: "post", // This is the new line
        // try to leverage ajaxQueue plugin to abort previous requests
        mode: "abort",
        // limit abortion to this input
        port: "autocomplete" + input.name,
    

    (If you are using a minified version of the autocomplete plugin, just do a search for .ajax and insert the line as appropriate).

    This change causes the autocomplete script will now send form data by POST, rather then GET. Accessing this from within codeigniter can be done as follows:

    public function filter()
    {
        $filter_by = $this->input->post('q');
    }
    

    And from this you can generate your list as required. I'm not sure if there is a nicer way to do this (one which doesn't require you to edit the source code), but it works for me.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题