123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183 |
- define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {
- var Controller = {
- index: function () {
- // 初始化表格参数配置
- Table.api.init({
- extend: {
- index_url: 'category/index',
- add_url: 'category/add',
- edit_url: 'category/edit',
- del_url: 'category/del',
- multi_url: 'category/multi',
- dragsort_url: 'ajax/weigh',
- table: 'category',
- }
- });
- var table = $("#table");
- var tableOptions = {
- url: $.fn.bootstrapTable.defaults.extend.index_url,
- pk: 'id',
- sortName: 'weigh',
- escape: false,
- columns: [
- [
- {checkbox: true},
- {field: 'id', title: __('Id')},
- // {field: 'type', title: __('Type'), operate: false, searchList: Config.searchList, formatter: Table.api.formatter.label},
- {field: 'name', title: __('Name'), align: 'left', formatter:function (value, row, index) {
- return value.toString().replace(/(&|&)nbsp;/g, ' ');
- }
- },
- {field: 'nickname', title: __('Nickname')},
- // {field: 'flag', title: __('Flag'), formatter: Table.api.formatter.flag},
- // {field: 'image', title: __('Image'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.image},
- {field: 'weigh', title: __('Weigh')},
- {
- field: 'id',
- title: '<a href="javascript:;" class="btn btn-success btn-xs btn-toggle" style="border-top:none;"><i class="fa fa-chevron-up"></i></a>',
- operate: false,
- formatter: Controller.api.formatter.subnode
- },
- {field: 'status', title: __('Status'), operate: false, formatter: Table.api.formatter.status},
- {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
- ]
- ],
- pagination: false,
- search: false,
- commonSearch: false,
- rowAttributes: function (row, index) {
- if (this.totalRows > 500) {
- return row.pid == 0 ? {} : {style: "display:none"};
- }
- return row.haschild == 1 || row.ismenu == 1 ? {} : {style: "display:none"};
- }
- };
- // 初始化表格
- table.bootstrapTable(tableOptions);
- // 为表格绑定事件
- Table.api.bindevent(table);
- //绑定TAB事件
- // $('a[data-toggle="tab"]').on('shown.bs.tab', function (e) {
- // // var options = table.bootstrapTable(tableOptions);
- // var typeStr = $(this).attr("href").replace('#', '');
- // var options = table.bootstrapTable('getOptions');
- // options.pageNumber = 1;
- // options.queryParams = function (params) {
- // // params.filter = JSON.stringify({type: typeStr});
- // params.type = typeStr;
- //
- // return params;
- // };
- // table.bootstrapTable('refresh', {});
- // return false;
- //
- // });
- //必须默认触发shown.bs.tab事件
- // $('ul.nav-tabs li.active a[data-toggle="tab"]').trigger("shown.bs.tab");
- var btnSuccessEvent = function (data, ret) {
- if ($(this).hasClass("btn-change")) {
- var index = $(this).data("index");
- var row = Table.api.getrowbyindex(table, index);
- row.ismenu = $("i.fa.text-gray", this).length > 0 ? 1 : 0;
- table.bootstrapTable("updateRow", {index: index, row: row});
- } else if ($(this).hasClass("btn-delone")) {
- if ($(this).closest("tr[data-index]").find("a.btn-node-sub.disabled").length > 0) {
- $(this).closest("tr[data-index]").remove();
- } else {
- table.bootstrapTable('refresh');
- }
- } else if ($(this).hasClass("btn-dragsort")) {
- table.bootstrapTable('refresh');
- }
- Fast.api.refreshmenu();
- return false;
- };
- //表格内容渲染前
- table.on('pre-body.bs.table', function (e, data) {
- var options = table.bootstrapTable("getOptions");
- options.escape = true;
- });
- //当内容渲染完成后
- table.on('post-body.bs.table', function (e, data) {
- var options = table.bootstrapTable("getOptions");
- options.escape = false;
- //点击切换/排序/删除操作后刷新左侧菜单
- $(".btn-change[data-id],.btn-delone,.btn-dragsort").data("success", btnSuccessEvent);
- });
- //显示隐藏子节点
- $(document).on("click", ".btn-node-sub", function (e) {
- var status = $(this).data("shown") ? true : false;
- $("a.btn[data-pid='" + $(this).data("id") + "']").each(function () {
- $(this).closest("tr").toggle(!status);
- });
- $(this).data("shown", !status);
- return false;
- });
- //批量删除后的回调
- $(".toolbar > .btn-del,.toolbar .btn-more~ul>li>a").data("success", function (e) {
- Fast.api.refreshmenu();
- });
- //展开隐藏一级
- $(document.body).on("click", ".btn-toggle", function (e) {
- $("a.btn[data-id][data-pid][data-pid!=0].disabled").closest("tr").hide();
- var that = this;
- var show = $("i", that).hasClass("fa-chevron-down");
- $("i", that).toggleClass("fa-chevron-down", !show);
- $("i", that).toggleClass("fa-chevron-up", show);
- $("a.btn[data-id][data-pid][data-pid!=0]").not('.disabled').closest("tr").toggle(show);
- $(".btn-node-sub[data-pid=0]").data("shown", show);
- });
- //展开隐藏全部
- $(document.body).on("click", ".btn-toggle-all", function (e) {
- var that = this;
- var show = $("i", that).hasClass("fa-plus");
- $("i", that).toggleClass("fa-plus", !show);
- $("i", that).toggleClass("fa-minus", show);
- $(".btn-node-sub.disabled").closest("tr").toggle(show);
- $(".btn-node-sub").data("shown", show);
- });
- },
- add: function () {
- Controller.api.bindevent();
- setTimeout(function () {
- $("#c-type").trigger("change");
- }, 100);
- },
- edit: function () {
- Controller.api.bindevent();
- },
- api: {
- formatter: {
- subnode: function (value, row, index) {
- return '<a href="javascript:;" data-toggle="tooltip" title="' + __('Toggle sub menu') + '" data-id="' + row.id + '" data-pid="' + row.pid + '" class="btn btn-xs '
- + (row.haschild == 1 || row.ismenu == 1 ? 'btn-success' : 'btn-default disabled') + ' btn-node-sub"><i class="fa fa-sitemap"></i></a>';
- }
- },
- bindevent: function () {
- $(document).on("change", "#c-type", function () {
- $("#c-pid option[data-type='all']").prop("selected", true);
- $("#c-pid option").removeClass("hide");
- $("#c-pid option[data-type!='" + $(this).val() + "'][data-type!='all']").addClass("hide");
- $("#c-pid").data("selectpicker") && $("#c-pid").selectpicker("refresh");
- });
- Form.api.bindevent($("form[role=form]"));
- }
- }
- };
- return Controller;
- });
|