formset woes
This commit is contained in:
parent
d0eaa41bb1
commit
4e374c8d39
128
itf/static/js/jquery.formset.js
Normal file
128
itf/static/js/jquery.formset.js
Normal file
|
@ -0,0 +1,128 @@
|
|||
/**
|
||||
* jQuery Formset 1.1
|
||||
* @author Stanislaus Madueke (stan DOT madueke AT gmail DOT com)
|
||||
* @requires jQuery 1.2.6 or later
|
||||
*
|
||||
* Copyright (c) 2009, Stanislaus Madueke
|
||||
* All rights reserved.
|
||||
*
|
||||
* Licensed under the New BSD License
|
||||
* See: http://www.opensource.org/licenses/bsd-license.php
|
||||
*/
|
||||
(function($) {
|
||||
$.fn.formset = function(opts)
|
||||
{
|
||||
var options = $.extend({}, $.fn.formset.defaults, opts);
|
||||
|
||||
var updateElementIndex = function(el, prefix, ndx) {
|
||||
var id_regex = new RegExp('(' + prefix + '-\\d+)');
|
||||
var replacement = prefix + '-' + ndx;
|
||||
if ($(el).attr("for")) $(el).attr("for", $(el).attr("for").replace(id_regex, replacement));
|
||||
if (el.id) el.id = el.id.replace(id_regex, replacement);
|
||||
if (el.name) el.name = el.name.replace(id_regex, replacement);
|
||||
};
|
||||
|
||||
$(this).each(function(i) {
|
||||
$(this).addClass(options.formCssClass);
|
||||
if ($(this).is('TR')) {
|
||||
// If the forms are laid out in table rows, insert
|
||||
// the remove button into the last table cell:
|
||||
$(this).children(':last').append('<a class="' + options.deleteCssClass +'" href="javascript:void(0)">' + options.deleteText + '</a>');
|
||||
} else if ($(this).is('UL') || $(this).is('OL')) {
|
||||
// If they're laid out as an ordered/unordered list,
|
||||
// insert an <li> after the last list item:
|
||||
$(this).append('<li><a class="' + options.deleteCssClass + '" href="javascript:void(0)">' + options.deleteText +'</a></li>');
|
||||
} else {
|
||||
// Otherwise, just insert the remove button as the
|
||||
// last child element of the form's container:
|
||||
$(this).append('<a class="' + options.deleteCssClass + '" href="javascript:void(0)">' + options.deleteText +'</a>');
|
||||
}
|
||||
$(this).find('a.' + options.deleteCssClass).click(function() {
|
||||
// Remove the parent form containing this button:
|
||||
var row = $(this).parents('.' + options.formCssClass);
|
||||
row.remove();
|
||||
// If a post-delete callback was provided, call it with the deleted form:
|
||||
if (options.removed) options.removed(row);
|
||||
// Update the TOTAL_FORMS form count.
|
||||
// Also, update names and ids for all remaining form controls
|
||||
// so they remain in sequence:
|
||||
var forms = $('.' + options.formCssClass);
|
||||
$('#id_' + options.prefix + '-TOTAL_FORMS').val(forms.length);
|
||||
// If there's only one form left, disable its delete button:
|
||||
if (forms.length == 1) { $('a.' + options.deleteCssClass).hide(); }
|
||||
for (var i=0, formCount=forms.length; i<formCount; i++) {
|
||||
$(forms.get(i)).find('input,select,textarea,label').each(function() {
|
||||
updateElementIndex(this, options.prefix, i);
|
||||
});
|
||||
}
|
||||
return false;
|
||||
});
|
||||
});
|
||||
|
||||
if ($(this).length) {
|
||||
var $addBtn;
|
||||
if ($(this).attr('tagName') == 'TR') {
|
||||
// If forms are laid out as table rows, insert the
|
||||
// "add" button in a new table row:
|
||||
var numCols = this.eq(0).children().length;
|
||||
$(this).parent().append('<tr><td colspan="' + numCols + '"><a class="' + options.addCssClass + '" href="javascript:void(0)">' + options.addText + '</a></tr>');
|
||||
$addBtn = $(this).parent().find('tr:last a');
|
||||
} else {
|
||||
// Otherwise, insert it immediately after the last form:
|
||||
$(this).filter(':last').after('<a class="' + options.addCssClass + '" href="javascript:void(0)">' + options.addText + '</a>');
|
||||
$addBtn = $(this).filter(':last').next();
|
||||
}
|
||||
$addBtn.click(function() {
|
||||
var nextIndex = parseInt($('#id_' + options.prefix + '-TOTAL_FORMS').val());
|
||||
var row = $('.' + options.formCssClass + ' fieldset:first').clone().get(0);
|
||||
$(row).removeAttr('id').insertAfter($('.' + options.formCssClass + ':last'));
|
||||
$(row).find('input,select,textarea,label').each(function() {
|
||||
updateElementIndex(this, options.prefix, nextIndex);
|
||||
// If this is a checkbox or radiobutton, set uncheck it.
|
||||
// Fix for Issue 1, reported by Wilson.Andrew.J:
|
||||
var elem = $(this);
|
||||
if (elem.is('input:checkbox') || elem.is('input:radio')) {
|
||||
elem.attr('checked', false);
|
||||
} else {
|
||||
elem.val('');
|
||||
}
|
||||
if (elem.hasClass("select2class")) {
|
||||
//console.log(elem);
|
||||
elem.attr("data-id", "");
|
||||
elem.attr("data-title", "");
|
||||
$(row).find(".select2-container").remove();
|
||||
|
||||
elem.itfSelect2();
|
||||
//console.log($(row));
|
||||
//$(row).find(".select2-container").remove();
|
||||
//elem.itfSelect2();
|
||||
}
|
||||
});
|
||||
var formCount = nextIndex + 1;
|
||||
$('#id_' + options.prefix + '-TOTAL_FORMS').val(formCount);
|
||||
// If we've got more than one form, enable delete buttons:
|
||||
if (formCount > 1) { $('a.' + options.deleteCssClass).show(); }
|
||||
// If a post-add callback was supplied, call it with the added form:
|
||||
if (options.added) options.added($(row));
|
||||
return false;
|
||||
});
|
||||
}
|
||||
|
||||
// If the formset is initialized with a single form, hide the delete button:
|
||||
if ($(this).length == 1) { $('a.' + options.deleteCssClass).hide(); }
|
||||
|
||||
return $(this);
|
||||
}
|
||||
|
||||
/* Setup plugin defaults */
|
||||
$.fn.formset.defaults = {
|
||||
prefix: 'form', // The form prefix for your django formset
|
||||
addText: 'add another', // Text for the add link
|
||||
deleteText: 'remove', // Text for the delete link
|
||||
addCssClass: 'add-row', // CSS class applied to the add link
|
||||
deleteCssClass: 'delete-row', // CSS class applied to the delete link
|
||||
formCssClass: 'dynamic-form', // CSS class applied to each form in a formset
|
||||
added: null, // Function called each time a new form is added
|
||||
removed: null // Function called each time a form is deleted
|
||||
}
|
||||
})(jQuery)
|
|
@ -17,32 +17,21 @@
|
|||
.select2-input {
|
||||
width: 200px !important;
|
||||
}
|
||||
|
||||
.delete-row {
|
||||
display:none;
|
||||
}
|
||||
</style>
|
||||
<!-- uni-form JS library, optional -->
|
||||
<script src="/static/uni_form/uni-form.jquery.js" type="text/javascript"></script>
|
||||
<script src="/static/js/select2.js" type="text/javascript"></script>
|
||||
<script src="/static/js/jquery.formset.min.js"></script>
|
||||
<script src="/static/js/jquery.formset.js"></script>
|
||||
|
||||
|
||||
<script type="text/javascript">
|
||||
$(function(){
|
||||
$('form.uniForm').uniform();
|
||||
/*
|
||||
$('select').select2({
|
||||
|
||||
});
|
||||
*/
|
||||
/*
|
||||
sampleData = [
|
||||
{
|
||||
'id': 'foo',
|
||||
'text': 'some text',
|
||||
'first_name': 'some',
|
||||
'last_name': 'test'
|
||||
}
|
||||
];
|
||||
*/
|
||||
$('.select2class').each(function() {
|
||||
|
||||
$.fn.itfSelect2 = function() {
|
||||
return this.each(function() {
|
||||
var $this = $(this);
|
||||
var ctype_id = $this.attr("data-ctype");
|
||||
var url = '/autocomplete/' + ctype_id;
|
||||
|
@ -82,8 +71,31 @@ $(function(){
|
|||
var title = $this.attr("data-title");
|
||||
$this.select2("val", {'id': id, 'title': title});
|
||||
}
|
||||
//return this;
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
$(function(){
|
||||
$('form.uniForm').uniform();
|
||||
/*
|
||||
$('select').select2({
|
||||
|
||||
});
|
||||
*/
|
||||
/*
|
||||
sampleData = [
|
||||
{
|
||||
'id': 'foo',
|
||||
'text': 'some text',
|
||||
'first_name': 'some',
|
||||
'last_name': 'test'
|
||||
}
|
||||
];
|
||||
*/
|
||||
|
||||
|
||||
$('.select2class').itfSelect2();
|
||||
|
||||
/*
|
||||
$('.select2class').select2({
|
||||
|
|
Loading…
Reference in New Issue
Block a user