Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implements {empty} tag in Lister #611

Merged
merged 2 commits into from
Dec 2, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 25 additions & 1 deletion demos/lister.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,19 @@
include 'init.php';
include 'database.php';

// default lister
$app->add('Header')->set('Default lister');
$app->add(['Lister', 'defaultTemplate'=>'lister.html'])->setSource([
['icon'=>'map marker', 'title'=>'Krolewskie Jadlo', 'descr'=>'An excellent polish restaurant, quick delivery and hearty, filling meals'],
['icon'=> 'map marker', 'title'=>'Xian Famous Foods', 'descr'=>'A taste of Shaanxi\'s delicious culinary traditions, with delights like spicy cold noodles and lamb burgers.'],
['icon'=> 'check', 'title'=>'Sapporo Haru', 'descr'=>'Greenpoint\'s best choice for quick and delicious sushi'],
]);
$app->add(['ui' => 'clearing divider']);

// lister with custom template
$view = $app->add(['View', 'template' => new \atk4\ui\Template('<div>
<div class="ui header">Top 5 countries (alphabetically)</div>
{List}<div class="ui icon label"><i class="{iso}ae{/} flag"></i> {name}andorra{/}</div>{/}
{List}<div class="ui icon label"><i class="{$iso} flag"></i> {$name}</div>{/}
</div>')]);

$view->add('Lister', 'List')
Expand All @@ -21,3 +25,23 @@
})
->setModel(new Country($db))
->setLimit(20);

$app->add(['ui' => 'clearing divider']);

// empty lister with default template
$app->add('Header')->set('Empty default lister');
$app->add(['Lister', 'defaultTemplate'=>'lister.html'])->setSource([]);
$app->add(['ui' => 'clearing divider']);

// empty lister with custom template
$view = $app->add(['View', 'template' => new \atk4\ui\Template('<div>
<div class="ui header">Empty lister with custom template</div>
{List}<div class="ui icon label"><i class="{$iso} flag"></i> {$name}</div>{empty}no flags to show here{/}{/}
</div>')]);

$view->add('Lister', 'List')
->addHook('beforeRow', function ($l) {
$l->current_row['iso'] = strtolower($l->current_row['iso']);
})
->setModel(new Country($db))
->addCondition('id', -1); // no such records so model will be empty
37 changes: 33 additions & 4 deletions src/Lister.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,13 @@ class Lister extends View
*/
public $t_row = null;

/**
* Lister use this part of template in case there are no elements in it.
*
* @var null|Template
*/
public $t_empty;

public $defaultTemplate = null;

/**
Expand All @@ -31,6 +38,9 @@ class Lister extends View
*/
public $ipp = null;

/**
* Initialization.
*/
public function init()
{
parent::init();
Expand All @@ -39,15 +49,21 @@ public function init()
}

/**
* From the current template will extract {row} into $this->t_row.
*
* @return void
* From the current template will extract {row} into $this->t_row and {empty} into $this->t_empty.
*/
public function initChunks()
{
if (!$this->template) {
throw new Exception(['Lister does not have default template. Either supply your own HTML or use "defaultTemplate"=>"lister.html"']);
}

// empty row template
if ($this->template->hasTag('empty')) {
$this->t_empty = $this->template->cloneRegion('empty');
$this->template->del('empty');
}

// data row template
if ($this->template->hasTag('row')) {
$this->t_row = $this->template->cloneRegion('row');
$this->template->del('rows');
Expand Down Expand Up @@ -108,14 +124,15 @@ public function renderView()
if (!$this->template) {
throw new Exception(['Lister requires you to specify template explicitly']);
}

$this->t_row->trySet('_id', $this->name);
$rowHTML = '';

// if no model is set, don't show anything (even warning)
if (!$this->model) {
return parent::renderView();
}

$rowHTML = '';
$this->_rendered_rows_count = 0;
foreach ($this->model as $this->current_id => $this->current_row) {
if ($this->hook('beforeRow') === false) {
Expand All @@ -136,6 +153,17 @@ public function renderView()
$this->_rendered_rows_count++;
}

// empty message
if (!$this->_rendered_rows_count) {
if (!$this->jsPaginator || !$this->jsPaginator->getPage()) {
if ($this->t_row == $this->template) {
$rowHTML = $this->t_empty->render();
} else {
$this->template->appendHTML('rows', $this->t_empty->render());
}
}
}

if ($this->t_row == $this->template) {
$this->template = new Template('{$c}');
$this->template->setHTML('c', $rowHTML);
Expand All @@ -144,6 +172,7 @@ public function renderView()
//$this->template->set('_top', $rowHTML);
}

// stop jsPaginator if there are no more records to fetch
if ($this->jsPaginator && ($this->_rendered_rows_count < $this->ipp)) {
$this->jsPaginator->jsIdle();
}
Expand Down
44 changes: 25 additions & 19 deletions src/Table.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,13 +118,37 @@ class Table extends Lister
*/
public $sort_order = null;

/**
* Constructor.
*
* @param null|string $class CSS class to add
*/
public function __construct($class = null)
{
if ($class) {
$this->addClass($class);
}
}

/**
* initChunks method will create one column object that will be used to render
* all columns in the table unless you have specified a different
* column object.
*/
public function initChunks()
{
if (!$this->t_head) {
$this->t_head = $this->template->cloneRegion('Head');
$this->t_row_master = $this->template->cloneRegion('Row');
$this->t_totals = $this->template->cloneRegion('Totals');
$this->t_empty = $this->template->cloneRegion('Empty');

$this->template->del('Head');
$this->template->del('Body');
$this->template->del('Foot');
}
}

/**
* Defines a new column for this field. You need two objects for field to
* work.
Expand Down Expand Up @@ -400,25 +424,6 @@ public function addTotals($plan = [])
$this->totals_plan = $plan;
}

/**
* initChunks method will create one column object that will be used to render
* all columns in the table unless you have specified a different
* column object.
*/
public function initChunks()
{
if (!$this->t_head) {
$this->t_head = $this->template->cloneRegion('Head');
$this->t_row_master = $this->template->cloneRegion('Row');
$this->t_totals = $this->template->cloneRegion('Totals');
$this->t_empty = $this->template->cloneRegion('Empty');

$this->template->del('Head');
$this->template->del('Body');
$this->template->del('Foot');
}
}

/**
* Sets data Model of Table.
*
Expand Down Expand Up @@ -510,6 +515,7 @@ public function renderView()
} else {
}

// stop jsPaginator if there are no more records to fetch
if ($this->jsPaginator && ($this->_rendered_rows_count < $this->ipp)) {
$this->jsPaginator->jsIdle();
}
Expand Down
2 changes: 1 addition & 1 deletion src/Template.php
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ public function getTagRefList($tag, &$template)
}

if ($this->isTopTag($tag)) {
$template = &$this->template;
$template = &$this->template; // BUG IS HERE - THIS DOESN'T RETURN REFERENCE TO THIS->TEMPLATE !!!

return false;
}
Expand Down
19 changes: 11 additions & 8 deletions template/semantic-ui/lister.html
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@

<div id="{$_id}" class="{_ui}ui{/} {$class} {_class}list{/}" style="{$style}" {$attributes}>{rows}
<div class="ui list">{row}
<div class="item"><i class="{$icon} icon"></i>
<div class="content"><a class="header" href="{$_href}">{$_title}</a>
<div class="description">{descr}Value of "descr" column appears here. See docs on how to override Lister template{/}</div>
</div>
</div>{/}
</div>{/}</div>
<div class="ui list">{row}
<div class="item"><i class="{$icon} icon"></i>
<div class="content"><a class="header" href="{$_href}">{$_title}</a>
<div class="description">{descr}Value of "descr" column appears here. See docs on how to override Lister template{/}</div>
</div>
</div>{/row} {empty}
<div class="item">
<div class="content">
<div class="description">No records</div>
</div>
</div>{/empty}</div>{/rows}</div>
24 changes: 15 additions & 9 deletions template/semantic-ui/lister.pug
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
<div id="{$_id}" class="{_ui}ui{/} {$class} {_class}list{/}" style="{$style}" {$attributes}>
| {rows}
.ui.list
| {rows}
.ui.list
| {row}
.item
i(class="{$icon} icon")
.content
a.header(href="{$_href}") {$_title}
.description
| {descr}Value of "descr" column appears here. See docs on how to override Lister template{/}
| {/}
| {/}
i(class="{$icon} icon")
.content
a.header(href="{$_href}") {$_title}
.description
| {descr}Value of "descr" column appears here. See docs on how to override Lister template{/}
| {/row}
| {empty}
.item
.content
.description
| No records
| {/empty}
| {/rows}
</div>
9 changes: 4 additions & 5 deletions template/semantic-ui/table.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<table id="{$_id}" class="{_ui}ui{/} {$class} {_class}{/}" style="{$style}" {$attributes}>
<table id="{$_id}" class="{_ui}ui{/} {$class} {$_class}" style="{$style}" {$attributes}>
<thead>{Head}
<tr>{$cells}</tr>
{/}
Expand All @@ -14,14 +14,13 @@
<td colspan="{$columns}">No records.</td>
</tr>
{/}
{/tbody}
{/Body}
</tbody>{has_foot}
<tfoot>
{Foot}
{Totals}
<tr>{$cells}</tr>
{/}
{Paginator}
{$Paginator}
{/}
{/}
</tfoot>{/}</table>
</tfoot>{/}</table>
7 changes: 3 additions & 4 deletions template/semantic-ui/table.pug
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<table id="{$_id}" class="{_ui}ui{/} {$class} {_class}{/}" style="{$style}" {$attributes}>
<table id="{$_id}" class="{_ui}ui{/} {$class} {$_class}" style="{$style}" {$attributes}>
thead

//- Defines how header is rendered
Expand All @@ -23,7 +23,7 @@ tbody
td(colspan="{$columns}") No records.
| {/}

| {/tbody}
| {/Body}

| {has_foot}
tfoot
Expand All @@ -35,8 +35,7 @@ tfoot
| {/}

//- Paginator is used by a separate class
| {Paginator}
| {/}
| {$Paginator}

| {/}
| {/}
Expand Down