Skip to content

Instantly share code, notes, and snippets.

@mdmunir
Created November 18, 2017 08:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mdmunir/7897e5c8eeed280fc1cb36b904763fcc to your computer and use it in GitHub Desktop.
Save mdmunir/7897e5c8eeed280fc1cb36b904763fcc to your computer and use it in GitHub Desktop.
function prependRow($e) {
var settings = $e.data('mdmTabularInput').settings;
var counter = settings.counter++;
var template = replace(settings.template, settings.replaces, counter);
var $row = $(template);
var $element = settings.container ? $e.find(settings.container) : $e;
var event = $.Event('beforeAdd');
$e.trigger(event, [$row]);
if (event.result !== false) {
$element.prepend($row);
$e.trigger('afterAdd', [$row]);
// add js
if (settings.templateJs) {
var js = replace(settings.templateJs, settings.replaces, counter);
eval(js);
}
// validation for active form
if (settings.formSelector && settings.validations && settings.validations.length) {
var $form = $(settings.formSelector);
var validations = $.extend(true, {}, settings.validations);
$.each(validations, function () {
var validation = this;
$.each(['id', 'name', 'container', 'input'], function () {
validation[this] = replace(validation[this], settings.replaces, counter);
});
$form.yiiActiveForm('add', validation);
});
}
$e.mdmTabularInput('rearrage');
}
return $row;
}
$('button').click(function () {
prependRow($('#detail-grid'));
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment