carrooi/no-grid

不是 Nette 框架的网格

2.0.2 2017-05-17 15:46 UTC

This package is auto-updated.

Last update: 2024-09-06 03:41:41 UTC


README

Build Status Donate

NoGrid

绝对不是网格,只是一个用于在自定义模板中打印数据的简单控件,带有分页器。

总是显示一些自动生成的数据网格并不是一件好事,主要在前端,而这个包就是为了这样的时刻。

BC Break!

请注意,此包完全重写了版本 2.0.0。请阅读新的README。

特性

它有

  • 带有自定义模板选项的分页器
  • Latte 宏以简化模板
  • 视图(例如,用于存档和未存档的数据)
  • 不同的数据源
  • 过滤

它没有

  • 默认网格模板
  • CSS 样式
  • JS 脚本
  • 排序
  • 表单

它可能在将来获得一些这些特性。

安装

$ composer require carrooi/no-grid

现在您可以注册 Nette 的扩展

extensions:
    grid: Carrooi\NoGrid\DI\NoGridExtension

配置

grid:
    itemsPerPage: 20
    paginator:
        template: %appDir%/paginator.latte
        templateProvider: App\Grid\TemplateProvider
  • itemsPerPage: 默认为 10
  • paginator/template: 不必需
  • paginator/templateProvider: 模板提供者的类名,必须是 Carrooi\NoGrid\IPaginatorTemplateProvider 接口的实例。不必需

定义

use Carrooi\NoGrid\DataSource\ArrayDataSource;
use Nette\Application\UI\Presenter;

class BooksPresenter extends Presenter
{

	/** @var \Carrooi\NoGrid\INoGridFactory @inject */
	public $gridFactory;
	
	protected function createComponentBooksGrid()
	{
		$dataSource = new ArrayDataSource([			// Read more about data sources below
			['title' => 'Lord of the Rings'],
			['title' => 'Harry Potter'],
			['title' => 'Narnia'],
		]);
	
		$grid = $this->gridFactory->create($dataSource);
		
		return $grid;
	}

}

转换从数据源加载的数据

$grid->transformData(function($line) {
	return [
		'id' => $line->getId(),
		'title' => $line->getTitle(),
	];
});

打印

<table n:no-grid="booksGrid">
	<thead>
		<tr>
			<th>Title</th>
		</tr>
	</thead>
	<tbody>
		<tr n:no-grid-data-as="$line">
			<td>{$line[title]}</td>
		</tr>
	</tbody>
	<tfoot>
		<tr>
			<th>Display {$noGrid->getCount()} items from {$noGrid->getTotalCount()}</th>
		</tr>
		<tr>
			<td n:no-grid-not-empty>{control booksGrid:paginator}</td>
			<td n:no-grid-empty>No data found...</td>
		</tr>
	</tfoot>
</table>

Latte 宏

  • no-grid: 开始网格渲染(类似于 {form} 宏)
  • no-grid-data-as: 遍历数据源中的数据并将当前行保存到给定的变量中
  • no-grid-views-as: 遍历当前 NoGrid 中的视图并将视图数据保存到给定的变量中(关于视图的更多信息请见下文)
  • no-grid-not-empty: 仅当有数据时才处理内容
  • no-grid-empty: 仅当没有数据时才处理内容
  • no-grid-has-paginator: 仅当分页器应渲染时才处理内容

您还可以看到分页器可以与 {control booksGrid:paginator} 一起渲染。

这些 Latte 宏不能用作“非属性”宏,因此还有以 camelCase 编写的变体

  • noGrid
  • noGridDataAs
  • noGridViewsAs
  • noGridNotEmpty
  • noGridEmpty
  • noGridHasPaginator

视图

想象一下,您想创建例如两个标签页——“活动书籍”和“已售书籍”。这可以使用视图轻松完成。

定义

protected function createComponentBooksGrid()
{
	$dataSource = new ArrayDataSource([			// Read more about data sources below
		[
			'title' => 'Lord of the Rings,
			'sold' => true,
		'],
		[
			'title' => 'Harry Potter,
			'sold' => false,
		'],
		[
			'title' => 'Narnia,
			'sold' => false,
		'],
	]);

	$grid = $this->gridFactory($dataSource);
	
	$grid->addView('active', 'Active', function(array &$data) {
		$data = array_filter($data, function($book) {
			return !$book['sold'];
		});
	});
	
	$grid->addView('sold', 'Sold out', function(array &$data) {
		$data = array_filter($data, function($book) {
			return $book['sold'];
		});
	});
	
	return $grid;
}

显示

<div n:no-grid="booksGrid">
	<ul>
		<li n:no-grid-views-as="$view" n:class="$view->isCurrent() ? active">
			<a href="{$view->getLink()}">
				{$view->getTitle()}
			</a>
		</li>
	</ul>
	<table>
		<!-- same like previous example -->
	</table>
</div>

过滤

支持的条件

  • Condition::SAME(默认)
  • Condition::NOT_SAME
  • Condition::IS_NULL
  • Condition::IS_NOT_NULL
  • Condition::LIKE
$form = new Form;
$form->addText('name');
$form->addSubmit('search', 'Search!');

$grid->setFilteringForm($form);

// setting filters is not required
$grid->addFilter('name', Condition::LIKE, [
	Condition::CASE_INSENSITIVE => true,
], function($name) {

	// update value before sending query to database
	return '%'. $name. '%';
});

现在您只需要在模板中显示表单输入。

不要渲染数组的开始和结束,它会自动渲染!

数据源

  • Carrooi\NoGrid\DataSource\ArrayDataSource(array)
  • Carrooi\NoGrid\DataSource\Doctrine\DataSource(Doctrine\ORM\QueryBuilder)
  • Carrooi\NoGrid\DataSource\Doctrine\QueryObjectDataSource(Kdyby\Persistence\Queryable, Kdyby\Doctrine\QueryObject)
  • Carrooi\NoGrid\DataSource\Doctrine\QueryFunctionDataSource(Kdyby\Persistence\Queryable, Carrooi\NoGrid\DataSource\DoctrineQueryFunction)