GithubHelp home page GithubHelp logo

fritids / ajax-typeahead Goto Github PK

View Code? Open in Web Editor NEW

This project forked from pwarelis/ajax-typeahead

0.0 1.0 0.0 222 KB

Modifications to the Bootstrap Typeahead plugin to add ajax capabilities

ajax-typeahead's Introduction

Ajax Typeahead

Modifications to the Bootstrap Typeahead plugin to give it ajax capabilities

How to Use

To make a regular typeahead plugin query a server for the source, just specify an ajax member when initializing.

Simple use

For a quick setup, specify a source url to pull data from.

$("#ajax-typeahead").typeahead({
	ajax: "/path/to/source"
});

Options

There are a few options to make this a bit more flexible. To use these, make the ajax option into an object containing any of these options:

  • ajax.timeout Specify the amount of time to wait for keyboard input to stop until you send the query to the server. Default is at 300ms.

  • ajax.displayField If the data returned from the server is a list of objects (as opposed to an array of strings), set this member to the name for the field to use for display.

  • ajax.triggerLength This is the minimum length of text to take action on. Default is at 3.

  • ajax.method The method to use, either "post" or "get". Defaults to "post".

  • ajax.loadingClass If a call is in progress, this class will be added to the typeahead element. It is removed if the call is done.

  • ajax.preDispatch This function will be run prior to any call. It is used to fashion a custom parameter object to send to the server. Its only argument is the query text. It must return an object that jQuery's post() function will use as its second argument. There's no default for this. If not specified, the parameters send to the server are:

{ query: "some text" }
  • ajax.preProcess This function will be run right after a call and before the typeahead list is populated. It is used to pre process the data returned from the server. Its only argument is the data from the server. Returning false from this method will hide the typeahead list. If not specified, the data will be passed to the typeahead mechanism as is.

Example

$("#ajax-typeahead").typeahead({
	ajax: {
		url: "/path/to/source",
		timeout: 500,
		displayField: "name",
		triggerLength: 1,
		method: "get",
		loadingClass: "loading-circle",
		preDispatch: function (query) {
			showLoadingMask(true);
			return {
				search: query,
				otherParam: 123
			}
		},
		preProcess: function (data) {
			showLoadingMask(false);
			if (data.success === false) {
				// Hide the list, there was some error
				return false;
			}
			// We good!
			return data.mylist;
		}
	}
});

Enjoy!

Contact

pwarelis at gmail dot com

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.