vojtys/typeahead

Typeahead (twitter/typeahead.js) for Nette (nette.org) - Forms

Installs: 1 894

Dependents: 0

Suggesters: 0

Security: 0

Stars: 2

Watchers: 2

Forks: 2

Open Issues: 1

Language:JavaScript

v0.4.3 2020-03-22 15:07 UTC

This package is auto-updated.

Last update: 2024-03-29 03:14:59 UTC


README

Typeahead (twitter/typeahead.js) for Nette framework

Install

$ composer require vojtys/typeahead

Versions

State Version Branch Nette PHP
stable v0.4.1 master 3.0+ >=7.1
stable v0.3.3 master 2.4 >=5.6

Configuration

extensions:
	typehead: Vojtys\Forms\Typeahead\TypeaheadExtension

typeahead:
	limit: 10
	minLength: 2
	highlight: true

Usage

Presenter/Control

public function createComponentTypeaheadForm(): Form
{
    $form = new Form();

    /** @var TypeaheadInput $typeahead */

    // name, label, display (input displayed value), suggestion callback
    $typeahead = $form->addTypeahead('typeahead', 'Typeahead', 'title', function($display, $q) {

        return $this->searchBy($q); // returns array result [title => 'foo', description => 'foo foo']
    });

    $typeahead->setPlaceholder('Začni psát...'); // initial placeholder

    // add handlebars templates (http://handlebarsjs.com/)

    // suggestion template
    $typeahead->setSuggestionTemplate(function(Html $template) {
        $inner = Html::el('div')->setText('{{title}} – {{description}}');

        return $template->addHtml($inner);
    });

    // empty template
    $typeahead->setNotFoundTemplate(function(Html $template) {
        $inner = Html::el('div')->setText('nic tu neni');

        return $template->addHtml($inner);
    });

    $form->addSubmit('ok', 'Odeslat');

    return $form;
}

css

<link rel="stylesheet" type="text/css" href="https://www.example.com/vendor/vojtys/assets/css/typeahead.css">

js

Before </body> element.

<!-- nette.ajax.js -->
<script src='https://www.example.com/vendor/vojtys/assets/js/nette.ajax.js'></script>
<!-- handlebars.js -->
<script src='https://www.example.com/vendor/vojtys/assets/js/handlebars.min-v4.7.3.js'></script>
<!-- typehead.js -->
<script src='https://www.example.com/vendor/vojtys/assets/js/typeahead.js'></script>
<script src='https://www.example.com/vendor/vojtys/assets/js/vojtys.typeahead.js'></script>