bootstrap typeahead return name and id

前端 未结 6 1463
予麋鹿
予麋鹿 2021-02-03 13:33

Hy! I\'m using twitter bootstraps typeahead:

I\'m calling a page that returns a response with json_encode the page returns a name and an ID,

I want that the type

6条回答
  •  不知归路
    2021-02-03 14:13

    I had the same issue when i tried to process username and id.I had a tricky fix but later on i have fixed it with proper solution.

    Have a look at this one,

    $('#search').typeahead({
     source: function(query, process) {
     var $url = "/controller/function/list_users/?q="+query;
     var $datas = new Array;
     $datas = [""];
     $.ajax({
     url: $url,
     dataType: "json",
     type: "GET",
     success: function(data) {
     $.map(data, function(data){
     var group;
     group = {
     id: data.id,
     name: data.user_name,
     toString: function () {
     return JSON.stringify(this);
     //return this.variable;
     },
     toLowerCase: function () {
     return this.name.toLowerCase();
     },
     indexOf: function (string) {
     return String.prototype.indexOf.apply(this.name, arguments);
     },
     replace: function (string) {
     var value = '';
     value += this.name;
     if(typeof(this.name) != 'undefined') {
     value += ' ';
     //value += this.name;
     value += '';
     }
     return String.prototype.replace.apply('
    ' + value + '
    ', arguments); } }; $datas.push(group); }); process($datas); } }); }, property: 'user_name', items: 10, minLength: 2, updater: function (item) { var item = JSON.parse(item); $('#hiddenId').val(item.id); $('#username').val(item.name); //you can perform your actions here //example

    //location = '/controller/function/'+item.id+'/edit'; //document.redirect = location; } });

    look at this link also.

    http://vaisakhvm.wordpress.com/2013/07/31/twitter-bootstrap-typeahead-process-multiple-values/

提交回复
热议问题