Skip to content

Instantly share code, notes, and snippets.

@Kedrigern
Last active December 18, 2015 02:49
Show Gist options
  • Save Kedrigern/5714127 to your computer and use it in GitHub Desktop.
Save Kedrigern/5714127 to your computer and use it in GitHub Desktop.
Verry simple grid for conrete app (no plans to extends to standalone component).
{**
* @package Carty
*
* New list
*
* @param string[][] $columns like:
* array(
* array(
* 'heading' => '...', // required
* 'name' => '...',
* 'sort' => true|false,
* 'width' => '10%'
* 'prefix' => '',
* 'sufix' => '',
* )
* );
* @param string[][] $actions like:
* array(
* array(
* 'class' => 'btn btn-mini btn-primary ajax',
* 'plink' => 'edit',
* 'label' => '..'
* )
* );
* @param \Nette\Database\Table\ActiveRow[] $items
**}
{if count($items)>0}
<table class="table table-bordered">
<thead>
<tr>
<th width="6%" n:if="$switches['id']">
Id
</th>
<th width="4%" n:if="$switches['default']">
Výchozí
</th>
<th width="6%" n:if="$switches['sort']">
Řazení
</th>
<th width="4%" n:if="$switches['state']">
Stav
</th>
{foreach $columns as $column}
<th n:attr="width => $column['width']">
{$column['heading']}
{if isset($column['sort'])}
<div n:if="$column['sort']" class="span pull-right">
<a href="#" class="ajax">▲</a>
<a href="#" class="ajax">▼</a>
</div>
{/if}
</th>
{/foreach}
<th width="15%">Akce</th>
</tr>
</thead>
<tbody n:snippet="itemsList">
<tr n:foreach="$items->order('sort') as $item">
<td n:if="$switches['id']">
{$item->id}
</td>
<td n:if="$switches['default']">
<div n:snippet="default-$item->id" class="text-center">
{if $item->default}
<span class="btn btn-mini"><i n:class="icon-ok"></i></span>
{else}
<a class="btn btn-mini ajax" href="#"><i n:class="icon-pause"></i></a>
{/if}
</div>
</td>
<td n:if="$switches['sort']">
<div n:if="$iterator->count()>0" class="btn-group">
<button n:if="$iterator->isFirst()" class="btn btn-mini"><i class="icon-right"></i></button>
<a class="btn btn-mini ajax" href="#" n:if="!$iterator->isFirst()"><i class="icon-chevron-up"></i></a>
<a class="btn btn-mini ajax" href="#" n:if="!$iterator->isLast()"><i class="icon-chevron-down"></i></a>
<button n:if="$iterator->isLast()" class="btn btn-mini"><i class="icon-right"></i></button>
{if $iterator->count()>1}
<button class="btn btn-mini dropdown-toggle" data-toggle="dropdown"><span class="caret"></span>
</button>
<ul class="dropdown-menu">
<li n:if="!$iterator->isFirst()"><a class="ajax" href="#"><i class="icon-chevron-up"></i> První</a>
</li>
<li n:if="!$iterator->isLast()"><a class="ajax" href="#"><i class="icon-chevron-down"></i>
Poslední</a></li>
</ul>
{/if}
</div>
</td>
<td n:if="$switches['state']">
<a n:if="$item->state == 'active'" class="btn btn-mini btn-success ajax" href="#">
<i class="icon-ok icon-white"></i>
</a>
<a n:if="$item->state == 'inactive'" class="btn btn-mini btn-danger ajax" href="#">
<i class="icon-pause icon-white"></i>
</a>
</td>
<td n:foreach="$columns as $column">
{if isset($column['prefix'])}{$column['prefix']}{/if}
{if isset($column['name'])}{$item->$column['name']}{/if}
{if isset($column['sufix'])}{$column['sufix']}{/if}
</td>
<td>
<div class="text-center" n:inner-foreach="$actions as $action">
<a class="{$action['class']}" href="{plink $action['plink'] $item->id}">{!$action['label']}</a>
</div>
</td>
</tr>
</tbody>
</table>
{/if}
<?php
namespace BackendModule;
/**
* New List Control
*/
class NewList extends BaseControl
{
/**
* @var string[][] like:
* array(
* array(
* 'heading' => '...', // required
* 'name' => '...',
* 'sort' => true|false,
* 'width' => '10%'
* 'prefix' => '...',
* 'sufix' => '...',
* )
* );
*/
protected $columns = array();
/**
* @var string[][] like:
* array(
* array(
* 'class' => 'btn btn-mini btn-primary ajax',
* 'plink' => 'edit', // int id is given automatically
* 'label' => '...' // html will be interpreted
* )
* );
*/
protected $actions = array();
/**
* @todo maybe better bool vars?
* @var array
*/
protected $switches = array(
'id' => true,
'default' => true,
'sort' => true,
'state' => true
);
/** @var \Nette\Database\Table\Selection */
protected $selection;
/**
* @param string $heading of column
* @param string[] $options (assoc with optional keys: name, sort, width, prefix, sufix)
* @return NewList
*/
public function addColumn($heading, array $options = array() )
{
$options['heading'] = $heading;
if(!isset($options['width'])) $options['width'] = false;
$this->columns[] = $options;
return $this;
}
/**
* @param string[] $options assoc with keys: class. plink, label
* @return NewList
*/
public function addAction(array $options)
{
$this->actions[] = $options;
return $this;
}
/** @param \Nette\Database\Table\Selection */
public function setSelection($selection)
{
$this->selection = $selection;
}
/**
* Set default actions edit and delete
*/
public function addDefaultActions()
{
$this->addAction(array(
'class' => 'btn btn-mini btn-primary ajax',
'plink' => 'edit',
'label' => "<i class='icon-pencil icon-white'></i>&nbsp;Upravit"
));
$this->addAction(array(
'class' => 'btn btn-mini btn-danger ajax',
'plink' => 'delete',
'label' => "<i class='icon-trash icon-white'></i>&nbsp;Upravit"
));
return $this;
}
public function render()
{
$this->template->columns = $this->columns;
$this->template->actions = $this->actions;
$this->template->switches = $this->switches;
$this->template->items = $this->selection;
parent::render(); // must be at end, because in parent::render is call template->render()
}
}
<?php
namespace BackendModule;
use \Nette\Application\UI\Form;
class SomePresenter extends BasePresenter
{
public function renderDefault()
{
//$items je např. \Nette\Database\Selection (číli klidně join více tabulek)
$this['list1']->selection = $items;
$this['list2']->selection = $items;
}
/** @return NewList */
protected function createComponentList1()
{
return (new NewList())
->addDefaultActions()
->addColumn('Jméno', array('name'=> 'name'))
->addColumn('Jméno domény', array('name' => 'domain_name'));
}
/** @return NewList */
protected function createComponentList2()
{
return (new NewList())
->addColumn('Název', array('name'=> 'name', 'prefix' => '$', 'sufix' => '%', 'width' => '15%'))
->addColumn('Jméno', array('name' => 'name', 'sort' => true));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment