Git Product home page Git Product logo

typeahead's Introduction

typeahead ajax

Connects to a server and populates a box with matches.

Download: [Release] (https://github.com/manngo/typeahead/releases/download/0.1/typeahead.js "Initial Release")

Requirements

  1. A form with a textbox for input. This will used for typeahead, so it needs to be identifiable. In the sample below, the form has an id, and the textbox has a name.
  2. A server prepared to respond to an GET request, returning some sort of data in JSON format. It should represent an array of objects.

Usage

typeAhead(element,url,template,callback);

Code

HTML

<form id="test">
	<input name="data">
</form>

JS

var element='form#test input[name="data"]';
var url='http://…';
var template='[item] [item]';
function callback(result) {
	//	process result
}
var options={
	"suggestions":"suggestions",
};

typeAhead(element,url,template,callback,options);

Parameters

Option Meanning
element Selector of input element to become typehead element
url URL for ajax data
template String with pattern to display inside suggestions enclose database fields in square brackets
callback Callback function to process results. The result object is an array of objects matching the request.
options An optional object – currently only the selector is available.

Samples

element

form#data input[name=town]

url

test

template

[town] [state] [postcode]

callback

function(result) {

}

The callback function takes one parameter, which is an array with the results.

options

{
"suggestions": "suggestions",
};

The only option available is the selector for the selection box. This allows you to make changes to the appearance in CSS. The value takes the following format:

Format Meaning CSS Selector
@someting attribute div[something]
#something id div#something
.something class div.something
something attribute div[something]

Sample

This release has a sample folder. To view the sample live, see http://github.manngo.net/typeahead/

The Sample file says it all, but here are the important parts:

<script type="text/javascript" src="typeahead.js"></script>
<script type="text/javascript">
	window.onload=init;
	function init() {
		var form=document.querySelector('form#sample');
		typeAhead('form#sample input[name="town"]','http://widgets.comparity.net/postcodes.php?limit=16&town=','[locality] [state] [postcode]',function(result) {
			form['location'].value=result.locality;
			form['state'].value=result.state;
			form['postcode'].value=result.postcode;
		});
	}
	</script>

To Do:

  1. Consolidate input & element variables

typeahead's People

Contributors

manngo avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.