新增表格初始化搜索功能;新增getQueryVariable助手函数.

This commit is contained in:
augushong
2021-12-14 15:40:58 +08:00
parent 644f9ffbfe
commit 6bb2b70807
2 changed files with 52 additions and 16 deletions

View File

@@ -29,20 +29,21 @@ define(["jquery", "easy-admin"], function ($, ea) {
}],
'delete', 'export'],
cols: [[
{type: "checkbox"},
{field: 'id', width: 80, title: 'ID'},
{field: 'sort', width: 80, title: '排序', edit: 'text'},
{field: 'cate.title', minWidth: 80, title: '商品分类'},
{field: 'title', minWidth: 80, title: '商品名称'},
{field: 'logo', minWidth: 80, title: '分类图片', search: false, templet: ea.table.image},
{field: 'market_price', width: 100, title: '市场价', templet: ea.table.price},
{field: 'discount_price', width: 100, title: '折扣价', templet: ea.table.price},
{field: 'total_stock', width: 100, title: '库存统计'},
{field: 'stock', width: 100, title: '剩余库存'},
{field: 'virtual_sales', width: 100, title: '虚拟销量'},
{field: 'sales', width: 80, title: '销量'},
{field: 'status', title: '状态', width: 85, selectList: {0: '禁用', 1: '启用'}, templet: ea.table.switch},
{field: 'create_time', minWidth: 80, title: '创建时间', search: 'range'},
{ type: "checkbox" },
{ field: 'id', width: 80, title: 'ID' },
{ field: 'sort', width: 80, title: '排序', edit: 'text' },
{ field: 'cate.title', minWidth: 80, title: '商品分类', },
{ field: 'cate.id', minWidth: 80, title: '商品分类', hide: true, defaultSearchValue: ea.getQueryVariable('cate_id', '') },
{ field: 'title', minWidth: 80, title: '商品名称', },
{ field: 'logo', minWidth: 80, title: '分类图片', search: false, templet: ea.table.image },
{ field: 'market_price', width: 100, title: '市场价', templet: ea.table.price },
{ field: 'discount_price', width: 100, title: '折扣价', templet: ea.table.price },
{ field: 'total_stock', width: 100, title: '库存统计' },
{ field: 'stock', width: 100, title: '剩余库存' },
{ field: 'virtual_sales', width: 100, title: '虚拟销量' },
{ field: 'sales', width: 80, title: '销量' },
{ field: 'status', title: '状态', width: 85, selectList: { 0: '禁用', 1: '启用' }, templet: ea.table.switch },
{ field: 'create_time', minWidth: 80, title: '创建时间', search: 'range' },
{
width: 250,
title: '操作',

View File

@@ -259,7 +259,7 @@ define(["jquery", "tableSelect", "ckeditor"], function ($, tableSelect, undefine
// 初始化表格搜索
if (options.search === true) {
admin.table.renderSearch(options.cols, options.elem, options.id);
options = admin.table.renderSearch(options.cols, options.elem, options.id, options);
}
// 初始化表格左上方工具栏
@@ -320,11 +320,13 @@ define(["jquery", "tableSelect", "ckeditor"], function ($, tableSelect, undefine
});
return '<div>' + toolbarHtml + '</div>';
},
renderSearch: function (cols, elem, tableId) {
renderSearch: function (cols, elem, tableId, options) {
// TODO 只初始化第一个table搜索字段如果存在多个(绝少数需求),得自己去扩展
cols = cols[0] || {};
var newCols = [];
var formHtml = '';
var formatFilter = {},
formatOp = {};
$.each(cols, function (i, d) {
d.field = d.field || false;
d.fieldAlias = admin.parame(d.fieldAlias, d.field);
@@ -333,8 +335,19 @@ define(["jquery", "tableSelect", "ckeditor"], function ($, tableSelect, undefine
d.search = admin.parame(d.search, true);
d.searchTip = d.searchTip || '请输入' + d.title || '';
d.searchValue = d.searchValue || '';
d.defaultSearchValue = d.defaultSearchValue || '';
d.searchOp = d.searchOp || '%*%';
d.timeType = d.timeType || 'datetime';
if (d.defaultSearchValue.length > 0) {
if (d.searchValue.length == 0) {
d.searchValue = d.defaultSearchValue;
}
formatFilter[d.field] = d.defaultSearchValue;
formatOp[d.field] = d.searchOp;
}
if (d.field !== false && d.search !== false) {
switch (d.search) {
case true:
@@ -348,6 +361,7 @@ define(["jquery", "tableSelect", "ckeditor"], function ($, tableSelect, undefine
case 'select':
d.searchOp = '=';
var selectHtml = '';
$.each(d.selectList, function (sI, sV) {
var selected = '';
if (sI === d.searchValue) {
@@ -413,6 +427,13 @@ define(["jquery", "tableSelect", "ckeditor"], function ($, tableSelect, undefine
}
});
}
options.where = {
filter: JSON.stringify(formatFilter),
op: JSON.stringify(formatOp)
}
return options;
},
renderSwitch: function (cols, tableInit, tableId, modifyReload) {
tableInit.modify_url = tableInit.modify_url || false;
@@ -1559,6 +1580,20 @@ define(["jquery", "tableSelect", "ckeditor"], function ($, tableSelect, undefine
}
},
},
getQueryVariable(variable, defaultValue) {
if (typeof defaultValue == 'undefined') {
defaultValue = undefined;
}
var query = window.location.search.substring(1);
var vars = query.split("&");
for (var i = 0; i < vars.length; i++) {
var pair = vars[i].split("=");
if (pair[0] == variable) { return decodeURIComponent(pair[1]); }
}
return defaultValue;
}
};
return admin;
});