2012-08-29 7 views
21

के बजाय ऑब्जेक्ट लेने के लिए बूटस्ट्रैप-टाइपहेड बढ़ाएं, मैंने स्ट्रिंग के बजाय ऑब्जेक्ट लेने के लिए बूटस्ट्रैप-टाइपहेड बढ़ाया है।
यह काम करता है लेकिन मैं जानना चाहता हूं कि यह चीजों को करने का सही तरीका है।स्ट्रिंग

धन्यवाद।

संदर्भ:
http://twitter.github.com/bootstrap/javascript.html#typeahead http://twitter.github.com/bootstrap/assets/js/bootstrap-typeahead.js

_.extend($.fn.typeahead.Constructor.prototype, { 
    render: function (items) { 
     var that = this; 

     items = $(items).map(function (i, item) { 
      i = $(that.options.item) 
       .attr('data-value', item[that.options.display]) 
       .attr('data-id', item.id); 
      i.find('a').html(that.highlighter(item)); 
      return i[0]; 
     }); 

     items.first().addClass('active'); 
     this.$menu.html(items); 
     return this; 
    }, 
    select: function() { 
     var val = this.$menu.find('.active').attr('data-value'), 
      id = this.$menu.find('.active').attr('data-id'); 
     this.$element 
      .val(this.updater(val, id)) 
      .change(); 
     return this.hide() 
    } 
}); 

return function (element, options) { 
    var getSource = function() { 
      return { 
       id: 2, 
       full_name: 'first_name last_name' 
      }; 
    }; 

    element.typeahead({ 
     minLength: 3, 
     source: getSource, 
     display: 'full_name', 
     sorter: function (items) { 

      var beginswith = [], 
       caseSensitive = [], 
       caseInsensitive = [], 
       item, 
       itemDisplayed; 

      while (item = items.shift()) { 
       itemDisplayed = item[this.options.display]; 
       if (!itemDisplayed.toLowerCase().indexOf(this.query.toLowerCase())) { 
        beginswith.push(item); 
       } else if (~itemDisplayed.indexOf(this.query)) { 
        caseSensitive.push(item); 
       } else { 
        caseInsensitive.push(item); 
       } 
      } 

      return beginswith.concat(caseSensitive, caseInsensitive); 
     }, 
     highlighter: function (item) { 
      var query = this.query.replace(/[\-\[\]{}()*+?.,\\\^$|#\s]/g, '\\$&'); 

      return item[this.options.display].replace(new RegExp('(' + query + ')', 'ig'), function ($1, match) { 
       return '<strong>' + match + '</strong>'; 
      }); 
     }, 
     matcher: function (item) { 
      var value = item[this.options.display]; 

      return { 
       value: ~value.toLowerCase().indexOf(this.query.toLowerCase()), 
       id: item.id 
      }; 
     }, 
     updater: function (item, userId) { 
      options.hiddenInputElement.val(userId); 
      return item; 
     } 
    }); 
}; 
+1

निम्नलिखित काम करता है मैं नहीं दिख रहा है कि कैसे आप भी है कि अब तक हो रही है। जब मैं आपके कोड को चलाने का प्रयास करता हूं तो वस्तुओं पर स्ट्रिंग विधियों को लागू करने का प्रयास करने के लिए 'सॉर्टर()' विधि में अपवाद फेंक दिया जाता है। क्या आप उस विधि को बदल रहे थे? या शायद वस्तुओं में कार्यों को जोड़ना? – merv

+1

हाँ, आप सही हैं क्योंकि मैंने अन्य बदलाव किए हैं जिन्हें मैंने अभी तक पोस्ट नहीं किया है .. मैं इसे बाद में बनाउंगा। धन्यवाद –

+1

हाँ, यह मददगार होगा, क्योंकि 'matcher()' और 'sorter()' दोनों 'रेंडर()' विधि को पास करने से पहले 'आइटम' को बदल दें। साथ ही, एपीआई में 'sorter() 'को ओवरराइड करना समर्थित है, इसलिए ** bootstrap-typeahead.js ** स्रोत में इसे संपादित करने की आवश्यकता नहीं होनी चाहिए। (यह नहीं कह रहा कि आप क्या कर रहे थे - बस इसे इंगित करते हुए) – merv

उत्तर

18

मैं प्रोटोटाइप (जो जहां Autocompleter, न केवल निम्नलिखित समारोह इस्तेमाल कर सकते हैं सभी स्थानों को प्रभावित करेंगे) पुनर्लेखन नहीं करने के लिए सलाह देते हैं।
इस कोड को काम करना चाहिए:

var getSource = function() { 

    var users = new Backbone.Collection([ 
     { 
      first_name: 'primo', 
      last_name: 'ultimo', 
      id: 1 
     }, 
     { 
      first_name: 'altro_primo', 
      last_name: 'altro_ultimo', 
      id: 2 
     } 
    ]).models; 

    return _.map(users, function (user) { 
     return { 
      id: user.get('id'), 
      full_name: user.get('first_name') + ' ' + user.get('last_name'), 
      // these functions allows Bootstrap typehead to use this item in places where it was expecting a string 
      toString: function() { 
       return JSON.stringify(this); 
      }, 
      toLowerCase: function() { 
       return this.full_name.toLowerCase(); 
      }, 
      indexOf: function (string) { 
       return String.prototype.indexOf.apply(this.full_name, arguments); 
      }, 
      replace: function (string) { 
       return String.prototype.replace.apply(this.full_name, arguments); 
      } 
     }; 
    }); 
}; 

$('.full_name').typeahead({ 
      minLength: 3, 
      source: getSource(), 
      display: 'full_name', 
      updater: function (itemString) { 
       var item = JSON.parse(itemString); 
       $('.id').val(item.id); 
       return item.full_name; 
      } 
}); 

डेमो:

$('.full_name').typeahead({ 
    /* ... */ 
    source: function(query,process){ 
    $.ajax({ 
     url: 'your_url_here', 
     data: {term: query, additional_data: 'some_data'}, 
     success: function(data){ 
      process($.map(data, function(user){ 
       return { 
        id: user.id, 
        full_name: user.first_name + ' ' + user.last_name, 
        toString: function() { 
         return JSON.stringify(this); 
        }, 
        toLowerCase: function() { 
         return this.full_name.toLowerCase(); 
        }, 
        indexOf: function (string) { 
         return String.prototype.indexOf.apply(this.full_name, arguments); 
        }, 
        replace: function (string) { 
         return String.prototype.replace.apply(this.full_name, arguments); 
        } 
       } 
      })); 
     } 
    }); 
    }, 
/* ... */ 
}); 

Btw: http://jsfiddle.net/hyxMh/48/

+0

हाय, मैं आपके समाधान का उपयोग करने की कोशिश कर रहा हूं, और चूंकि मैं उपयोगकर्ता डेटा प्राप्त करने के लिए AJAX कॉल का उपयोग कर रहा हूं, इसलिए मुझे इसे आपके कोड से एकीकृत करना मुश्किल लगता है।अगर मैं बाहर घोषित गेटसोर्स में AJAX लिखता हूं, तो मुझे कोई रास्ता नहीं मिल रहा है कि मैं अपनी क्वेरी को कैसे पास करूंगा। क्या आपके पास इसके लिए एक काम है? धन्यवाद :) –

0

@AntoJs के अलावा उत्तर देने के लिए, यहाँ एक ajax संस्करण (सिर्फ 'स्रोत' भाग) है: @ एंटोजेस - आपके उत्तर के लिए बहुत धन्यवाद!

0

@ antonjs का जवाब मेरे लिए काम नहीं कर रहा था और मैं निम्न त्रुटि

लेखन त्रुटि हो रही थी: item.substr एक समारोह

jQuery v2.1.3 और Bootstrap3-typeahead.js v3 के साथ इतना

नहीं है .1.0 मुझे

var getSource = function() { 
 

 
    var users = new Backbone.Collection([ 
 
     { 
 
      first_name: 'primo', 
 
      last_name: 'ultimo', 
 
      id: 1 
 
     }, 
 
     { 
 
      first_name: 'altro_primo', 
 
      last_name: 'altro_ultimo', 
 
      id: 2 
 
     } 
 
    ]).models; 
 

 
    return _.map(users, function (user) { 
 
     return { 
 
      id: user.get('id'), 
 
      full_name: user.get('first_name') + ' ' + user.get('last_name'), 
 
      // these functions allows Bootstrap typehead to use this item in places where it was expecting a string 
 
      toString: function() { 
 
       return JSON.stringify(this); 
 
      }, 
 
      toLowerCase: function() { 
 
       return this.full_name.toLowerCase(); 
 
      }, 
 
      indexOf: function (string) { 
 
       return String.prototype.indexOf.apply(this.full_name, arguments); 
 
      }, 
 
      replace: function (string) { 
 
       return String.prototype.replace.apply(this.full_name, arguments); 
 
      }, 
 
      substr: function(start, len){ 
 
        return this.full_name.substr(start, len); 
 
      } 
 
     }; 
 
    }); 
 
}; 
 

 
$('.full_name').typeahead({ 
 
      minLength: 3, 
 
      source: getSource(), 
 
      display: 'full_name', 
 
      updater: function (itemString) { 
 
       var item = JSON.parse(itemString); 
 
       $('.id').val(item.id); 
 
       return item.full_name; 
 
      } 
 
});