php - Include a search box that can look through a JSON and return drop down list -
i'm using json file populate drop down menus various conversions. converter has grown , i'd have option of including search bar.
so user can search milliampere instead of navigating 'current' category within "electricity" etc etc. i'd make bit easier them.
my json has 2 fields, name , value of drop down, example below. value string in fashion understand milliampere milliampere , various others using camel case. don't think suitable run search on field values may differ name.
the name field in instance milliampere(ma) need search able @ part of string , not full match ignoring case liklihood search string milliamp or milliampere or milliamperes.
from there next step either populate drop downs whilst on page or return list of possible options if there many.
is possible @ , if can please guide me in right direction?
many thanks!
"current":[ { "value" : "ampere", "name" : "ampere(a)" }, { "value" : "kiloampere", "name" : "kiloampere(ka)" }]
you can use js lib - defiantjs (defiantjs.com), extends global object json new method: "search". method, can search json structure xpath expressions, this:
var data = { "current": [ { "value": "ampere", "name": "ampere(a)" }, { "value": "kiloampere", "name": "kiloampere(ka)" }, { "value": "milliampere", "name": "milliampere" }, { "value": "milliampere", "name": "milliampere" } ] }, res = json.search( data, '//name[contains(translate(., "ma", "ma"), "millia")]/..' ); console.log( res[0].name ); console.log( res[1].name );
the "translate" method in expression extends search include uppercase , lowercase versions of letters "ma". if want cover alphabetic letter, add of them.
here working fiddle;
http://jsfiddle.net/hbi99/6sss9/
Comments
Post a Comment