2024-06-02 13:38:07 +08:00
|
|
|
{% load static %}
|
|
|
|
|
|
|
|
<div id="addModifyModal" class="modal fade bd-example-modal-lg" tabindex="-1" role="dialog"
|
|
|
|
aria-labelledby="formModalCenterTitle" aria-hidden="true">
|
|
|
|
<div class="modal-dialog modal-dialog-centered modal-xl" role="document">
|
|
|
|
<div class="modal-content">
|
|
|
|
<div class="modal-header">
|
|
|
|
<h5 class="modal-title" id="formModalTitle"></h5>
|
|
|
|
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
|
|
|
|
</div>
|
|
|
|
<div class="modal-body">
|
|
|
|
<form id="formModalForm">
|
|
|
|
{% csrf_token %}
|
|
|
|
<input type="hidden" id="operationType" name="operation_type">
|
2024-06-06 15:08:38 +08:00
|
|
|
<input type="hidden" id="{{ id_name }}" name="{{ id_name }}">
|
|
|
|
{% if table_columns|length <= 4 %}
|
|
|
|
<div class="row">
|
|
|
|
{% for item in table_columns %}
|
|
|
|
{% if item.is_add %}
|
|
|
|
<div class="col">
|
|
|
|
<div class="form-group">
|
|
|
|
<label class="form-label" for="{{ item.field }}">{{ item.label }}</label>
|
|
|
|
{% if item.type == 'text' %}
|
|
|
|
<input type="text" class="form-control" id="{{ item.id }}"
|
|
|
|
name="{{ item.field }}">
|
|
|
|
{% elif item.type == 'select' %}
|
|
|
|
<select class="form-control" id="{{ item.id }}"
|
|
|
|
name="{{ item.field }}">
|
|
|
|
{% for option in item.options %}
|
|
|
|
<option value="{{ option }}">{{ option }}</option>
|
|
|
|
{% endfor %}
|
|
|
|
</select>
|
|
|
|
{% elif item.type == 'date' %}
|
|
|
|
<input type="date" class="form-control" id="{{ item.id }}"
|
|
|
|
name="{{ item.field }}">
|
|
|
|
{% elif item.type == 'textarea' %}
|
|
|
|
<textarea class="form-control" id="{{ item.id }}"
|
|
|
|
name="{{ item.field }}"></textarea>
|
|
|
|
{% elif item.type == 'month' %}
|
|
|
|
<input type="month" class="form-control" id="{{ item.id }}"
|
|
|
|
name="{{ item.field }}">
|
|
|
|
{% elif item.type == 'number' %}
|
|
|
|
<input type="number" class="form-control" id="{{ item.id }}"
|
|
|
|
name="{{ item.field }}">
|
|
|
|
{% elif item.type == 'email' %}
|
|
|
|
<input type="email" class="form-control" id="{{ item.id }}"
|
|
|
|
name="{{ item.field }}">
|
|
|
|
{% endif %}
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
{% endif %}
|
|
|
|
{% endfor %}
|
2024-06-02 13:38:07 +08:00
|
|
|
</div>
|
2024-06-06 15:08:38 +08:00
|
|
|
{% else %}
|
|
|
|
{% for item in table_columns|slice:"1:" %}
|
|
|
|
{% if forloop.counter0|divisibleby:"3" %}
|
|
|
|
<div class="row mb-3">
|
|
|
|
{% endif %}
|
|
|
|
{% if item.is_add %}
|
|
|
|
<div class="col-md-4">
|
|
|
|
<div class="form-group">
|
|
|
|
<label class="form-label" for="{{ item.field }}">{{ item.label }}</label>
|
|
|
|
{% if item.type == 'text' %}
|
|
|
|
<input type="text" class="form-control" id="{{ item.id }}"
|
|
|
|
name="{{ item.field }}">
|
|
|
|
{% elif item.type == 'select' %}
|
|
|
|
<select class="form-control" id="{{ item.id }}" name="{{ item.field }}">
|
|
|
|
{% for option in item.options %}
|
|
|
|
<option value="{{ option }}">{{ option }}</option>
|
|
|
|
{% endfor %}
|
|
|
|
</select>
|
|
|
|
{% elif item.type == 'date' %}
|
|
|
|
<input type="date" class="form-control" id="{{ item.id }}"
|
|
|
|
name="{{ item.field }}">
|
|
|
|
{% elif item.type == 'textarea' %}
|
|
|
|
<textarea class="form-control" id="{{ item.id }}"
|
|
|
|
name="{{ item.field }}"></textarea>
|
|
|
|
{% elif item.type == 'month' %}
|
|
|
|
<input type="month" class="form-control" id="{{ item.id }}"
|
|
|
|
name="{{ item.field }}">
|
|
|
|
{% elif item.type == 'number' %}
|
|
|
|
<input type="number" class="form-control" id="{{ item.id }}"
|
|
|
|
name="{{ item.field }}">
|
|
|
|
{% elif item.type == 'email' %}
|
|
|
|
<input type="email" class="form-control" id="{{ item.id }}"
|
|
|
|
name="{{ item.field }}">
|
|
|
|
{% endif %}
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
{% endif %}
|
|
|
|
{% if forloop.counter|divisibleby:"3" or forloop.last %}
|
|
|
|
</div>
|
|
|
|
{% endif %}
|
|
|
|
|
|
|
|
{% endfor %}
|
2024-06-02 13:38:07 +08:00
|
|
|
{% endif %}
|
2024-06-03 11:13:20 +08:00
|
|
|
</form>
|
2024-06-02 13:38:07 +08:00
|
|
|
</div>
|
|
|
|
<div class="modal-footer">
|
|
|
|
<button type="button" class="btn btn-primary" id="submitForm">提交</button>
|
|
|
|
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">取消</button>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
|
|
|
|
<script>
|
|
|
|
function openFormModal(operationType, record = null) {
|
|
|
|
$('#operationType').val(operationType);
|
|
|
|
if (operationType === 'add') {
|
|
|
|
$('#formModalTitle').text('添加');
|
|
|
|
$('#formModalForm').trigger('reset');
|
2024-06-04 16:50:30 +08:00
|
|
|
$('#{{ list_key }}').val('');
|
2024-06-02 13:38:07 +08:00
|
|
|
} else if (operationType === 'modify' && record) {
|
|
|
|
$('#formModalTitle').text('修改');
|
2024-06-06 15:08:38 +08:00
|
|
|
$('#{{ id_name }}').val(record.{{ id_name }});
|
|
|
|
{% for item in table_columns %}
|
|
|
|
{% if item.is_add %}
|
|
|
|
var fieldType = '{{ item.type }}';
|
|
|
|
$('#{{ item.id }}').val(record['{{ item.field }}']);
|
|
|
|
if (fieldType === 'date') {
|
|
|
|
var recordBirthday = record['{{ item.field }}'];
|
|
|
|
var dateParts = recordBirthday.match(/(\d{4})年(\d{1,2})月(\d{1,2})日/);
|
|
|
|
if (dateParts) {
|
|
|
|
var year = dateParts[1];
|
|
|
|
var month = dateParts[2].padStart(2, '0'); // 确保两位数格式
|
|
|
|
var day = dateParts[3].padStart(2, '0'); // 确保两位数格式
|
|
|
|
var formattedDate = `${year}-${month}-${day}`;
|
|
|
|
$('#{{ item.id }}').val(formattedDate);
|
2024-06-03 00:13:17 +08:00
|
|
|
}
|
2024-06-06 15:08:38 +08:00
|
|
|
}
|
|
|
|
if (fieldType === 'select') {
|
|
|
|
$('#{{ item.id }}').val(record['{{ item.field }}']);
|
|
|
|
$('#{{ item.id }} option').each(function () {
|
|
|
|
if ($(this).val() === record['{{ item.field }}']) {
|
|
|
|
$(this).prop('selected', true);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
{% endif %}
|
2024-06-02 13:38:07 +08:00
|
|
|
{% endfor %}
|
|
|
|
}
|
|
|
|
$('#addModifyModal').modal('show');
|
|
|
|
}
|
|
|
|
|
|
|
|
$(document).ready(function () {
|
|
|
|
$('.edit-btn').on('click', function () {
|
2024-06-03 00:13:17 +08:00
|
|
|
const record = {
|
2024-06-06 15:08:38 +08:00
|
|
|
"{{ id_name }}": $(this).data("target_id")
|
2024-06-02 14:07:08 +08:00
|
|
|
};
|
2024-06-06 15:08:38 +08:00
|
|
|
{% for item in table_columns %}
|
|
|
|
{% if item.is_add %}
|
|
|
|
record['{{ item.field }}'] = $(this).data('{{ item.field }}');
|
|
|
|
{% endif %}
|
2024-06-02 13:38:07 +08:00
|
|
|
{% endfor %}
|
|
|
|
openFormModal('modify', record);
|
|
|
|
});
|
|
|
|
|
2024-06-06 15:08:38 +08:00
|
|
|
var modifyUrl = "{{ url.modify }}";
|
|
|
|
var addUrl = "{{ url.add }}";
|
2024-06-04 16:50:30 +08:00
|
|
|
|
|
|
|
|
2024-06-02 13:38:07 +08:00
|
|
|
$('#submitForm').on('click', function () {
|
2024-06-02 14:07:08 +08:00
|
|
|
var operationType = $('#operationType').val();
|
2024-06-02 13:38:07 +08:00
|
|
|
var formData = $('#formModalForm').serialize();
|
2024-06-04 16:50:30 +08:00
|
|
|
var url = operationType === 'modify' ? modifyUrl : addUrl;
|
2024-06-02 13:38:07 +08:00
|
|
|
|
|
|
|
$.ajax({
|
|
|
|
type: 'POST',
|
|
|
|
url: url,
|
|
|
|
data: formData,
|
|
|
|
success: function (response) {
|
|
|
|
$('#addModifyModal').modal('hide');
|
2024-06-02 14:07:08 +08:00
|
|
|
if (operationType === 'modify') {
|
|
|
|
alert('修改成功');
|
|
|
|
} else {
|
|
|
|
alert('添加成功');
|
|
|
|
}
|
2024-06-02 13:38:07 +08:00
|
|
|
location.reload(); // 刷新页面
|
|
|
|
},
|
|
|
|
error: function (response) {
|
|
|
|
alert('操作失败,请重试。');
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
// 当模态框关闭时,重置表单
|
|
|
|
$('#addModifyModal').on('hidden.bs.modal', function () {
|
|
|
|
$('#formModalForm').trigger('reset');
|
|
|
|
$('#formModalTitle').text('');
|
|
|
|
$('#target_id').val('');
|
|
|
|
$('#operationType').val('');
|
2024-06-06 15:08:38 +08:00
|
|
|
{% for item in table_columns %}
|
|
|
|
$('#{{ item.field }}').val('');
|
2024-06-02 13:38:07 +08:00
|
|
|
{% endfor %}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
</script>
|