1. Go to this page and download the library: Download adt/ajax-select library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
adt / ajax-select example snippets
class BasePresenter extends ... {
use \ADT\Components\AjaxSelect\Traits\AjaxServiceSignalTrait;
namespace App\Queries;
interface IUserFactory {
/** @return User */
function create();
}
class User extends \ADT\BaseQuery\BaseQuery {
const OPTION_ACTIVE = 'active';
protected $fetchWithDataEmail = FALSE;
public function active() {
$this->filter[static::OPTION_ACTIVE] = function (\Kdyby\Doctrine\QueryBuilder $qb) {
$qb->andWhere('e.active = TRUE');
};
return $this;
}
public function disableActiveFilter() {
unset($this->filter[static::OPTION_ACTIVE]);
return $this;
}
protected function doCreateQuery(\Kdyby\Persistence\Queryable $repository) {
$qb = parent::doCreateQuery($repository);
$qb->addOrderBy('e.name');
return $qb;
}
/**
* @param Queryable|null $repository
* @param int $hydrationMode
* @return array|\Kdyby\Doctrine\ResultSet|mixed|object|\stdClass|null
*/
public function fetch(?Queryable $repository = null, $hydrationMode = AbstractQuery::HYDRATE_OBJECT)
{
$fetch = parent::fetch($repository, $hydrationMode);
if ($this->fetchWithDataEmail) {
$array = [];
foreach ($fetch as $person) {
$array[$person->getId()] = \Nette\Utils\Html::el('option')
->setAttribute('value', $person->getId())
->setHtml($person->getName())
->setAttribute('data-email', $person->getEmail());
}
$fetch = $array;
}
return $fetch;
}
/**
* @return $this
*/
public function fetchOptionsWithEmail()
{
$this->fetchWithDataEmail = TRUE;
return $this;
}
/**
* @return bool
*/
public function callSelectPairsAuto()
{
return ! $this->fetchWithDataEmail && parent::callSelectPairsAuto();
}
}
// Active users with default user
$entityForm->addDynamicSelect('user', 'Active users', $this->userQueryFactory->create()->active())
->setRequired(TRUE);
// All users without default user
$entityForm->addDynamicSelect('allUser', 'All users', $this->userQueryFactory->create(), NULL, [
\ADT\Components\AjaxSelect\DI\AjaxSelectExtension::CONFIG_OR_BY_ID_FILTER => FALSE
]);
// Active users with email in label
$entityForm->addDynamicSelect('userWithEmail', 'All users', $this->userQueryFactory->create()->selectPairs('nameWithEmail'));
// Attribute that is not mapped so CONFIG_OR_BY_ID_FILTER must be turned off
$entityForm->addDynamicSelect('profession', 'Profession', $this->userQueryFactory->create(), NULL, [
\ADT\Components\AjaxSelect\DI\AjaxSelectExtension::CONFIG_OR_BY_ID_FILTER => FALSE
]);
use AjaxServiceSignalTrait {
handleGetAjaxItems as handleYourSignalName;
}
// Form.php
public function init($form) {
$form->addDynamic('address', function ($container) {
$container->addAjaxSelect('city', 'City', function ($ajaxEntity) {
$ajaxEntity
->byCountryCode('CZ');
});
});
// No container exists right now
}
// Form.php
public function init($form) {
$form->addDynamic('address', function ($container) {
$container->addAjaxSelect('city', 'City', function ($ajaxEntity) {
$ajaxEntity
->byCountryCode('CZ');
});
});
// This will create 3 new containers, its "city" input and AjaxEntity
$form->setDefaults([
'address' => [
0 => [],
1 => [],
2 => [],
],
]);
}
latte
{* Form.latte *}
<div n:foreach="[0, 1, 2] as $addressIndex">
{* When you access $form['address'][$addressIndex], then the container, "city" input and its AjaxEntity are created *}
{label $form['address'][$addressIndex]['city'] /} {input $form['address'][$addressIndex]['city']}
</div>
latte
{* Form.latte *}
{* We render only those containers, which already exists. *}
<div n:foreach="$form['address']->getContainers() as $container">
{label $container['city'] /} {input $container['city']}
</div>
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.