Login

Dynamical formsets

Author:
I159
Posted:
November 16, 2011
Language:
JavaScript
Version:
Not specified
Tags:
formset
Score:
-1 (after 1 ratings)

If you need dynamical formsets you can use {{ empty_form }} in template and even makeup it.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
TEMPLATE_MAKEUP['language'] = '<tr>\n'+
                      				'<td class="labels">\n'+
                      					'<label for="">\n'+
                      					'{{ language_form.empty_form.language|escapejs }}{{ language_form.empty_form.language.errors|escapejs }}:'+
                      					'</label>\n'+
                      				'</td>\n'+
                      				'<td class="fields">\n'+
                        				'<table cellpadding="0" cellspacing="0" border="0" class="sub_tables languages">\n'+
                        				'{{ language_form.empty_form.level|escapejs }}{{ language_form.empty_form.level.errors|escapejs }}'+
                        				'</table>\n'+
                      				'</td>\n'+
                    			'</tr>\n';


TEMPLATE_MAKEUP['education'] = '<tr class="removeble_label">\n'+
                                     '<td class="line_break">&nbsp</td>\n'+
                                    '</tr>\n'+
                                    '<tr class="removeble_label">\n'+
                                     '<td class="labels">\n'+
                      	               '<label for="">\n'+
                      		             '<b>\n'+
                      			           'Второе образование:'+
                      		             '</b>\n'+
                      	               '</label>\n'+
                                     '</td>\n'+
                                   '</tr>\n'+
                                   '<tr>\n'+
                                     '<td class="labels"><label for="">Учебное заведение:</label></td>\n'+
                                     '<td class="fields">\n'+
                      	             '{{ education_form.empty_form.institution|escapejs }}{{ education_form.empty_form.institution.errors|escapejs }}'+
                                     '</td>\n'+
                                   '</tr>\n'+
                                   '<tr>\n'+
                                     '<td class="labels"><label for="">Факультет:</label></td>\n'+
                                     '<td class="fields">\n'+
                                     '{{ education_form.empty_form.faculty|escapejs }}{{ education_form.empty_form.faculty.errors|escapejs }}'+
                                     '</td>\n'+
                                   '</tr>\n'+
                                   '<tr>\n'+
                                     '<td class="labels"><label for="">Специальность:</label></td>\n'+
                                     '<td class="fields">\n'+
                                     '{{ education_form.empty_form.specialty|escapejs }}{{ education_form.empty_form.specialty.errors|escapejs }}'+
                                     '</td>\n'+
                                   '</tr>\n'+
                                   '<tr>\n'+
                                     '<td class="labels"><label for="">Квалификация:</label></td>\n'+
                                     '<td class="fields">\n'+
                                     '{{ education_form.empty_form.qualification|escapejs }}{{ education_form.empty_form.qualification.errors|escapejs }}'+
                                     '</td>\n'+
                                   '</tr>\n'+
                                   '<tr>\n'+
                                     '<td class="labels"><label for="">Год оконцания:</label></td>\n'+
                                     '<td class="fields">\n'+
                                     '{{ education_form.empty_form.graduation_year|escapejs }}{{ education_form.empty_form.graduation_year.errors|escapejs }}'+
                                     '</td>\n'+
                                   '</tr>';  


/* Without deletion.*/ 
function addForm(form_count, form_name, temp_form){    	
        var button = $('#' + form_name + ' .add_form_button');               
        button.click(function() {
            var form = temp_form.replace(/__prefix__/g, form_count);
            form_count++;
            button.closest('tr').before(form);
            $('#id_form-TOTAL_FORMS').val(form_count);
            $('#id_form-MAX_NUM_FORMS').val(form_count);
        });
    };


/* With deletion */
function addDelEducation(){
        var checked = $('.education_number:checked');       
        var form = TEMPLATE_MAKEUP['education'].replace(/__prefix__/g, 1);
        if(checked.val() == 2){
            $('#education_form').append(form);
            $('#id_education-TOTAL_FORMS').val(2);
        }else if(checked.val() == 1){
            $('#id_education-TOTAL_FORMS').val(1);
            $('.removeble_label').remove();
            var inputs = $('#education_form input:text');
            for(var i=0; i<inputs.length; i++){
                if($(inputs[i]).attr('id').indexOf('1') != -1){                    
                    $(inputs[i]).closest('tr').remove();                    
                };
            };
        };
    };

More like this

  1. Form and FormSet that are defined dynamically with request-data by Lacour 5 years, 1 month ago
  2. Dynamically alter the attributes of a formset by cronosa 4 years, 9 months ago
  3. Dynamically adding forms to a formset with jQuery by elo80ka 5 years, 11 months ago
  4. Complex Formsets by smagala 6 years, 1 month ago
  5. Arbitrary length formset by Rupe 5 years, 6 months ago

Comments

Please login first before commenting.