car/public/assets/js/backend/cars.js
2025-08-15 17:55:20 +08:00

219 lines
10 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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: 'count', title: '库存', 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,
buttons: [
{
name: 'edit',
text: "修改",
icon: 'fa fa-pencil',
title: __('Edit'),
extend: 'data-toggle="tooltip" data-container="body"',
classname: 'btn btn-xs btn-info btn-editone',
},
{
name: 'sale',
icon: 'fa fa-copy',
title: '售出',
text: "售出",
url: function (row) {
return 'car/sales/add/ids/' + row.id;
},
extend: 'data-toggle="tooltip" data-container="body"',
classname: 'btn btn-xs btn-info btn-dialog',
},
{
name: 'del',
text: "删除",
icon: 'fa fa-trash',
title: __('Delete'),
extend: 'data-toggle="tooltip"',
classname: 'btn btn-xs btn-danger btn-delone',
},
],
}
]
]
});
// 为表格绑定事件
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;
});