Custom search rules for Jquery.autocomplete.js plugins

Source: Internet
Author: User

This two-day start with Jquery.autocomplete this AutoComplete plugin. Basic function is relatively strong, but in the actual needs of the discovery or there is a shortage!
The problem is this: when I define a local data JS file, the format is a JSON-like array. As follows:

varStockinfojson =[{name:"Deep Development A", Code:"000001", Spell:"Sfza"}, {name:"Vanke a", Code:"000002", Spell:"Wka"}, {name:"ST Country Agriculture", Code:"000004", Spell:"STGN"}, {name:"Century star Source", Code:"000005", Spell:"Sjxy"}, {name:"Deep Vibration Industry a", Code:"000006", Spell:"Szya"}, {name:"ST da Sound", Code:"000007", Spell:"STDs"}, {name:"St Po Lee to", Code:"000008", Spell:"STBLL"}, {name:"China Bao ' an", Code:"000009", Spell:"Zgba"}, {name:"S St Huaxin", Code:"000010", Spell:"Ssthx"}, {name:"Mountain Airways B", Code:"200152", Spell:"SHB"}, {name:"*st b", Code:"200160", Spell:"STDXB"}, {name:"Rey B", Code:"200168", Spell:"Lyb"}, {name:"Gem B", Code:"200413", Spell:"BSB"}, {name:"Little Swan B", Code:"200418", Spell:"Xteb"}, {name:"Yue High speed b", Code:"200429", Spell:"AGSB"}, {name:"Ning Communication B", Code:"200468", Spell:"LTXB"}, {name:"Morning Bell b", Code:"200488", Spell:"CMB"}, {name:"Pearl River B", Code:"200505", Spell:"ZJB"}, {name:"min-kun b", Code:"200512", Spell:"MSKB"}, {name:"Huadian International (China )", Code:"600027", Spell:"Hdgj"}];

Now you want the user input to match in the Code and spell properties. But the default Jquery.autocomplete.js plugin always matches the name when the user enters the content.
It is inconvenient to see that the source code does not find any related attributes that support this requirement! So it was decided to modify the jquery.autocomplete.js so that it supported the custom matching pattern.
First add an event to line 429

Reasultsearch:null,// add by Fengyan event property that adds a custom query result

In $. Add a cache data variable 450 row in Autocompleter.cache

var New Array (); // add by Fengyan array variable caches all data

Add 511 rows in the populate () method

Alldata.push (row); // Add by Fengyan store each row of data in the array variable that you just defined

Then add a judgment in the Load:function (q) method to 568 rows

//Add by Fengyan Call User Custom Query methodElse if(typeof(options.reasultsearch) = ="function"){    varCsub = []; $.each (alldata, function (i, row) {varms =Options.reasultsearch (ROW,Q); if(ms!=NULL&& ms!=false) {Csub.push (MS);    }    }); returncsub; }

Then the foreground call can be extended using the Resultsearch property to expand the way we want to query

$ (function () {$ ("#suggest1"). AutoComplete (Stockinfojson, {minchars:1, MatchCase:false,// Case insensitiveAutoFill:false, Max:Ten, formatitem:function (row, I, max,term) {varV = $ ("#suggest1"). Val (); returnRow.name +" ("+ Row.code +")"; if(Row.code.indexOf (v) = =0|| Row.spell.indexOf (v) = =0)            {                returnRow.name +" ("+ Row.code +")"; }            Else                return false; }, Formatmatch:function (row, I, max) {returnRow.name +" ("+ row.code+")"; }, Formatresult:function (row) {returnRow.code; }, Reasultsearch:function (row,v)//This field data custom query syntax Note This is my own new event.        {            //custom match in code or spell            if(Row.data.code.indexOf (v) = =0|| Row.data.spell.indexOf (v) = =0)            {                returnRow; }            Else                return false; }    });});

By modifying the source files to meet the needs of the purpose!


Run effect

Extension methods This article has been described in detail
If there is a need for the source of friends can access
SOURCE download: download code download local

Custom search rules for Jquery.autocomplete.js plugins

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.