Download the PHP package lifo/typeahead-bundle without Composer
On this page you can find all versions of the php package lifo/typeahead-bundle. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download lifo/typeahead-bundle
More information about lifo/typeahead-bundle
Files in lifo/typeahead-bundle
Package typeahead-bundle
Short Description A Symfony bundle that provides an autocomplete Typeahead form type compatible with Bootstrap v2|v3
License MIT
Informations about the package typeahead-bundle
Introduction
This is a Symfony v2.2+ Bundle that provides a Bootstrap (v2.x or v3.x) Typeahead widget for use in forms. The Typeahead component used in this bundle is the original Bootstrap v2.3 Typeahead library with a few enhancements.
*Update: If you were pointing to dev-master
in your composer.json and this bundle stopped working change your version to
"^1." and run composer update lifo/typeahead
to update your project.**
- If you are using Bootstrap v2.x then you must use version *[1.](https://github.com/lifo101/typeahead-bundle/tree/1.1)** of this bundle.
- If you are using Bootstrap v3.x then you must use version *[2.](https://github.com/lifo101/typeahead-bundle/tree/2.0)** of this bundle.
Note: This bundle does not use the newer Twitter/Typeahead.js javascript library.
Enhanced Typeahead Features
This bundle adds a few enhancements to the original bootstrap typeahead javascript library:
- Supports JSON objects
- Caches results
- Delays AJAX request to reduce server requests
- Properly handles pasting via mouse
- Includes an
AJAX Loader
icon - Supports Non-Entity lookup
- Supports non AJAX loading via a custom callback function
Screenshots
This example shows a form field that allows a single name to be entered.
This example shows a form field that allows multiple names to be entered. Clicking on a name link removes the entity. The entity in the backend is actually an ArrayCollection and automatically allows adding/removing entities from the list.
How to install
Note: This bundle requires jQuery and Bootstrap to be installed in your environment but does not include them directly. I suggest using the mopa/bootstrap-bundle which can help with this for you.
-
Add
lifo/typeahead-bundle
to the "requires" section of your project'scomposer.json
file, which can be done automatically by running the composer command from within your project directory:or manually by editing the composer.json file:
- Run
composer update
in your project root. -
Update your project
app/AppKernel.php
file and add this bundle to the $bundles array: -
Add
@lifo_typeahead_js
to your Asseticjavascripts
block. Similar to the block below. Your actual setup may differ. Be sure to include it AFTER your jquery and bootstrap libraries. -
Add
@lifo_typeahead_css
to your Asseticstylesheets
block. Similar to the block below. Your actual setup may differ. - If you're not using
Assetic
then you will have to manually include the javascript and css files into your project.
How to use
Using the typeahead control is extremely simple. The available options are outlined below:
Options
class
is your entity class. Ifnull
(or not specified), the items returned from your controller AJAX response do not have to be Entities. If not blank, the class is used to map the items to your DB Entities.source
is the name of a function to call that will collect items to display. This orroute
must be specified. The prototype is:function(query, process)
whereprocess
is the callback your function should call after you've fetched your list of matching items. It expects a FLAT array of strings to render into the pull down menu (Not {id:'...', value:'...'} objects!). See the example below for more information.route
is the name of the route to fetch entities from. The controller matching the route will receive the following parameters viaPOST
:query
The query string to filter results by.limit
The maximum number of results to return.render
The configuredrender
name. This is what you should use to set thevalue
attribute in the AJAX response.property
The configuredproperty
name. Normally this isid
. This is what you should use to set theid
attribute in the AJAX response.
route_params
Extra parameters to pass to theroute
.minLength
Minimum characters needed before firing AJAX request.items
Maximum items to display at once (default: 8)delay
Delay in milliseconds before firing AJAX (default: 250)spinner
Class string to use for loading spinner (default: "glyphicon glyphicon-refresh spin") Font-Awesome example: "fa fa-refresh fa-spin fa-fw"multiple
If true the widget will allow multiple entities to be selected. One at a time. This special mode creates an unordered list below the typeahead widget to display the selected entities.callback
Callback function (or string) that is called when an item is selected. Prototype:function(text, data)
wheretext
is the label of the selected item anddata
is the JSON object returned by the server.render
is the property of your entity to display in the typeahead input. This is used to render the initial value(s) into the widget. Once a user starts typing, the rendered responses are dependent on theroute
orsource
used.
AJAX Response
The controller should return a JSON
array in the following format. Note: id
and value
properties are required but
you may include other properties as well.
Note: If you are using a null class
option then your JavaScript array should return an id
and value
that are
the same thing. e.g:
If you do not return the same string for the id
and value
you will get confusing results in your UI.
Custom Source Callback
Here is an example of a custom source
callback. This example mimics the same result if you had used the route
option.
Template
Your form template might look something like this (The screenshots above used this template bit).
Note: The widget_addon
attribute is a mopa/bootstrap-bundle
attribute.
Notes
This bundle renders its form elements in standard Symfony style. You will have to override the form blocks to get the proper Bootstrap styles applied. I strongly suggest something like mopa/bootstrap-bundle that will override the Symfony form templates with proper Bootstrap versions automatically for you.