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

Fixes and few new features (insert, delete) #92

Open
wants to merge 17 commits into
base: master
Choose a base branch
from
Open
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
36 changes: 36 additions & 0 deletions src/Column.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ class Column
/** @var Datagrid */
protected $grid;

/** @var array */
protected $attributes = [];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No idea what is the purpose.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For example

$grid->addColumn('institute')->setAttribute('help', 'School where you ...');

and then you can render nice help icon in column head.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🆗



public function __construct($name, $label, Datagrid $grid)
{
Expand All @@ -37,6 +40,39 @@ public function __construct($name, $label, Datagrid $grid)
}


/**
* @param string $name
* @param mixed $value
* @return self
*/
public function setAttribute($name, $value = true)
{
$this->attributes[$name] = $value;
return $this;
}


/**
* @param string $name
* @return bool
*/
public function hasAttribute($name)
{
return isset($this->attributes[$name]);
}


/**
* @param string $name
* @param mixed $default
* @return self
*/
public function getAttribute($name, $default = null)
{
return $this->attributes[$name] ?? $default;
}


public function enableSort($default = NULL)
{
$this->sort = TRUE;
Expand Down
56 changes: 48 additions & 8 deletions src/Datagrid.blocks.latte
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@
{/if}
{/define}

{define row-head-cell}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please document this.

{$column->label}
{/define}

{define row-head-columns}
<tr class="grid-columns">
{ifset $form[actions]}
Expand All @@ -28,7 +32,7 @@
{foreach $columns as $column}
<th class="grid-col-{$column->name}">
{if $column->canSort()}
<a href="{link sort! orderColumn => $column->name, orderType => $column->getNewState()}" class="ajax">{$column->label}</a>
<a href="{link sort! orderColumn => $column->name, orderType => $column->getNewState()}" class="ajax">{include #row-head-cell column => $column}</a>
{if $column->isAsc()}
<span class="grid-sort-symbol grid-sort-symbol-asc"><em>&#9650;</em></span>
{elseif $column->isDesc()}
Expand All @@ -37,7 +41,7 @@
<span class="grid-sort-symbol grid-sort-symbol-no"></span>
{/if}
{else}
{$column->label}
{include #row-head-cell column => $column}
{/if}
</th>
{/foreach}
Expand Down Expand Up @@ -91,6 +95,40 @@
<a href="{link edit! $primary}" class="ajax" data-datagrid-edit>{$control->translate(Edit)}</a>
{/define}

{define row-edit-control}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please document this.

{input $column->name}
<p class="error" n:foreach="$formContainer[$column->name]->getErrors() as $error">{$error}</p>
{/define}

{define row-insert-control}
{input $formContainer[$column->name]}
{/define}

{define row-insert-empty}
{/define}

{define row-insert-button}
{input button}
{/define}

{define row-actions-delete-link}
<a href="{link delete! $primary}" class="ajax" data-datagrid-delete>{$control->translate(Delete)}</a>
{/define}

{define row-insert}
{formContainer insert-data}
{foreach $columns as $column}
{ifset $formContainer[$column->name]}
<td class="grid-col-{$column->name}">{include #row-insert-control form => $form, formContainer => $formContainer, column => $column}</td>
{else}
<td class="grid-col-{$column->name}">{include #row-insert-empty form => $form, formContainer => $formContainer, column => $column}</td>
{/}
{/foreach}
{/formContainer}
<td class="grid-col-actions" n:formContainer="insert">{include #row-insert-button form => $form}</td>
{/define}


{define row}
<tr id={$rowId}>
{include #row-inner row => $row}
Expand All @@ -117,10 +155,7 @@
{include #"cell-edit-{$column->name}" form => $form, column => $column, row => $row}
{else}
{formContainer edit}
{input $column->name}
{if $form[edit][$column->name]->hasErrors()}
<p class="error" n:foreach="$form[edit][$column->name]->getErrors() as $error">{$error}</p>
{/if}
{include #row-edit-control form => $form, formContainer => $formContainer, column => $column, row => $row}
{/formContainer}
{/ifset}
</td>
Expand Down Expand Up @@ -148,8 +183,13 @@
{else}
{ifset #row-actions}
{include #row-actions row => $row, primary => $primary}
{elseif $control->getEditFormFactory()}
{include #row-actions-edit-link row => $row, primary => $primary}
{else}
{if $control->getEditFormFactory()}
{include #row-actions-edit-link row => $row, primary => $primary}
{/if}
{if $control->getDeleteCallback()}
{include #row-actions-delete-link row => $row, primary => $primary}
{/if}
{/ifset}
{/if}
</td>
Expand Down
12 changes: 10 additions & 2 deletions src/Datagrid.latte
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* @license MIT
* @link https://github.com/nextras
*}
<div class="grid" data-grid-name="{$control->getUniqueId()}">
<div class="grid" data-grid-name="{$control->getUniqueId()}" id="{$control->getUniqueId()}">
{snippet rows}

{var $_templates = []}
Expand All @@ -21,7 +21,10 @@
{php
$hasActionsColumn =
(bool) $control->getEditFormFactory() /* we may render only one row so the form[filter] may not be created */
|| isset($this->blockQueue["row-actions"]);
|| isset($this->blockQueue["row-actions"])
|| isset($form["insert"])
|| isset($form["filter"])
|| $control->getDeleteCallback();
$hasGlobalActionsColumn = isset($form['actions']);

foreach ($_templates as $_template):
Expand Down Expand Up @@ -50,6 +53,11 @@
{ifset #empty-result}{include #empty-result}{/ifset}
{/if}
</tbody>
<tfoot n:ifset="$form[insert]">
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it tfoot corrent for inserting?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure where to render. In header, it can be accidentally interchanged with filter by user.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I mean like last tbody's tr? But probbaly its ok :) /shrug

<tr class="grid-inserts">
{include #row-insert}
</tr>
</tfoot>
<tfoot n:if="isset($paginator) || $hasGlobalActionsColumn">
<tr>
<th colspan="{=count($columns) + $hasGlobalActionsColumn + $hasActionsColumn}">
Expand Down
Loading