Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
array.phtml
Go to the documentation of this file.
1 <?php
7 // @codingStandardsIgnoreFile
8 
9 ?>
10 
11 <?php
12 $_htmlId = $block->getHtmlId() ? $block->getHtmlId() : '_' . uniqid();
13 $_colspan = $block->isAddAfter() ? 2 : 1;
14 ?>
15 
16 <div class="design_theme_ua_regexp" id="grid<?= /* @escapeNotVerified */ $_htmlId ?>">
17  <div class="admin__control-table-wrapper">
18  <table class="admin__control-table" id="<?= /* @escapeNotVerified */ $block->getElement()->getId() ?>">
19  <thead>
20  <tr>
21  <?php foreach ($block->getColumns() as $columnName => $column): ?>
22  <th><?= /* @escapeNotVerified */ $column['label'] ?></th>
23  <?php endforeach;?>
24  <th class="col-actions" colspan="<?= /* @escapeNotVerified */ $_colspan ?>"><?= /* @escapeNotVerified */ __('Action') ?></th>
25  </tr>
26  </thead>
27  <tfoot>
28  <tr>
29  <td colspan="<?= count($block->getColumns())+$_colspan ?>" class="col-actions-add">
30  <button id="addToEndBtn<?= /* @escapeNotVerified */ $_htmlId ?>" class="action-add" title="<?= /* @escapeNotVerified */ __('Add') ?>" type="button">
31  <span><?= /* @escapeNotVerified */ $block->getAddButtonLabel() ?></span>
32  </button>
33  </td>
34  </tr>
35  </tfoot>
36  <tbody id="addRow<?= /* @escapeNotVerified */ $_htmlId ?>"></tbody>
37  </table>
38  </div>
39  <input type="hidden" name="<?= /* @escapeNotVerified */ $block->getElement()->getName() ?>[__empty]" value="" />
40 
41  <script>
42  require([
43  'mage/template',
44  'prototype'
45  ], function (mageTemplate) {
46  // create row creator
47  window.arrayRow<?= /* @escapeNotVerified */ $_htmlId ?> = {
48 
49  // define row prototypeJS template
50  template: mageTemplate(
51  '<tr id="<%- _id %>">'
52  <?php foreach ($block->getColumns() as $columnName => $column): ?>
53  + '<td>'
54  + '<?= /* @escapeNotVerified */ $block->renderCellTemplate($columnName) ?>'
55  + '<\/td>'
56  <?php endforeach; ?>
57 
58  <?php if ($block->isAddAfter()): ?>
59  + '<td><button class="action-add" type="button" id="addAfterBtn<%- _id %>"><span><?= /* @escapeNotVerified */ __('Add after') ?><\/span><\/button><\/td>'
60  <?php endif; ?>
61 
62  + '<td class="col-actions"><button onclick="arrayRow<?= /* @escapeNotVerified */ $_htmlId ?>.del(\'<%- _id %>\')" class="action-delete" type="button"><span><?= /* @escapeNotVerified */ __('Delete') ?><\/span><\/button><\/td>'
63  +'<\/tr>'
64  ),
65 
66  add: function(rowData, insertAfterId) {
67  // generate default template data
68  var templateValues;
69 
70  // Prepare template values
71  if (rowData) {
72  templateValues = rowData;
73  } else {
74  var d = new Date();
75  templateValues = {
76  <?php foreach ($block->getColumns() as $columnName => $column): ?>
77  <?= /* @escapeNotVerified */ $columnName ?>: '',
78  'option_extra_attrs': {},
79  <?php endforeach; ?>
80  _id: '_' + d.getTime() + '_' + d.getMilliseconds()
81  };
82  }
83 
84  // Insert new row after specified row or at the bottom
85  if (insertAfterId) {
86  Element.insert($(insertAfterId), {after: this.template(templateValues)});
87  } else {
88  Element.insert($('addRow<?= /* @escapeNotVerified */ $_htmlId ?>'), {bottom: this.template(templateValues)});
89  }
90 
91  // Fill controls with data
92  if (rowData) {
93  var rowInputElementNames = Object.keys(rowData.column_values);
94  for (var i = 0; i < rowInputElementNames.length; i++) {
95  if ($(rowInputElementNames[i])) {
96  $(rowInputElementNames[i]).setValue(rowData.column_values[rowInputElementNames[i]]);
97  }
98  }
99  }
100 
101  // Add event for {addAfterBtn} button
102  <?php if ($block->isAddAfter()): ?>
103  Event.observe('addAfterBtn' + templateValues._id, 'click', this.add.bind(this, false, templateValues._id));
104  <?php endif; ?>
105  },
106 
107  del: function(rowId) {
108  $(rowId).remove();
109  }
110  }
111 
112  // bind add action to "Add" button in last row
113  Event.observe('addToEndBtn<?= /* @escapeNotVerified */ $_htmlId ?>', 'click', arrayRow<?= /* @escapeNotVerified */ $_htmlId ?>.add.bind(arrayRow<?= /* @escapeNotVerified */ $_htmlId ?>, false, false));
114 
115  // add existing rows
116  <?php
117  foreach ($block->getArrayRows() as $_rowId => $_row) {
118  /* @escapeNotVerified */ echo "arrayRow{$_htmlId}.add(" . $_row->toJson() . ");\n";
119  }
120  ?>
121 
122  // Toggle the grid availability, if element is disabled (depending on scope)
123  <?php if ($block->getElement()->getDisabled()):?>
124  toggleValueElements({checked: true}, $('grid<?= /* @escapeNotVerified */ $_htmlId ?>').parentNode);
125  <?php endif;?>
126  });
127  </script>
128 </div>
$block setTitle( 'CMS Block Title') -> setIdentifier('fixture_block') ->setContent('< h1 >Fixture Block Title</h1 >< a href=" store url</a><p> Config value
Definition: block.php:9
endif
Definition: array.phtml:104
$_colspan
Definition: array.phtml:13
__()
Definition: __.php:13
$block
Definition: block.php:8
foreach($block->getColumns() as $columnName=> $column) foreach($block->getColumns() as $columnName=> $column) if($block->isAddAfter()) endforeach
Definition: array.phtml:76
$_htmlId
Definition: array.phtml:12