This repository has been archived by the owner on Jan 7, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #35 from antoniojunyor/master
Creating a method to multiply form
- Loading branch information
Showing
5 changed files
with
131 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
Locastyle.prototype.duplicateForm = (function() { | ||
'use strict'; | ||
|
||
function init() { | ||
multiplyForm(); | ||
verifyFormNumbers(); | ||
addValueToDataForm(); | ||
removeForm(); | ||
} | ||
|
||
var config = { | ||
selectors: { | ||
form: '[data-form]', | ||
parent: '[data-form-parent]' | ||
}, | ||
classes: { | ||
hide: 'dNone' | ||
}, | ||
actions: { | ||
duplicate: '[data-duplicate-form]', | ||
remove: '[data-remove-form]' | ||
} | ||
}; | ||
|
||
function multiplyForm() { | ||
$(config.actions.duplicate).on('click', function(el) { | ||
el.preventDefault(); | ||
var cloneLast = $(config.selectors.form).last().clone(); | ||
cloneLast.appendTo(config.selectors.parent); | ||
|
||
addValueToDataForm(); | ||
verifyFormNumbers(); | ||
removeForm(); | ||
}); | ||
} | ||
|
||
function addValueToDataForm() { | ||
$(config.selectors.form).each(function(i, el) { | ||
$(el).attr('data-form', i); | ||
}); | ||
} | ||
|
||
function verifyFormNumbers() { | ||
if ($(config.selectors.form).length > 1) { | ||
$(config.actions.remove).removeClass(config.classes.hide); | ||
} else { | ||
$(config.actions.remove).addClass(config.classes.hide); | ||
} | ||
} | ||
|
||
function removeForm() { | ||
$(config.actions.remove).on('click', function(el) { | ||
el.preventDefault(); | ||
if($(config.selectors.form).length > 1) { | ||
$(this).parents().eq(1).remove(); | ||
} | ||
verifyFormNumbers(); | ||
}); | ||
} | ||
|
||
return { | ||
init: init | ||
}; | ||
|
||
}()); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
--- | ||
title: Formulários - Multiplicando Campos | ||
menu_page: sidebar-formularios.erb | ||
--- | ||
|
||
<article class="text"> | ||
<header> | ||
<h1>Multiplicando Formulários</h1> | ||
<h3>Como multiplicar campos dentro de um formulários</h3> | ||
</header> | ||
|
||
<div class="example"> | ||
<form action="#" class="form"> | ||
<div class="row"> | ||
<div data-form-parent class="dInlineBlock"> | ||
<div class="dInlineBlock" data-form> | ||
<div class="control-group span3"> | ||
<label for="type" class="dNone">Select</label> | ||
<select name="" id=""> | ||
<option value="1">Opção 1</option> | ||
<option value="1">Opção 2</option> | ||
</select> | ||
</div> | ||
<div class="control-group span6"> | ||
<label for="" class="dNone">Campo</label> | ||
<input type="text" class="span6" placeholder="Campo"> | ||
</div> | ||
<div class="control-group span2"> | ||
<a href="#" class="btn ico-hifen dNone" data-remove-form></a> | ||
</div> | ||
</div> | ||
</div> | ||
</div> | ||
<a href="#" class="btn" data-duplicate-form>Adicionar</a> | ||
</form> | ||
</div> | ||
|
||
<pre class="lang-html prettyprint linenums"> | ||
<form action="#" class="form"> | ||
<div class="row"> | ||
<div data-form-parent class="dInlineBlock"> | ||
<div class="dInlineBlock" data-form> | ||
<div class="control-group span3"> | ||
<label for="type" class="dNone">Select</label> | ||
<select> | ||
<option value="1">Opção 1</option> | ||
<option value="1">Opção 2</option> | ||
</select> | ||
</div> | ||
<div class="control-group span6"> | ||
<label class="dNone">Campo</label> | ||
<input type="text" class="span6" placeholder="Campo"> | ||
</div> | ||
<div class="control-group span2"> | ||
<a href="#" class="btn ico-hifen dNone" data-remove-form></a> | ||
</div> | ||
</div> | ||
</div> | ||
</div> | ||
<a href="#" class="btn" data-duplicate-form>Adicionar</a> | ||
</form> | ||
</pre> | ||
</article> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters