Class Atk4\Ui\Form\Control\Multiline

extends Control

Creates a Multiline field within a table, which allows adding/editing multiple data rows.

Using hasMany reference will required to save reference data using Multiline::saveRows() method.

$form = Form::addTo($app); $form->setModel($invoice, []);

// add Multiline form control and set model for Invoice items $ml = $form->addControl('ml', [Multiline::class]); $ml->setReferenceModel('Items', null, ['item', 'cat', 'qty', 'price', 'total']);

$form->onSubmit(function (Form $form) use ($ml) { // save Form model and then Multiline model $form->model->save(); // saving invoice record $ml->saveRows(); // saving invoice items record related to invoice return new JsToast('Saved!'); });

If Multiline's model contains expressions, these will be evaluated on the fly whenever data gets entered.

Multiline input also has an onChange callback that will return all data rows in an array. It is also possible to fire onChange handler only for certain fields by passing them as an array to the method.

Note that deleting a row will always fire the onChange callback.

You can use the returned data to update other related areas of the form. For example, updating Grand Total field of all invoice items.

$ml->onChange(function (array $rows) use ($form) { $grandTotal = 0; foreach ($rows as $row => $cols) { foreach ($cols as $col) { $fieldName = array_key_first($col); if ($fieldName === 'total') { $grandTotal += $col[$fieldName]; } } }

return $form->js(false, null, 'input[name="grand_total"]')->val($app->uiPersistence->typecastSaveField(new Field(['type' => 'atk4_money']), $grandTotal)); }, ['qty', 'price']);

Finally, it's also possible to use Multiline for quickly adding records to a model. Be aware that in the example below all User records will be displayed. If your model contains a lot of records, you should handle their limit somehow.

$form = Form::addTo($app); $ml = $form->addControl('ml', [Form\Control\Multiline::class]); $ml->setModel($user, ['name', 'is_vip']);

$form->onSubmit(function (Form $form) use ($ml) { $ml->saveRows(); return new JsToast('Saved!'); });

Methods
Methods inherited from Atk4\Ui\Form\Control
set(), renderTemplateToHtml(), onChange(), jsInput()
Methods inherited from Atk4\Ui\View
__construct(), setSource(), setMissingProperty(), setElement(), link(), getExecutorFactory(), add(), getClosestOwner(), addClass(), removeClass(), setStyle(), removeStyle(), setAttr(), removeAttr(), url(), jsUrl(), _getStickyArgs(), stickyGet(), recursiveRender(), renderAll(), render(), renderToTab(), renderToJsonArr(), getHtml(), js(), vue(), jsEmitEvent(), jsGetStoreData(), jsClearStoreData(), jsAddStoreData(), jsReload(), on(), getHtmlId(), getJsRenderActions(), getJs()
Methods inherited from Atk4\Ui\AbstractView
getOwner()
Methods used from Atk4\Core\AppScopeTrait
assertInstanceOfApp(), issetApp(), getApp(), setApp()
Methods used from Atk4\Core\ContainerTrait
_uniqueElementName(), _addContainer(), removeElement(), _shorten(), getElement(), hasElement()
Methods used from Atk4\Core\InitializerTrait
isInitialized(), assertIsInitialized(), invokeInit()
Methods used from Atk4\Core\StaticAddToTrait
_addToAdd(), addTo(), addToWithCl(), addToWithClUnsafe()
Methods used from Atk4\Core\TrackableTrait
issetOwner(), setOwner(), unsetOwner(), getDesiredName(), destroy()
Constants
Properties
Properties inherited from Atk4\Ui\Form\Control
$form, $entityField, $controlClass, $layoutWrap, $renderLabel, $width, $caption, $hint, $disabled, $readOnly
Properties inherited from Atk4\Ui\View
$_jsActions, $model, $region, $ui, $class, $style, $attr, $template, $defaultTemplate, $content, $element, $executorFactory, $stickyArgs
Properties inherited from Atk4\Ui\AbstractView
$_addLater, $_rendered, $elements
Properties used from Atk4\Core\AppScopeTrait
$_app, $maxNameLength, $uniqueNameHashes
Properties used from Atk4\Core\ContainerTrait
$_elementNameCounts
Properties used from Atk4\Core\InitializerTrait
$_initialized
Properties used from Atk4\Core\NameTrait
$name
Properties used from Atk4\Core\TrackableTrait
$_owner, $shortName