简体   繁体   English

jquery ui autocomplete:导航json对象

[英]jquery ui autocomplete: navigating json objects

The excellent answer by Paz got me through a rough patch this week with the autocomplete jQuery UI deal. Paz的优秀答案让我在本周通过自动完成的jQuery UI交易进行了粗略的修补。

All that to say, How does one choose the correct json response if the you have two different "base" objects in your json response. 所有这一切,如果你在json响应中有两个不同的“基础”对象,如何选择正确的json响应。

Simply put, I have a ruby instance variable made up of two models, Pitch and User. 简单地说,我有一个ruby实例变量,由两个模型组成,Pitch和User。 I want to autocomplete based on this instance variable. 我想基于此实例变量自动完成。 The json response is, say 比如,json的反应是

[{"user":{"id":1,"name":"Blah Hugo"}},
{"user":{"id":3,"name":"Blaz Foobar"}},
{"pitch":{"id":2,"name":"Blasters On Business Apparel"}}]

The autocomplete javascript file looks like this NOW, and it works < snippet only >: 自动完成javascript文件现在看起来像这样,它只能<snippet>:

.data( "autocomplete" )._renderItem = function( ul, item ) {
  return jQuery( "<li></li>" )
  .data( "item.autocomplete", item )
  .append( "<a>" + (item.user ? item.user.name : item.pitch.name) + "</a>" )
  .appendTo( ul );
};

Again my problem is this ternary i had to use. 我的问题再一次是我必须使用的这个三元组。 item[0].name doesn't work, nor did item.item[0].name Is there no way to get the generic 'user' or 'pitch' if you don't know the name of either? item[0].name不起作用,也没有item.item[0].name如果您不知道任何一个名称,是否无法获得通用的“用户”或“推销”?

I'd have though one of them would. 我会有其中一个会的。 What says you? 什么说你?

I don't think there's much wrong with what you have, but maybe a slight improvement would be: 我不认为你的东西有多大的错误,但可能会略有改善:

.data("autocomplete")._renderItem = function(ul, item) {
    item = item.user || item.pitch;

    return jQuery("<li></li>")
        .data("item.autocomplete", item)
        .append("<a>" + item.name + "</a>")
        .appendTo(ul);
};

You could try using the || 您可以尝试使用|| (logical OR) to sort of short hand your ternary operator you have in the .append() method. (逻辑或)用于在.append()方法中对您的三元运算符进行简写。

Does this do what you want it to? 这样做你想要的吗?

.data( "autocomplete" )._renderItem = function( ul, item ) {
    item = item.user || item.pitch || {name:""};
    .append( "<a>" + item.name + "</a>" )
    return jQuery( "<li></li>" )
    .data( "item.autocomplete", item )
    .append( "<a>" + item.name  + "</a>" )
    .appendTo( ul );
};

This will return the first "non-false" value in the list of arguments; 这将返回参数列表中的第一个“非假”值; in this case item.user.name and item.pitch.name . 在这种情况下, item.user.nameitem.pitch.name In this case, if your JSON object has a user property, the name value will be returned. 在这种情况下,如果您的JSON对象具有user属性,则将返回name值。 If the JSON object has a pitch property, the name value will be returned. 如果JSON对象具有pitch属性,则将返回name值。 If both of those operations return undefined , a blank string will be returned and appended into the a tag. 如果这两个操作都返回undefined ,则返回一个空字符串并将其附加到a标记中。

This answer gives more information about use and special cases to keep in the back of your mind when using using the 'OR' method. 这个答案提供了有关使用和特殊情况的更多信息,以便在使用“OR”方法时保持在脑海中。

UPDATE : Fixed null exception possibility. 更新 :修复了null异常的可能性。

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

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