XH_Digital_Management/templates/form_partial.html

139 lines
9.5 KiB
HTML

{% load tags %}
<form id="addEditForm" method="post">
{% csrf_token %}
{% if form.instance.pk %}
<input type="hidden" name="id" value="{{ form.instance.pk }}">
{% endif %}
<div class="card-body">
<div class="row">
<div class="col-md-6">
{% for field in form %}
{% if forloop.counter0|divisibleby:2 %}
<div class="mb-3">
<label class="form-label" for="{{ field.id_for_label }}">{{ field.label }}</label>
{% if field.field.widget.input_type == "text" %}
<input type="text" class="form-control" id="{{ field.id_for_label }}" name="{{ field.name }}" value="{{ field.value|default_if_none:'' }}" placeholder="{{ field.label }}">
{% elif field.field.widget.input_type == "number" %}
<input type="number" class="form-control" id="{{ field.id_for_label }}" name="{{ field.name }}" value="{{ field.value|default_if_none:'' }}" placeholder="{{ field.label }}">
{% elif field.field.widget.input_type == "select" %}
<select class="form-control" id="{{ field.id_for_label }}" name="{{ field.name }}">
{% for choice in field.field.choices %}
<option value="{{ choice.0 }}" {% if choice.0 == field.value %}selected{% endif %}>{{ choice.1 }}</option>
{% endfor %}
</select>
{% elif field|is_input_type:"textarea" %}
<textarea class="form-control" id="{{ field.id_for_label }}" name="{{ field.name }}" rows="3">{{ field.value|default_if_none:'' }}</textarea>
{% elif field.field.widget.input_type == "date" %}
<input type="date" class="form-control" id="{{ field.id_for_label }}" name="{{ field.name }}" value="{{ field.value|date:"Y-m-d" }}">
{% elif field.field.widget.input_type == "datetime-local" %}
<input type="datetime-local" class="form-control" id="{{ field.id_for_label }}" name="{{ field.name }}" value="{{ field.value|date:"Y-m-d\TH:i" }}">
{% elif field.field.widget.input_type == "email" %}
<input type="email" class="form-control" id="{{ field.id_for_label }}" name="{{ field.name }}" value="{{ field.value|default_if_none:'' }}" placeholder="{{ field.label }}">
{% elif field.field.widget.input_type == "password" %}
<input type="password" class="form-control" id="{{ field.id_for_label }}" name="{{ field.name }}" placeholder="{{ field.label }}">
{% elif field.field.widget.input_type == "checkbox" %}
<div class="form-check">
<input type="checkbox" class="form-check-input" id="{{ field.id_for_label }}" name="{{ field.name }}" {% if field.value %}checked{% endif %}>
<label class="form-check-label" for="{{ field.id_for_label }}">{{ field.label }}</label>
</div>
{% else %}
{{ field }}
{% endif %}
{% if field.help_text %}
<small class="form-text text-muted">{{ field.help_text }}</small>
{% endif %}
{% for error in field.errors %}
<div class="text-danger">{{ error }}</div>
{% endfor %}
</div>
{% endif %}
{% endfor %}
</div>
<div class="col-md-6">
{% for field in form %}
{% if not forloop.counter0|divisibleby:2 %}
<div class="mb-3">
<label class="form-label" for="{{ field.id_for_label }}">{{ field.label }}</label>
{% if field.field.widget.input_type == "text" %}
<input type="text" class="form-control" id="{{ field.id_for_label }}" name="{{ field.name }}" value="{{ field.value|default_if_none:'' }}" placeholder="{{ field.label }}">
{% elif field.field.widget.input_type == "number" %}
<input type="number" class="form-control" id="{{ field.id_for_label }}" name="{{ field.name }}" value="{{ field.value|default_if_none:'' }}" placeholder="{{ field.label }}">
{% elif field.field.widget.input_type == "select" %}
<select class="form-control" id="{{ field.id_for_label }}" name="{{ field.name }}">
{% for choice in field.field.choices %}
<option value="{{ choice.0 }}" {% if choice.0 == field.value %}selected{% endif %}>{{ choice.1 }}</option>
{% endfor %}
</select>
{% elif field|is_input_type:"textarea" %}
<textarea class="form-control" id="{{ field.id_for_label }}" name="{{ field.name }}" rows="3">{{ field.value|default_if_none:'' }}</textarea>
{% elif field.field.widget.input_type == "date" %}
<input type="date" class="form-control" id="{{ field.id_for_label }}" name="{{ field.name }}" value="{{ field.value|date:"Y-m-d" }}">
{% elif field.field.widget.input_type == "datetime-local" %}
<input type="datetime-local" class="form-control" id="{{ field.id_for_label }}" name="{{ field.name }}" value="{{ field.value|date:"Y-m-d\TH:i" }}">
{% elif field.field.widget.input_type == "month" %}
<input type="date" class="form-control" id="{{ field.id_for_label }}" name="{{ field.name }}" value="{{ field.value|date:"Y-m-d" }}">
{% elif field.field.widget.input_type == "email" %}
<input type="email" class="form-control" id="{{ field.id_for_label }}" name="{{ field.name }}" value="{{ field.value|default_if_none:'' }}" placeholder="{{ field.label }}">
{% elif field.field.widget.input_type == "password" %}
<input type="password" class="form-control" id="{{ field.id_for_label }}" name="{{ field.name }}" placeholder="{{ field.label }}">
{% elif field.field.widget.input_type == "checkbox" %}
<div class="form-check">
<input type="checkbox" class="form-check-input" id="{{ field.id_for_label }}" name="{{ field.name }}" {% if field.value %}checked{% endif %}>
<label class="form-check-label" for="{{ field.id_for_label }}">{{ field.label }}</label>
</div>
{% else %}
{{ field }}
{% endif %}
{% if field.help_text %}
<small class="form-text text-muted">{{ field.help_text }}</small>
{% endif %}
{% for error in field.errors %}
<div class="text-danger">{{ error }}</div>
{% endfor %}
</div>
{% endif %}
{% endfor %}
</div>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">取消</button>
<button type="submit" class="btn btn-primary" id="submitForm">保存</button>
</div>
</form>
<script type="text/javascript">
$(document).ready(function () {
function updateSecondaryDepartments(primaryDepartmentId) {
var url = "{% url 'load_secondary_departments' %}";
$.ajax({
url: url,
data: {
'primary_department_id': primaryDepartmentId
},
success: function (data) {
$('#id_secondary_department').html('');
$('#id_secondary_department').append('<option value="">选择二级部门</option>');
$.each(data, function (key, value) {
$('#id_secondary_department').append('<option value="' + value.secondary_department_id + '">' + value.secondary_department_name + '</option>');
});
}
});
}
$('#id_primary_department').change(function () {
var primaryDepartmentId = $(this).val();
if (primaryDepartmentId) {
updateSecondaryDepartments(primaryDepartmentId);
} else {
$('#id_secondary_department').html('<option value="">选择二级部门</option>');
}
});
// 如果在页面加载时已选择一级部门,则加载相应的二级部门
var initialPrimaryDepartmentId = $('#id_primary_department').val();
if (initialPrimaryDepartmentId) {
updateSecondaryDepartments(initialPrimaryDepartmentId);
}
});
</script>