tomaskarlik / select2input
Select2 form input for Nette
Installs: 13 063
Dependents: 0
Suggesters: 0
Security: 0
Stars: 2
Watchers: 2
Forks: 6
Open Issues: 2
Requires
- php: ^7.1
- nette/application: ^2.4
- nette/di: ^2.4
- nette/forms: ^2.4
- nette/php-generator: ^2.4
- nette/reflection: ^2.4
- nette/utils: ^2.4
This package is not auto-updated.
Last update: 2024-12-17 05:07:24 UTC
README
Install
Download package
composer require tomaskarlik/select2input
Register extension in config.neon
select2: TomasKarlik\Select2Input\DI\Select2InputExtension
Download Select2 or add to bower.json
and include JS to your project
"dependencies": { "select2": "~4.0" }
$(document).ready(function() { $('.select2').each(function() { $(this).select2({ ajax: { url: $(this).data('select2-url'), dataType: 'json', // custom parameters } }); }); });
Form usage
$form = new Form; $form->addSelect2('client_id', $clientRepository, 'Klient:') ->setRequired('Vyberte klienta!') ->setResultsPerPage(15); $form->addSelect2Multiple('clients', $clientRepository, 'Klienti:')
Datasource example
namespace App\Model\Repository; use TomasKarlik\Select2Input\ISelect2DataSource; use TomasKarlik\Select2Input\Select2ResultEntity; class ClientRepository implements ISelect2DataSource { /** * @param string $query * @param int $limit * @param int $offset * @return Select2ResultEntity[] */ public function searchTerm(string $query, int $limit, int $offset): array { $return = []; $selection = $this->getClientTable() ->where( 'company ILIKE ?', '%' . $query . '%' ) ->order('company') ->select('client.id, client.company') ->limit($limit, $offset); while ($row = $selection->fetch()) { $result = new Select2ResultEntity; $result->setId($row->id); $result->setText($row->company); $return[] = $result; } return $return; } /** * @param string $query * @return int */ public function searchTermCount(string $query): int { return $this->getClientTable() ->where( 'company ILIKE ?', '%' . $query . '%' ) ->count('*'); } /** * @param mixed $key * @return Select2ResultEntity|NULL */ public function findByKey($key): ?Select2ResultEntity { if ( ! is_numeric($key)) { return NULL; } $client = $this->getClientTable()->wherePrimary($key)->fetch(); if ( ! $client) { return NULL; } $result = new Select2ResultEntity; $result->setId($client->id); $result->setText($client->company); return $result; } }