简体   繁体   English

更改 Twitter Typeahead 返回的内容

[英]Change what Twitter Typeahead returns

Is it possible to change what Typeahead from Twitter returns?是否可以更改从 Twitter 返回的 Typeahead?

So that users can search the name in the textfield, but it returns the ID that corresponds to the selected result?这样用户就可以在文本字段中搜索名称,但返回与所选结果对应的ID?

I haven't been able to find anything about this, I even searched the source code.我一直没能找到任何关于这个的东西,我什至搜索了源代码。

Thanks in advance提前致谢

$(document).ready(function(){
    var modules = getBloodhound('modules');

    $('.typeahead').typeahead({
        hint: true,
        highlight: true,
        minLength: 1
    },
    {
        name: 'id',
        displayKey: 'name',
        source: modules
    });
});

var getBloodhound = function(name){
    return new Bloodhound({
        datumTokenizer: Bloodhound.tokenizers.whitespace,
        queryTokenizer: Bloodhound.tokenizers.whitespace,
        remote: {
            url: '/json/' + name + '/%QUERY',
            wildcard: '%QUERY'
        }
    });
}

Edit, Updated编辑、更新

If interpret Question correctly, the returned array contains objects which have name and id properties.如果正确解释 Question,则返回的数组包含具有nameid属性的对象。 Requirement is to set the hint displayed at input element to id of returned object, not name , which would be rendered in suggestion results?要求是将input元素上显示的hint设置为返回对象的id ,而不是name ,这将在建议结果中呈现?

You can use templates , suggestion function to set .tt-hint input , which displays hint to id of value;您可以使用templatessuggestion功能来设置.tt-hint input ,它显示对值idhint set .tt-hint placeholder attribute, which displays hint to id of object argument at suggestion option..tt-hint placeholder属性,它显示hintid对象参数中的suggestion的选项。

Use typeahead:render , input events to set css left property of .tt-hint , or placeholder to empty string if no value at .typeahead使用typeahead:renderinput事件来设置.tt-hint css left属性,或者如果.typeahead没有value ,则placeholder为空字符串

var getBloodhound = function(name){
      return new Bloodhound({
        datumTokenizer: Bloodhound.tokenizers.whitespace("value"),
        queryTokenizer: Bloodhound.tokenizers.whitespace,
        remote: {
            url: '/json/' + name + '/%QUERY',
            wildcard: '%QUERY'
        }
      });
}

var modules = getBloodhound('modules').ttAdapter();

$(".typeahead").typeahead({
    hint: true,
    minLength: 1,
    highlight: true
  }, {
    name: "id",
    display: "value",
    source: modules,
    templates: {
      suggestion: function(data) {
        return "<li>" + data.team + "</li>"
      }
    }
  })
  .on("typeahead:render", function(e, data) {
    console.log(e, data);
    $(".tt-hint").attr("placeholder", data.id)
    .css("left", e.target.value.length * 10)
  })
  .on("input", function() {
    if (this.value === "" || /^\s+$/.test(this.value)) {
      $(".tt-hint").attr("placeholder", "")
    }
  })

 var nflTeams = new Bloodhound({ datumTokenizer: Bloodhound.tokenizers.obj.whitespace("team"), queryTokenizer: Bloodhound.tokenizers.whitespace, local: [{ "team": "San Francisco 49ers", "id": "49ers" }, { "team": "Chicago Bears", "id": "Bears" }, { "team": "Cincinnati Bengals", "id": "Bengals" }, { "team": "Buffalo Bills", "id": "Bills" }, { "team": "Denver Broncos", "id": "Broncos" }, { "team": "Cleveland Browns", "id": "Browns" }, { "team": "Tampa Bay Buccaneers", "id": "Buccaneers" }, { "team": "Arizona Cardinals", "id": "Cardinals" }, { "team": "San Diego Chargers", "id": "Chargers" }, { "team": "Kansas City Chiefs", "id": "Chiefs" }, { "team": "Indianapolis Colts", "id": "Colts" }, { "team": "Dallas Cowboys", "id": "Cowboys" }, { "team": "Miami Dolphins", "id": "Dolphins" }, { "team": "Philadelphia Eagles", "id": "Eagles" }, { "team": "Atlanta Falcons", "id": "Falcons" }, { "team": "New York Giants", "id": "Giants" }, { "team": "Jacksonville Jaguars", "id": "Jaguars" }, { "team": "New York Jets", "id": "Jets" }, { "team": "Detroit Lions", "id": "Lions" }, { "team": "Green Bay Packers", "id": "Packers" }, { "team": "Carolina Panthers", "id": "Panthers" }, { "team": "New England Patriots", "id": "Patriots" }, { "team": "Oakland Raiders", "id": "Raiders" }, { "team": "St.Louis Rams", "id": "Rams" }, { "team": "Baltimore Ravens", "id": "Ravens" }, { "team": "Washington Redskins", "id": "Redskins" }, { "team": "New Orlean Saints", "id": "Saints" }, { "team": "Seattle Seahawks", "id": "Seahawks" }, { "team": "Pittsburgh Steelers", "id": "Steelers" }, { "team": "Houston Texans", "id": "Texans" }, { "team": "Tennesse Titans", "id": "Titans" }, { "team": "Minnesota Vikings", "id": "Vikings" }] }) var adapter = nflTeams.ttAdapter(); $("#default-suggestions .typeahead").typeahead({ hint: true, minLength: 1, highlight: true }, { name: "nfl-teams", display: "value", source: adapter, templates: { suggestion: function(data) { return "<li>" + data.team + "</li>" } } }) .on("typeahead:render", function(e, data) { console.log(e, data); $(".tt-hint").attr("placeholder", data.id) .css("left", "calc(" + e.target.value.length * 7 + "px)") }) .on("input", function() { if (this.value === "" || /^\\s+$/.test(this.value)) { $(".tt-hint").attr("placeholder", "") } })
 <script src="http://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script> <script src="http://twitter.github.io/typeahead.js/releases/latest/typeahead.bundle.js"></script> <div id="default-suggestions"> <input class="typeahead" type="text" placeholder="NFL Teams"> </div>

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

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