tomkirsch/sorter

CI4 的排序库

安装: 24

依赖: 0

建议: 0

安全: 0

星标: 0

关注者: 1

分支: 0

开放问题: 0

类型:项目

v1.3.2 2023-10-18 05:14 UTC

This package is auto-updated.

Last update: 2024-09-18 07:51:15 UTC


README

安装

App\Config\Services 中添加服务

	public static function sorter(array $tables=[], ?string $url=NULL, $getShared=TRUE){
		$sorter = $getShared ? static::getSharedInstance('sorter', $tables, $url) : new \Tomkirsch\Sorter\Sorter($tables, $url);
		return $sorter;
	}

用法

在控制器中,你可以通过服务访问它。如果你想设置,可以传递一个关联数组。

	$sorter = service('Sorter');
	// or pass a configuration to save a step:
	$sorter = service('Sorter', [
		'foo'=>'foo_id asc',
		'bar'=>'bar_id asc',
	]);

要添加表格,使用 addTable() 方法

	// add your tables. The "table name" is only used in GET parameters, so it doesn't need to match your database
	$sorter->addTable('foo', 'foo_id', 'asc');
	// you can pass the default sort using a space too:
	$sorter->addTable('bar', 'bar_id asc');

只需调用 getSort($tableName) 来获取当前的排序字段和方向。如果没有传递任何值,它将使用你上面指定的默认值。

	// this quick version uses the first table
	$list = model('MyModel')->orderBy($sorter->getSort())->findAll();

	// if you have more than one table, you'll want to pass the name
	$list = model('MyModel')->orderBy($sorter->getSort('foo'))->findAll();

在你的视图中,使用 QuickTable 来配置列、模板,并输出 thead 和 tbody

<?php
$qt = $sorter->quickTable('foo') // pass the table name, or you can omit for only 1 table
	// define your columns and how you'd like the values to be formatted...
	// simple
	->addCol('customer_id', 'Customer Number')

	// template with variable substitution
	->addCol('customer_address', 'Address', 'asc', '$customer_address<br>$customer_city')

	// typical formats
	->addCol('iscash', 		'Cash', 		'desc', 'yesno')
	->addCol('amount', 		'Amount', 		'desc', 'money')
	->addCol('balance', 	'Balance', 		'desc', 'balance') // negative values are shown with parentheses
	->addCol('widgetcount', '# of Widgets', 'desc', 'number')	// number with grouped thousands
	->addCol('ph_level', 	'pH', 			'desc', 'number|1') // 1 decimal place (arguments separated by pipe)
	->addCol('created', 	'Created', 		'desc', 'datetime')
	->addCol('thetime', 	'Time', 		'desc', 'time')
	->addCol('schedule', 	'Sched Date', 	'desc', 'dateFormat|D m/d') // custom date format (arguments separated by pipe)

	// closure (most flexible - however you MUST return TD tags)
	->addCol('customer_iscash', 'Cash', 'desc', function($value, $row){
		return '<td class="bg-success">'.($value ? 'Yes' : 'No').'</td>';
	})

	// you can also define a closure for the opening <tr> tag:
	->rowTemplate(function($row){
		return '<tr data-foobar="'.$row->id.'">';
	})
;
?>
<?= $qt->table($list, 'class="table table-bordered"') ?>

为了更多的控制,你还可以在你的视图中使用 anchorIcon()、anchor()、url()、queryString() 或 queryArray() 来构建链接

	<th><?= $sorter->anchorIcon('category_ordernum desc', 'Order') ?></th>

	// or if you have more than one table:
	<th><?= $sorter->anchorIcon('category_ordernum desc', 'Order', '', 'cat') ?></th>

查看类中的这些方法以获取更多用法(GET 参数、HTML 属性等)