car/public/assets/js/backend/attributes.js
2025-06-24 18:17:09 +08:00

101 lines
3.7 KiB
JavaScript

define(['jquery', 'bootstrap', 'backend', 'table', 'form','jsoneditor'], function ($, undefined, Backend, Table, Form, JSONEditor) {
var Controller = {
index: function () {
// 初始化表格参数配置
Table.api.init({
extend: {
index_url: 'attributes/index' + location.search,
add_url: 'attributes/add',
edit_url: 'attributes/edit',
del_url: 'attributes/del',
multi_url: 'attributes/multi',
import_url: 'attributes/import',
table: 'attributes',
}
});
var table = $("#table");
// 初始化表格
table.bootstrapTable({
url: $.fn.bootstrapTable.defaults.extend.index_url,
pk: 'id',
sortName: 'id',
columns: [[{checkbox: true}, {field: 'id', title: __('Id')}, {
field: 'name',
title: __('Name'),
operate: 'LIKE'
}, {field: 'field_key', title: __('Field_key'), operate: 'LIKE'}, {
field: 'input_type',
title: __('Input_type'),
searchList: {
"text": __('Text'),
"select": __('Select'),
"range": __('Range'),
"checkbox": __('Checkbox')
},
formatter: Table.api.formatter.normal
}, {field: 'is_filter', title: __('Is_filter')}, {
field: 'sort_order',
title: __('Sort_order')
}, {
field: 'created_at',
title: __('Created_at'),
operate: 'RANGE',
addclass: 'datetimerange',
autocomplete: false
}, {
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.initJsonEditor();
}, edit: function () {
Controller.api.bindevent();
Controller.api.initJsonEditor();
}, api: {
bindevent: function () {
Form.api.bindevent($("form[role=form]"));
},
initJsonEditor: function () {
var container = document.getElementById("jsoneditor");
var textarea = document.getElementById("jsoncontent");
var options = {
mode: 'code', // or 'code'
modes: ['code'], // allowed modes
onChange: function () {
try {
var json = editor.get(); // get returns object
textarea.value = JSON.stringify(json);
} catch (e) {
console.warn('Invalid JSON');
}
}
};
var editor = new JSONEditor(container, options);
// 如果有旧数据(回显)
if (textarea.value) {
try {
editor.set(JSON.parse(textarea.value));
} catch (e) {
console.warn("Invalid JSON data from server");
}
}
}
}
};
return Controller;
});