开发者

how to edit this code in ajax autocomplete for jquery

hi i am using ajax autocomplete for jquery plugin

http://www.devbridge.com/projects/autocomplete/jquery/

there is a code

  function Autocomplete(el, options) {
    this.el = $(el);
    this.id = options.id;
    this.el.attr('autocomplete', 'off');
    this.suggestions = [];
    this.data = [];
    this.badQueries = [];
    this.selectedIndex = -1;
    this.currentValue = this.el.val();
    this.intervalId = 0;
    this.cachedResponse = [];
    this.onChangeInterval = null;
    this.ignoreValueChange = false;
    this.serviceUrl = options.serviceUrl;
    this.isLocal = false;

    //this.options.fnFormatResult = fnFormatResult();

    this.options = {
      autoSubmit: false,
      minChars: 1,
      maxHeight: 300,
      deferRequestBy: 0,
      width: 0,
      highlight: true,
      params: {}, 
      fnFormatResult: fnFormatResult,      
      delimiter: null,
      zIndex: 9999
    };
    this.initialize();
    this.setOptions(options);
  }

in there

this.options = {
  autoSubmit: false,
  minChars: 1,
  maxHeight: 300,
  deferRequestBy: 0,
  width: 0,
  highlight: true,
  params: {}, 
  fnFormatResult: fnFormatResult,      
  delimiter: null,
  zIndex: 9999
};

it has define a function to format th开发者_运维问答e result

fnFormatResult: fnFormatResult,   

i want to use a function other than fnFormatResult like fnFormatResult2 .

i want to change the calling function acording to a parameter pass to the plugin

i need to do like this

if(param == 1){
 fnFormatResult: fnFormatResult,   
}
else if(param == 1){
 fnFormatResult: fnFormatResult2,   
}

how can i do this . please help......................


Hmm, I guess you can do:

this.options = {
  autoSubmit: false,
  minChars: 1,
  maxHeight: 300,
  deferRequestBy: 0,
  width: 0,
  highlight: true,
  params: {}, 
  fnFormatResult: function(value, data, currentValue) {
    if(param==1) {
      return fnFormatResult.call(this, value, data, currentValue);
    } else if (param==2) {
      return fnFormatResult2.call(this, value, data, currentValue);
    }
  },      
  delimiter: null,
  zIndex: 9999
};


I think You can make some changes in function:

function fnFormatResult(value, data, currentValue) {
    var pattern = '(' + currentValue.replace(reEscape, '\\$1') + ')';
    return value.replace(new RegExp(pattern, 'gi'), '<strong>$1<\/strong>');
  }

You can Implement both defination inside this function.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜