Skip to content

Commit bf7c580

Browse files
ENGCOM-5310: [Ui] Allow to define listing configuration via ui component xml #23307
- Merge Pull Request #23307 from magenius-team/magento2:bug/component-listing-page-size-xml-config - Merged commits: 1. b26a8f8
2 parents ef49ac6 + b26a8f8 commit bf7c580

File tree

6 files changed

+92
-104
lines changed

6 files changed

+92
-104
lines changed

app/code/Magento/ConfigurableProduct/view/adminhtml/web/js/variations/paging/sizes.js

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,21 +9,21 @@ define([
99

1010
return Sizes.extend({
1111
defaults: {
12-
excludedOptions: ['100', '200']
13-
},
14-
15-
/**
16-
* @override
17-
*/
18-
initialize: function () {
19-
this._super();
20-
21-
this.excludedOptions.forEach(function (excludedOption) {
22-
delete this.options[excludedOption];
23-
}, this);
24-
this.updateArray();
25-
26-
return this;
12+
options: {
13+
'20': {
14+
value: 20,
15+
label: 20
16+
},
17+
'30': {
18+
value: 30,
19+
label: 30
20+
},
21+
'50': {
22+
value: 50,
23+
label: 50
24+
}
25+
},
26+
value: 20
2727
}
2828
});
2929
});

app/code/Magento/Ui/Component/Paging.php

Lines changed: 61 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -3,46 +3,61 @@
33
* Copyright © Magento, Inc. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6+
67
namespace Magento\Ui\Component;
78

89
/**
10+
* Class Paging
11+
*
912
* @api
1013
* @since 100.0.2
1114
*/
1215
class Paging extends AbstractComponent
1316
{
1417
const NAME = 'paging';
1518

19+
/**
20+
* Default paging options
21+
*
22+
* @var array
23+
*/
24+
private $defaultOptions = [
25+
'20' => [
26+
'value' => 20,
27+
'label' => 20
28+
],
29+
'30' => [
30+
'value' => 30,
31+
'label' => 30
32+
],
33+
'50' => [
34+
'value' => 50,
35+
'label' => 50
36+
],
37+
'100' => [
38+
'value' => 100,
39+
'label' => 100
40+
],
41+
'200' => [
42+
'value' => 200,
43+
'label' => 200
44+
],
45+
];
46+
47+
/**
48+
* Default page size
49+
*
50+
* @var int
51+
*/
52+
private $defaultPageSize = 20;
53+
1654
/**
1755
* Default component data
1856
*
1957
* @var array
2058
*/
2159
protected $_data = [
2260
'config' => [
23-
'options' => [
24-
'20' => [
25-
'value' => 20,
26-
'label' => 20
27-
],
28-
'30' => [
29-
'value' => 30,
30-
'label' => 30
31-
],
32-
'50' => [
33-
'value' => 50,
34-
'label' => 50
35-
],
36-
'100' => [
37-
'value' => 100,
38-
'label' => 100
39-
],
40-
'200' => [
41-
'value' => 200,
42-
'label' => 200
43-
],
44-
],
45-
'pageSize' => 20,
4661
'current' => 1
4762
]
4863
];
@@ -65,13 +80,13 @@ public function getComponentName()
6580
public function prepare()
6681
{
6782
$this->prepareOptions();
83+
$this->preparePageSize();
6884
$paging = $this->getContext()->getRequestParam('paging');
6985
if (!isset($paging['notLimits'])) {
7086
$this->getContext()
7187
->getDataProvider()
7288
->setLimit($this->getOffset($paging), $this->getSize($paging));
7389
}
74-
7590
parent::prepare();
7691
}
7792

@@ -83,12 +98,26 @@ public function prepare()
8398
protected function prepareOptions()
8499
{
85100
$config = $this->getData('config');
86-
if (isset($config['options'])) {
87-
$config['options'] = array_values($config['options']);
88-
foreach ($config['options'] as &$item) {
89-
$item['value'] = (int) $item['value'];
90-
}
91-
unset($item);
101+
if (!isset($config['options'])) {
102+
$config['options'] = $this->defaultOptions;
103+
}
104+
foreach ($config['options'] as &$item) {
105+
$item['value'] = (int)$item['value'];
106+
}
107+
unset($item);
108+
$this->setData('config', $config);
109+
}
110+
111+
/**
112+
* Prepare page size
113+
*
114+
* @return void
115+
*/
116+
private function preparePageSize()
117+
{
118+
$config = $this->getData('config');
119+
if (!isset($config['pageSize'])) {
120+
$config['pageSize'] = $this->defaultPageSize;
92121
$this->setData('config', $config);
93122
}
94123
}
@@ -102,7 +131,7 @@ protected function prepareOptions()
102131
protected function getOffset($paging)
103132
{
104133
$defaultPage = $this->getData('config/current') ?: 1;
105-
return (int) (isset($paging['current']) ? $paging['current'] : $defaultPage);
134+
return (int)(isset($paging['current']) ? $paging['current'] : $defaultPage);
106135
}
107136

108137
/**
@@ -113,7 +142,6 @@ protected function getOffset($paging)
113142
*/
114143
protected function getSize($paging)
115144
{
116-
$defaultLimit = $this->getData('config/pageSize') ?: 20;
117-
return (int) (isset($paging['pageSize']) ? $paging['pageSize'] : $defaultLimit);
145+
return (int)(isset($paging['pageSize']) ? $paging['pageSize'] : $this->getData('config/pageSize'));
118146
}
119147
}

app/code/Magento/Ui/Test/Unit/Component/PagingTest.php

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -81,31 +81,11 @@ public function testPrepare()
8181
],
8282
'config' => [
8383
'options' => [
84-
[
85-
'value' => 20,
86-
'label' => 20
87-
],
88-
[
89-
'value' => 30,
90-
'label' => 30
91-
],
92-
[
93-
'value' => 50,
94-
'label' => 50
95-
],
96-
[
97-
'value' => 100,
98-
'label' => 100
99-
],
100-
[
101-
'value' => 200,
102-
'label' => 200
103-
],
104-
[
84+
'options1' => [
10585
'value' => 20,
10686
'label' => 'options1'
10787
],
108-
[
88+
'options2' => [
10989
'value' => 40,
11090
'label' => 'options2'
11191
],

app/code/Magento/Ui/view/base/web/js/grid/paging/paging.js

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ define([
2020
template: 'ui/grid/paging/paging',
2121
totalTmpl: 'ui/grid/paging-total',
2222
totalRecords: 0,
23-
pageSize: 20,
2423
pages: 1,
2524
current: 1,
2625
selectProvider: 'ns = ${ $.ns }, index = ids',
@@ -35,7 +34,6 @@ define([
3534
},
3635

3736
imports: {
38-
pageSize: '${ $.sizesConfig.name }:value',
3937
totalSelected: '${ $.selectProvider }:totalSelected',
4038
totalRecords: '${ $.provider }:data.totalRecords',
4139
filters: '${ $.provider }:params.filters'
@@ -46,6 +44,11 @@ define([
4644
current: '${ $.provider }:params.paging.current'
4745
},
4846

47+
links: {
48+
options: '${ $.sizesConfig.name }:options',
49+
pageSize: '${ $.sizesConfig.name }:value'
50+
},
51+
4952
statefull: {
5053
pageSize: true,
5154
current: true
@@ -231,10 +234,10 @@ define([
231234
* previous and current page size values.
232235
*/
233236
updateCursor: function () {
234-
var cursor = this.current - 1,
235-
size = this.pageSize,
237+
var cursor = this.current - 1,
238+
size = this.pageSize,
236239
oldSize = _.isUndefined(this.previousSize) ? this.pageSize : this.previousSize,
237-
delta = cursor * (oldSize - size) / size;
240+
delta = cursor * (oldSize - size) / size;
238241

239242
delta = size > oldSize ?
240243
Math.ceil(delta) :

app/code/Magento/Ui/view/base/web/js/grid/paging/sizes.js

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -17,31 +17,8 @@ define([
1717
return Element.extend({
1818
defaults: {
1919
template: 'ui/grid/paging/sizes',
20-
value: 20,
2120
minSize: 1,
2221
maxSize: 999,
23-
options: {
24-
'20': {
25-
value: 20,
26-
label: 20
27-
},
28-
'30': {
29-
value: 30,
30-
label: 30
31-
},
32-
'50': {
33-
value: 50,
34-
label: 50
35-
},
36-
'100': {
37-
value: 100,
38-
label: 100
39-
},
40-
'200': {
41-
value: 200,
42-
label: 200
43-
}
44-
},
4522
statefull: {
4623
options: true,
4724
value: true

lib/internal/Magento/Framework/View/Layout/Generator/UiComponent.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
* Copyright © Magento, Inc. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6+
67
namespace Magento\Framework\View\Layout\Generator;
78

89
use Magento\Framework\View\Element\BlockFactory;
@@ -65,7 +66,7 @@ public function __construct(
6566
}
6667

6768
/**
68-
* {@inheritdoc}
69+
* @inheritdoc
6970
*/
7071
public function getType()
7172
{
@@ -90,18 +91,17 @@ public function process(ReaderContext $readerContext, GeneratorContext $generato
9091
$layout = $generatorContext->getLayout();
9192

9293
// Instantiate blocks and collect all actions data
93-
/** @var $blocks \Magento\Framework\View\Element\AbstractBlock[] */
94-
$blocks = [];
9594
foreach ($scheduledElements as $elementName => $element) {
9695
list($elementType, $data) = $element;
9796

9897
if ($elementType !== Element::TYPE_UI_COMPONENT) {
9998
continue;
10099
}
101100

102-
$block = $this->generateComponent($structure, $elementName, $data, $layout);
103-
$blocks[$elementName] = $block;
104-
$layout->setBlock($elementName, $block);
101+
$layout->setBlock(
102+
$elementName,
103+
$this->generateComponent($structure, $elementName, $data, $layout)
104+
);
105105
$scheduledStructure->unsetElement($elementName);
106106
}
107107

0 commit comments

Comments
 (0)