189 lines
8.4 KiB
JavaScript
189 lines
8.4 KiB
JavaScript
define(['jquery', 'bootstrap', 'backend', 'table', 'form', 'cascader'], function ($, undefined, Backend, Table, Form) {
|
||
|
||
var Controller = {
|
||
index: function () {
|
||
// 初始化表格参数配置
|
||
Table.api.init({
|
||
extend: {
|
||
index_url: 'cars/index' + location.search,
|
||
add_url: 'cars/add',
|
||
edit_url: 'cars/edit',
|
||
del_url: 'cars/del',
|
||
multi_url: 'cars/multi',
|
||
import_url: 'cars/import',
|
||
table: 'cars',
|
||
}
|
||
});
|
||
|
||
var table = $("#table");
|
||
|
||
// 初始化表格
|
||
table.bootstrapTable({
|
||
url: $.fn.bootstrapTable.defaults.extend.index_url,
|
||
pk: 'id',
|
||
sortName: 'id',
|
||
fixedColumns: true,
|
||
fixedRightNumber: 1,
|
||
columns: [
|
||
[
|
||
{checkbox: true},
|
||
{field: 'id', title: __('Id')},
|
||
{
|
||
field: 'title',
|
||
title: __('Title'),
|
||
operate: 'LIKE',
|
||
table: table,
|
||
class: 'autocontent',
|
||
formatter: Table.api.formatter.content
|
||
},
|
||
{field: 'car_type', title: '类型',
|
||
searchList: {
|
||
"1": '新车',
|
||
"2": '二手车',
|
||
"3": '租车',
|
||
},
|
||
formatter: Table.api.formatter.status},
|
||
{field: 'brand.name', title: __('Brand_id'), operate: false},
|
||
{field: 'series.name', title: __('Series_id'), operate: false},
|
||
{field: 'price', title: __('Price'), operate: 'BETWEEN'},
|
||
{field: 're_price', title: '现价(万元)', operate: 'BETWEEN'},
|
||
{
|
||
field: 'cover_image',
|
||
title: __('Cover_image'),
|
||
operate: false,
|
||
events: Table.api.events.image,
|
||
formatter: Table.api.formatter.image
|
||
},
|
||
{field: 'contact.nickname', title: __('Contact_name'), operate: 'LIKE'},
|
||
{field: 'contact.mobile', title: __('Contact_phone'), operate: 'LIKE'},
|
||
{field: 'active_at', title: '上牌时间', operate: 'RANGE',
|
||
addclass: 'datetimerange',
|
||
autocomplete: false},
|
||
{
|
||
field: 'created_at',
|
||
title: __('Created_at'),
|
||
operate: 'RANGE',
|
||
addclass: 'datetimerange',
|
||
autocomplete: false
|
||
},
|
||
{
|
||
field: 'updated_at',
|
||
title: __('Updated_at'),
|
||
operate: 'RANGE',
|
||
addclass: 'datetimerange',
|
||
autocomplete: false
|
||
},
|
||
{field: 'is_active', title: __('Is_active'),
|
||
searchList: {
|
||
"0": '下架',
|
||
"1": '上架',
|
||
},
|
||
formatter: Table.api.formatter.status},
|
||
{
|
||
field: 'operate',
|
||
title: __('Operate'),
|
||
table: table,
|
||
events: Table.api.events.operate,
|
||
formatter: Table.api.formatter.operate
|
||
}
|
||
]
|
||
]
|
||
});
|
||
|
||
// 为表格绑定事件
|
||
Table.api.bindevent(table);
|
||
},
|
||
add: function () {
|
||
Controller.api.bindevent();
|
||
Controller.api.series();
|
||
Controller.api.extend();
|
||
},
|
||
edit: function () {
|
||
Controller.api.bindevent();
|
||
Controller.api.series();
|
||
Controller.api.extend();
|
||
},
|
||
api: {
|
||
bindevent: function () {
|
||
Form.api.bindevent($("form[role=form]"));
|
||
},
|
||
series: function () {
|
||
var _data = series;
|
||
$('#c-series_id').zdCascader({
|
||
data: _data,
|
||
onChange: function ($this, data, allPathData) {
|
||
// console.log(data,allPathData);
|
||
$('#c-series_id_value').val(data.value);
|
||
}
|
||
});
|
||
$('#c-series_id').val($('#c-series_id').data('value'));
|
||
},
|
||
extend: function () {
|
||
const id = $('input[name="row[id]"]').val();
|
||
let url = "cars/extend";
|
||
if (id) {
|
||
url += '&id=' + id;
|
||
}
|
||
Fast.api.ajax({
|
||
url: url, // 你的 API 地址
|
||
type: "get",
|
||
contentType: 'application/json',
|
||
dataType: "json"
|
||
},
|
||
function (data) {
|
||
var container = $('#add-form');
|
||
|
||
data.forEach(function (field) {
|
||
let html = `
|
||
<div class="form-group">
|
||
<label class="control-label col-xs-12 col-sm-2">${field.label}:</label>
|
||
<div class="col-xs-12 col-sm-8">`;
|
||
|
||
// ✅ 单选框(radio,替代 checkbox)
|
||
if (field.type === 'checkbox' || field.type === 'radio') {
|
||
field.options.forEach(function (opt) {
|
||
// 是否选中
|
||
const checked = String(field.value) === String(opt.value) ? 'checked' : '';
|
||
html += `
|
||
<label class="radio-inline">
|
||
<input type="radio" name="row[${field.name}]" value="${opt.value}" ${checked}> ${opt.key}
|
||
</label>`;
|
||
});
|
||
}
|
||
|
||
// ✅ 数值范围输入(单 input 版本)
|
||
else if (field.type === 'range') {
|
||
const opts = field.options || {};
|
||
const val = field.value ?? ''; // 可能是字符串、数字或空
|
||
const min = opts.start ?? '';
|
||
const max = opts.end ?? '';
|
||
const unit = opts.unit ?? '';
|
||
html += `
|
||
<div class="input-group">
|
||
<input type="number" name="row[${field.name}]"
|
||
class="form-control" placeholder="${unit}"
|
||
min="${min}" max="${max}" value="${val}">
|
||
<span class="input-group-addon">${unit}</span>
|
||
</div>`;
|
||
}
|
||
|
||
html += `</div></div>`;
|
||
container.append(html);
|
||
});
|
||
|
||
// ✅ 添加提交按钮区域
|
||
container.append(`
|
||
<div class="form-group">
|
||
<label class="control-label col-xs-12 col-sm-2"></label>
|
||
<div class="col-xs-12 col-sm-8">
|
||
<button type="submit" class="btn btn-primary btn-embossed">提交</button>
|
||
</div>
|
||
</div>`);
|
||
return false;
|
||
})
|
||
}
|
||
}
|
||
};
|
||
return Controller;
|
||
});
|