简体   繁体   English

使用带有非标准选项的jQuery自动完成功能?

[英]Using jQuery autocomplete with non-standard options?

I'd like to use jQuery's auto complete in a Drupal project to automatically find nodes (pieces of content) with a given title. 我想在Drupal项目中使用jQuery的自动完成功能来自动查找具有给定标题的节点(内容)。 I can't quite find any examples of option usage that match what I am trying to do, which is the following: 我找不到与我尝试执行的选项用法匹配的任何示例,如下所示:

  1. URL needs to conform to the pattern: /api/node/title/{whatever the user typed} 网址必须符合以下格式:/ api / node / title / {无论用户输入了什么}
  2. When results are returned as JSON, the title needs to be used in the auto complete list 当结果以JSON格式返回时,标题需要在自动完成列表中使用
  3. When a result is clicked, a styled paragraph containing the title will appear over the text box, but it will actually contain the node id (nid) of the node selected. 单击结果后,包含标题的样式化段落将出现在文本框上方,但实际上将包含所选节点的节点ID(nid)。

Here is what I have so far: 这是我到目前为止的内容:

jQuery(this).autocomplete({
    source: '/api/node/title/'+jQuery(this).val(),
    minLength: 2
}).data( "autocomplete")._renderItem = function(ul, item) {
    return jQuery('<li />')
        .data("item.autocomplete", item)
        .appendTo(ul);
};

I haven't even gotten to worrying about what to do once an element is selected - the URL is going out as /api/node/title?term={blank}, and even though I am getting JSON results back, nothing is appearing. 选择元素后,我什至不必担心该怎么办-URL以/ api / node / title?term = {blank}的形式出现,即使我得到了JSON结果,也没有任何反应。 Any suggestions, or examples of similar usage? 有什么建议或类似用法的例子吗? The examples on the jQuery UI website for autocomplete weren't especially helpful. jQuery UI网站上用于自动完成的示例并不是特别有用。

EDIT: Here is an example of the expected response. 编辑:这是预期响应的示例。

{
    "nid":"2",
    "vid":"2",
    "type":"lorem",
    "language":"und",
    "title":"Grid Computing",
    "uid":"0",
    "status":"1",
    "created":"1320092886",
    "changed":"1320273538",
    "comment":"1",
    "promote":"1",
    "sticky":"0",
    "tnid":"0",
    "translate":"0"
}

for 1), you can use a callback for the source 对于1),您可以对源使用回调

$('input').autocomplete({
    source: autoDrupal,
    minLength: 2});

function autoDrupal(requestObject, responseCallback) {
   var url = '/api/node/title/' + requestObject.term;
   $.get(url, function(data) {
       // check my fiddle to transform drupal response in autocomplete result ;)
       responseCallback(data);
   });
};

for 2) I don't get what you mean by "title" but it can surely be handled in the response of $.get for 3) a event handler can do the trick (once I understand your dreaded "title"). 对于2)我没有理解“标题”的含义,但是肯定可以在$ .get的响应中处理3)事件处理程序可以解决问题(一旦我理解了您恐惧的“标题”)。 something like that: 像这样的东西:

$('ul.ui-autocomplete').delegate('li', 'click', function () {
    $('#stuff').css('color', 'red');
})

check and play with this fiddle ;) 检查并玩这个小提琴;)

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

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