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

UI/DataTable: initialize with Order and Range #8106

Merged
merged 4 commits into from
Sep 30, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
5 changes: 0 additions & 5 deletions components/ILIAS/UI/src/Component/Table/Data.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,6 @@ public function withActions(array $actions): static;
*/
public function withRequest(ServerRequestInterface $request): static;

/**
* Number of Rows is the amount of rows shown per page
*/
public function withNumberOfRows(int $number_of_rows): self;

/**
* Not all columns are neccessarily visible; "selected optional" is the
* positive list of shown columns (the non-optional columns are always shown
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,10 @@
use ILIAS\UI\Implementation\Component\JavaScriptBindable;
use ILIAS\UI\Component\JavaScriptBindable as JSBindable;
use ILIAS\Data\Factory as DataFactory;


use ILIAS\UI\Component\Input\ViewControl;
use ILIAS\UI\Component\Input\Container\ViewControl as ViewControlContainer;
use ILIAS\Data\Range;
use ILIAS\Data\Order;

class Data extends AbstractTable implements T\Data
{
Expand Down Expand Up @@ -130,9 +129,12 @@ public function applyViewControls(
$data = $view_controls->getData();
$range = $data[self::VIEWCONTROL_KEY_PAGINATION];
$range = ($range instanceof Range) ? $range->croppedTo($total_count ?? PHP_INT_MAX) : null;
$order = $data[self::VIEWCONTROL_KEY_ORDERING];
$order = ($order instanceof Order) ? $order : null;

$table = $table
->withRange($range)
->withOrder($data[self::VIEWCONTROL_KEY_ORDERING] ?? null)
->withOrder($order)
->withSelectedOptionalColumns($data[self::VIEWCONTROL_KEY_FIELDSELECTION] ?? null);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
namespace ILIAS\UI\Implementation\Component\Table;

use ILIAS\UI\Component\Input\ViewControl\Sortation;
use ILIAS\UI\Component\Input\ViewControl\NullControl;
use ILIAS\UI\Implementation\Component\Input\ViewControl;
use ILIAS\UI\Component\Table\Column\Column;
use ILIAS\Data\Order;

Expand All @@ -31,7 +31,7 @@ trait TableViewControlOrdering

protected function initViewControlOrdering(): void
{
$this->order = $this->data_factory->order($this->initialOrder(), Order::ASC);
$this->order = $this->getOrder();
}

private function initialOrder(): string
Expand All @@ -47,7 +47,7 @@ private function initialOrder(): string
return array_key_first($sortable_visible_cols);
}

protected function getViewControlOrdering(int|null $total_count): Sortation|NullControl
protected function getViewControlOrdering(int|null $total_count): Sortation|ViewControl\Group
{

$sortable_visible_cols = array_filter(
Expand All @@ -58,7 +58,10 @@ protected function getViewControlOrdering(int|null $total_count): Sortation|Null
if ($sortable_visible_cols === [] ||
($total_count !== null && $total_count < 2)
) {
return $this->view_control_factory->nullControl();
return $this->view_control_factory->group([
$this->view_control_factory->nullControl(),
$this->view_control_factory->nullControl()
]);
}

$sort_options = [];
Expand All @@ -71,7 +74,10 @@ protected function getViewControlOrdering(int|null $total_count): Sortation|Null
$sort_options[$labels[0]] = $order_asc;
$sort_options[$labels[1]] = $order_desc;
}
return $this->view_control_factory->sortation($sort_options);

$value = $this->getOrder()->join('', fn($ret, $key, $value) => [$key, $value]);
return $this->view_control_factory->sortation($sort_options)
->withValue($value);
}

public function withOrder(?Order $order): self
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ trait TableViewControlPagination

protected function initViewControlpagination(): void
{
$this->range = $this->data_factory->range(0, $this->number_of_rows);
$this->range = $this->getRange();
}

protected function getViewControlPagination(?int $total_count = null): ViewControl\Pagination|ViewControl\Group
Expand All @@ -53,18 +53,6 @@ protected function getViewControlPagination(?int $total_count = null): ViewContr
]);
}

public function withNumberOfRows(int $number_of_rows): self
{
$clone = clone $this;
$clone->number_of_rows = $number_of_rows;
return $clone;
}

public function getNumberOfRows(): int
{
return $this->number_of_rows;
}

public function withRange(?Range $range): self
{
$clone = clone $this;
Expand Down
10 changes: 8 additions & 2 deletions components/ILIAS/UI/src/examples/Table/Data/base.php
Original file line number Diff line number Diff line change
Expand Up @@ -157,10 +157,10 @@ public function getRows(
$this->ui_factory->symbol()->icon()->custom('assets/images/standard/icon_x.svg', '', 'small'),
];
$icon = $icons[2];
if($record['achieve'] > 80) {
if ($record['achieve'] > 80) {
$icon = $icons[0];
}
if($record['achieve'] < 30) {
if ($record['achieve'] < 30) {
$icon = $icons[1];
}
$record['achieve'] = $icon;
Expand Down Expand Up @@ -227,6 +227,12 @@ protected function getRecords(Range $range = null, Order $order = null): array
->data('a data table', $columns, $data_retrieval)
->withId('example_base')
->withActions($actions)

//these are initial settings that apply if the according view control
//has not been operated, yet
->withRange(new Range(0, 2))
->withOrder(new Order('achieve', Order::DESC))

->withRequest($request);

/**
Expand Down
8 changes: 0 additions & 8 deletions components/ILIAS/UI/tests/Component/Table/DataTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ public function testDataTableBasicConstruction(): void
$data = $this->getDataRetrieval();
$cols = ['f0' => $this->getTableFactory()->column()->text("col1")];
$table = $this->getTableFactory()->data('title', $cols, $data);
$this->assertEquals(800, $table->getNumberOfRows());
$this->assertInstanceOf(Order::class, $table->getOrder());
$this->assertInstanceOf(Range::class, $table->getRange());
$this->assertInstanceOf(I\Signal::class, $table->getAsyncActionSignal());
Expand Down Expand Up @@ -154,13 +153,6 @@ public function testDataTableWithRequest(): void
$this->assertEquals($request, $table->withRequest($request)->getRequest());
}

public function testDataTableWithNumberOfRows(): void
{
$table = $this->getTable();
$nor = 12;
$this->assertEquals($nor, $table->withNumberOfRows($nor)->getNumberOfRows());
}

public function testDataTableWithOrder(): void
{
$table = $this->getTable();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ public function testDataTableHasNoOrderingViewControl(): void
array_keys($inputs)
);
$this->assertInstanceOf(
I\Input\ViewControl\NullControl::class,
I\Input\ViewControl\Group::class,
$inputs[C\Table\Data::VIEWCONTROL_KEY_ORDERING]
);
}
Expand Down Expand Up @@ -173,7 +173,7 @@ public function testDataTableHasNoOrderingViewControlDueToRecordCount(): void
array_keys($inputs)
);
$this->assertInstanceOf(
I\Input\ViewControl\NullControl::class,
I\Input\ViewControl\Group::class,
$inputs[C\Table\Data::VIEWCONTROL_KEY_ORDERING]
);
}
Expand Down
Loading