wbraganca / yii2-dynamicform

It is widget to yii2 framework to clone form elements in a nested manner, maintaining accessibility.
Other
436 stars 438 forks source link

class DynamicFormWidget (add a click handler for the clone button) #269

Open xr0m3oz opened 5 years ago

xr0m3oz commented 5 years ago

If you make the nesting structure of one to another. That is 2 buttons to add a line. When you click on the second button. Added 2 lines instead of 1st. Click triggered 2 times in a row.

But it works fine when you click on the first add button. And it adds 1 line as well.

//need add
$js .= "    e.stopImmediatePropagation();\n";

//to
         // add a click handler for the clone button
        $js = 'jQuery("#' . $this->formId . '").on("click", "' . $this->insertButton . '", function(e) {'. "\n";
        $js .= "    e.preventDefault();\n";
        $js .= "    e.stopImmediatePropagation();\n"; // ITS!
        $js .= '    jQuery(".' .  $this->widgetContainer . '").triggerHandler("beforeInsert", [jQuery(this)]);' . "\n";
        $js .= '    jQuery(".' .  $this->widgetContainer . '").yiiDynamicForm("addItem", '. $this->_hashVar . ", e, jQuery(this));\n";
        $js .= "});\n";
        $view->registerJs($js, $view::POS_READY);

widget