2024-07-21 20:31:04 +08:00
|
|
|
function Modal(modalTitle, fields, onSubmit) {
|
|
|
|
this.modalTitle = modalTitle
|
|
|
|
this.fields = fields
|
|
|
|
this.onSubmit = onSubmit
|
|
|
|
}
|
|
|
|
|
2024-08-06 20:37:34 +08:00
|
|
|
Modal.prototype.RenderModalForm = function () {
|
2024-07-21 20:31:04 +08:00
|
|
|
let self = this
|
|
|
|
// 设置表单标题
|
|
|
|
$('#genericFormModalLabel').text(self.modalTitle)
|
|
|
|
// 生成表单内容
|
|
|
|
let $form = $('#genericForm')
|
|
|
|
$form.empty()
|
|
|
|
self.fields.forEach(function(field) {
|
|
|
|
let $formGroup = $('<div class="form-group"></div>')
|
|
|
|
$formGroup.append('<label>' + field.label + '</label>')
|
|
|
|
|
|
|
|
let $input
|
|
|
|
if (field.type === 'textarea') {
|
|
|
|
$input = $('<textarea class="form-control" name="' + field.name + '"></textarea>')
|
|
|
|
} else {
|
|
|
|
$input = $('<input type="' + field.type + '" class="form-control" name="' + field.name + '">')
|
|
|
|
}
|
|
|
|
|
|
|
|
$formGroup.append($input)
|
|
|
|
$form.append($formGroup)
|
|
|
|
})
|
|
|
|
// 显示模态框
|
|
|
|
$('#genericFormModal').modal('show');
|
|
|
|
|
|
|
|
// 处理表单提交
|
|
|
|
$('#genericFormSubmit').off('click').on('click', function() {
|
|
|
|
let formData = {}
|
|
|
|
$form.serializeArray().forEach(function(item) {
|
|
|
|
formData[item.name] = item.value
|
|
|
|
})
|
|
|
|
self.onSubmit(formData)
|
|
|
|
$('#genericFormModal').modal('hide')
|
|
|
|
})
|
|
|
|
}
|