/** * Manipulate this object to add the deleteRow-button needed for dynamicLists * and perform other needed modifications. * * @return void */ public function performDynamicListModifications() { // Add deleteRow-button. $button = new Button('removeRow'); $button->context('danger')->title(trans('Nicat-HtmlBuilder::htmlbuilder.dynamicListRemoveButtonTitle'))->data('dynamiclist-remove', true)->content('<i class="fa fa-remove"></i>'); $inputGroupButton = new DivInputGroupButton($button); $this->prependChild($inputGroupButton); // Add class for bottom-margin. $this->addClass('m-b-1'); }
public function dynamicList($arrayName = '', DynamicListTemplateContract $template, $addButtonLabel = '', $minItems = null, $maxItems = null) { $this->dynamicListArrayName = $arrayName; $this->dynamicListTemplate = $template; // Get the submitted base-array. $submittedArray = app()['formbuilder']->getSubmittedValueForField($this->dynamicListArrayName); // If $minItems or $maxItems was not set via arguments, we try to get them from the FormBuilder. if (is_null($minItems) || is_null($maxItems)) { // Get the array-rules from the formbuilder. $arrayRules = FormBuilderTools::explodeRules(app()['formbuilder']->getRulesForField($this->dynamicListArrayName)); // Set minimum count of items from the gathered rules, or use default value. if (is_null($minItems) && isset($arrayRules['min'][0])) { $minItems = $arrayRules['min'][0]; } else { $minItems = 1; } // Set maximum count of items from the gathered rules, or use default value. if (is_null($minItems) && isset($arrayRules['max'][0])) { $maxItems = $arrayRules['max'][0]; } else { $maxItems = 10; } } // Set some basic stuff at the template. $this->dynamicListTemplate->isDynamicListTemplate = true; $this->dynamicListTemplate->performDynamicListModifications(); $this->dynamicListTemplate->wrap(false); if (method_exists($this, 'labelMode')) { $this->dynamicListTemplate->labelMode('sr-only'); } $this->dynamicListTemplate->data('dynamiclist-group', $this->dynamicListArrayName); // If data was submitted for this array, we have to add each submitted child using the same key it was submitted with. if (is_array($submittedArray) && count($submittedArray) > 0) { foreach ($submittedArray as $submittedChildKey => $submittedChild) { $this->addDynamicListChild($submittedChildKey); } } else { if (app('formbuilder')->getDefaultValueForField($this->dynamicListArrayName)) { foreach (app('formbuilder')->getDefaultValueForField($this->dynamicListArrayName) as $itemKey => $item) { $this->addDynamicListChild($itemKey); } } else { $i = 0; while ($i < $minItems) { $this->addDynamicListChild($i); $i++; } } } // We must also add an empty template to be available for the javascript-functionality. $template = $this->dynamicListTemplate; $template->hidden(); $template->data('dynamiclist-template', true); $this->addDynamicListChild('%itemID%', true); // Then we add the add-row button. if (!(strlen($addButtonLabel) > 0)) { $addButtonLabel = trans('Nicat-HtmlBuilder::htmlbuilder.dynamicListAddButtonTitle'); } $button = new Button('addRow'); $button->context('success')->addClass('btn-sm')->title($addButtonLabel)->data('dynamiclist-add', true)->data('dynamiclist-group', $this->dynamicListArrayName)->data('dynamiclist-min', $minItems)->data('dynamiclist-max', $maxItems)->content('<i class="fa fa-plus"></i> ' . $addButtonLabel); $this->appendChild($button); // We add the alert for maximum-reached. $alert = new DivAlertInfo(); $alert->hidden()->content(trans('Nicat-HtmlBuilder::htmlbuilder.dynamicListMaximumReached'))->data('dynamiclist-maxalert', true)->data('dynamiclist-group', $this->dynamicListArrayName); $this->appendChild($alert); return $this; }