PHP code example of ublaboo / datagrid-elasticsearch-data-source

1. Go to this page and download the library: Download ublaboo/datagrid-elasticsearch-data-source 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/ */

    

ublaboo / datagrid-elasticsearch-data-source example snippets




namespace App\Presenters;

use Elasticsearch\Client;
use Nette\Application\UI\Presenter;
use Ublaboo\DataGrid\DataGrid;
use Ublaboo\DatagridElasticsearchDataSource\ElasticsearchDataSource;

final class UsersPresenter extends Presenter
{

	/**
	 * @var ElasticsearchDataSource
	 */
	private $elasticsearchDataSource;


	public function __construct(Client $client)
	{
		$this->elasticsearchDataSource = new ElasticsearchDataSource(
			$client, // Elasticsearch\Client
			'users', // Index name
			'user' // Index type
		);
	}


	public function createComponentUsersGrid(): DataGrid
	{
		$grid = new DataGrid;

		$grid->setDataSource($this->elasticsearchDataSource);

		$grid->addColumnText('id', '#')->setSortable();
		$grid->addColumnLink('nickname', 'Nickname', 'edit')
			->setFilterText();
		$grid->addColumnText('username', 'E-mail (username)')
			->setFilterText();
		$grid->addColumnText('age', 'Age')
			->setSortable()
			->setFilterRange();
		$grid->addColumnText('status', 'Status')
			->setFilterMultiSelect([
				'active' => 'Active',
				'disabled' => 'Disabled',
			]);
		$grid->addColumnDateTime('created', 'Created')
			->setFormat('j. n. Y H:i:s')
			->setFilterDateRange();

		return $grid;
	}
}