审查视图

public/assets/js/require-table.js 41.1 KB
1
define(['jquery', 'bootstrap', 'moment', 'moment/locale/zh-cn', 'bootstrap-table', 'bootstrap-table-lang', 'bootstrap-table-export', 'bootstrap-table-commonsearch', 'bootstrap-table-template', 'bootstrap-table-jumpto'], function ($, undefined, Moment) {
Karson authored
2 3 4 5 6 7
    var Table = {
        list: {},
        // Bootstrap-table 基础配置
        defaults: {
            url: '',
            sidePagination: 'server',
Karson authored
8 9 10
            method: 'get', //请求方法
            toolbar: ".toolbar", //工具栏
            search: true, //是否启用快速搜索
11
            cache: false,
Karson authored
12 13
            commonSearch: true, //是否启用通用搜索
            searchFormVisible: false, //是否始终显示搜索表单
14
            titleForm: '', //为空则不显示标题,不定义默认显示:普通搜索
Karson authored
15
            idTable: 'commonTable',
Karson authored
16
            showExport: true,
17 18
            exportDataType: "all",
            exportTypes: ['json', 'xml', 'csv', 'txt', 'doc', 'excel'],
19 20 21 22
            exportOptions: {
                fileName: 'export_' + Moment().format("YYYY-MM-DD"),
                ignoreColumn: [0, 'operate'] //默认不导出第一列(checkbox)与操作(operate)列
            },
23 24
            pageSize: 10,
            pageList: [10, 25, 50, 'All'],
Karson authored
25
            pagination: true,
Karson authored
26
            clickToSelect: true, //是否启用点击选中
27
            dblClickToEdit: true, //是否启用双击编辑
Karson authored
28
            singleSelect: false, //是否启用单选
Karson authored
29
            showRefresh: false,
30
            showJumpto: true,
31
            locale: 'zh-CN',
Karson authored
32 33
            showToggle: true,
            showColumns: true,
34
            pk: 'id',
Karson authored
35 36 37 38 39 40
            sortName: 'id',
            sortOrder: 'desc',
            paginationFirstText: __("First"),
            paginationPreText: __("Previous"),
            paginationNextText: __("Next"),
            paginationLastText: __("Last"),
Karson authored
41 42 43
            cardView: false, //卡片视图
            checkOnInit: true, //是否在初始化时判断
            escape: true, //是否对内容进行转义
Karson authored
44 45 46 47 48
            extend: {
                index_url: '',
                add_url: '',
                edit_url: '',
                del_url: '',
Karson authored
49
                import_url: '',
Karson authored
50 51 52 53 54 55 56 57 58 59
                multi_url: '',
                dragsort_url: 'ajax/weigh',
            }
        },
        // Bootstrap-table 列配置
        columnDefaults: {
            align: 'center',
            valign: 'middle',
        },
        config: {
Karson authored
60
            firsttd: 'tbody tr td:first-child:not(:has(div.card-views))',
Karson authored
61 62 63 64 65
            toolbar: '.toolbar',
            refreshbtn: '.btn-refresh',
            addbtn: '.btn-add',
            editbtn: '.btn-edit',
            delbtn: '.btn-del',
Karson authored
66
            importbtn: '.btn-import',
Karson authored
67 68 69
            multibtn: '.btn-multi',
            disabledbtn: '.btn-disabled',
            editonebtn: '.btn-editone',
70 71 72 73
            restoreonebtn: '.btn-restoreone',
            destroyonebtn: '.btn-destroyone',
            restoreallbtn: '.btn-restoreall',
            destroyallbtn: '.btn-destroyall',
Karson authored
74 75
            dragsortfield: 'weigh',
        },
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
        button: {
            edit: {
                name: 'edit',
                icon: 'fa fa-pencil',
                title: __('Edit'),
                extend: 'data-toggle="tooltip"',
                classname: 'btn btn-xs btn-success btn-editone'
            },
            del: {
                name: 'del',
                icon: 'fa fa-trash',
                title: __('Del'),
                extend: 'data-toggle="tooltip"',
                classname: 'btn btn-xs btn-danger btn-delone'
            },
            dragsort: {
                name: 'dragsort',
                icon: 'fa fa-arrows',
                title: __('Drag to sort'),
                extend: 'data-toggle="tooltip"',
                classname: 'btn btn-xs btn-primary btn-dragsort'
            }
        },
Karson authored
99 100 101 102 103
        api: {
            init: function (defaults, columnDefaults, locales) {
                defaults = defaults ? defaults : {};
                columnDefaults = columnDefaults ? columnDefaults : {};
                locales = locales ? locales : {};
Karson authored
104 105 106 107
                // 如果是iOS设备则启用卡片视图
                if (navigator.userAgent.match(/(iPod|iPhone|iPad)/)) {
                    Table.defaults.cardView = true;
                }
Karson authored
108 109 110 111 112 113
                // 写入bootstrap-table默认配置
                $.extend(true, $.fn.bootstrapTable.defaults, Table.defaults, defaults);
                // 写入bootstrap-table column配置
                $.extend($.fn.bootstrapTable.columnDefaults, Table.columnDefaults, columnDefaults);
                // 写入bootstrap-table locale配置
                $.extend($.fn.bootstrapTable.locales[Table.defaults.locale], {
Karson authored
114 115
                    formatCommonSearch: function () {
                        return __('Common search');
Karson authored
116
                    },
Karson authored
117 118
                    formatCommonSubmitButton: function () {
                        return __('Submit');
Karson authored
119
                    },
Karson authored
120 121
                    formatCommonResetButton: function () {
                        return __('Reset');
Karson authored
122
                    },
Karson authored
123 124 125 126 127
                    formatCommonCloseButton: function () {
                        return __('Close');
                    },
                    formatCommonChoose: function () {
                        return __('Choose');
128 129 130
                    },
                    formatJumpto: function () {
                        return __('Go');
Karson authored
131 132
                    }
                }, locales);
133 134 135
                if (typeof defaults.exportTypes != 'undefined') {
                    $.fn.bootstrapTable.defaults.exportTypes = defaults.exportTypes;
                }
Karson authored
136 137 138 139 140 141 142 143 144 145
            },
            // 绑定事件
            bindevent: function (table) {
                //Bootstrap-table的父元素,包含table,toolbar,pagnation
                var parenttable = table.closest('.bootstrap-table');
                //Bootstrap-table配置
                var options = table.bootstrapTable('getOptions');
                //Bootstrap操作区
                var toolbar = $(options.toolbar, parenttable);
                //当刷新表格时
146 147 148 149
                table.on('load-error.bs.table', function (status, res, e) {
                    if (e.status === 0) {
                        return;
                    }
Karson authored
150 151
                    Toastr.error(__('Unknown data format'));
                });
152 153 154 155 156 157
                //当加载数据成功时
                table.on('load-success.bs.table', function (e, data) {
                    if (typeof data.rows === 'undefined' && typeof data.code != 'undefined') {
                        Toastr.error(data.msg);
                    }
                });
Karson authored
158 159 160 161
                //当刷新表格时
                table.on('refresh.bs.table', function (e, settings, data) {
                    $(Table.config.refreshbtn, toolbar).find(".fa").addClass("fa-spin");
                });
162 163 164 165 166 167
                if (options.dblClickToEdit) {
                    //当双击单元格时
                    table.on('dbl-click-row.bs.table', function (e, row, element, field) {
                        $(Table.config.editonebtn, element).trigger("click");
                    });
                }
Karson authored
168 169 170
                //当内容渲染完成后
                table.on('post-body.bs.table', function (e, settings, json, xhr) {
                    $(Table.config.refreshbtn, toolbar).find(".fa").removeClass("fa-spin");
171
                    $(Table.config.disabledbtn, toolbar).toggleClass('disabled', true);
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
                    if ($(Table.config.firsttd, table).find("input[type='checkbox'][data-index]").size() > 0) {
                        // 挺拽选择,需要重新绑定事件
                        require(['drag', 'drop'], function () {
                            $(Table.config.firsttd, table).drag("start", function (ev, dd) {
                                return $('<div class="selection" />').css('opacity', .65).appendTo(document.body);
                            }).drag(function (ev, dd) {
                                $(dd.proxy).css({
                                    top: Math.min(ev.pageY, dd.startY),
                                    left: Math.min(ev.pageX, dd.startX),
                                    height: Math.abs(ev.pageY - dd.startY),
                                    width: Math.abs(ev.pageX - dd.startX)
                                });
                            }).drag("end", function (ev, dd) {
                                $(dd.proxy).remove();
                            });
                            $(Table.config.firsttd, table).drop("start", function () {
                                Table.api.toggleattr(this);
                            }).drop(function () {
                                Table.api.toggleattr(this);
                            }).drop("end", function () {
                                Table.api.toggleattr(this);
                            });
                            $.drop({
                                multi: true
Karson authored
196 197
                            });
                        });
198
                    }
Karson authored
199 200
                });
                // 处理选中筛选框后按钮的状态统一变更
201
                table.on('check.bs.table uncheck.bs.table check-all.bs.table uncheck-all.bs.table', function () {
202 203
                    var ids = Table.api.selectedids(table);
                    $(Table.config.disabledbtn, toolbar).toggleClass('disabled', !ids.length);
Karson authored
204
                });
205
                // 绑定TAB事件
206 207
                $('.panel-heading [data-field] a[data-toggle="tab"]').on('shown.bs.tab', function (e) {
                    var field = $(this).closest("[data-field]").data("field");
208
                    var value = $(this).data("value");
209 210 211 212 213 214
                    var object = $("[name='" + field + "']", table.closest(".bootstrap-table").find(".commonsearch-table"));
                    if (object.prop('tagName') == "SELECT") {
                        $("option[value='" + value + "']", object).prop("selected", true);
                    } else {
                        object.val(value);
                    }
215
                    table.bootstrapTable('refresh', {pageNumber: 1});
216 217
                    return false;
                });
Karson authored
218 219 220 221 222 223 224
                // 刷新按钮事件
                $(toolbar).on('click', Table.config.refreshbtn, function () {
                    table.bootstrapTable('refresh');
                });
                // 添加按钮事件
                $(toolbar).on('click', Table.config.addbtn, function () {
                    var ids = Table.api.selectedids(table);
225
                    var url = options.extend.add_url;
226 227 228
                    if (url.indexOf("{ids}") !== -1) {
                        url = Table.api.replaceurl(url, {ids: ids.length > 0 ? ids.join(",") : 0}, table);
                    }
229
                    Fast.api.open(url, __('Add'), $(this).data() || {});
Karson authored
230
                });
Karson authored
231 232 233 234 235 236 237
                // 导入按钮事件
                if ($(Table.config.importbtn, toolbar).size() > 0) {
                    require(['upload'], function (Upload) {
                        Upload.api.plupload($(Table.config.importbtn, toolbar), function (data, ret) {
                            Fast.api.ajax({
                                url: options.extend.import_url,
                                data: {file: data.url},
238
                            }, function (data, ret) {
Karson authored
239 240 241 242 243
                                table.bootstrapTable('refresh');
                            });
                        });
                    });
                }
244
                // 批量编辑按钮事件
Karson authored
245
                $(toolbar).on('click', Table.config.editbtn, function () {
246
                    var that = this;
Karson authored
247
                    //循环弹出多个编辑框
248 249 250 251 252
                    $.each(table.bootstrapTable('getSelections'), function (index, row) {
                        var url = options.extend.edit_url;
                        row = $.extend({}, row ? row : {}, {ids: row[options.pk]});
                        var url = Table.api.replaceurl(url, row, table);
                        Fast.api.open(url, __('Edit'), $(that).data() || {});
Karson authored
253 254
                    });
                });
255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277
                //清空回收站
                $(document).on('click', Table.config.destroyallbtn, function () {
                    var that = this;
                    Layer.confirm(__('Are you sure you want to truncate?'), function () {
                        var url = $(that).data("url") ? $(that).data("url") : $(that).attr("href");
                        Fast.api.ajax(url, function () {
                            Layer.closeAll();
                            table.bootstrapTable('refresh');
                        }, function () {
                            Layer.closeAll();
                        });
                    });
                    return false;
                });
                //还原或删除
                $(document).on('click', Table.config.restoreallbtn + ',' + Table.config.restoreonebtn + ',' + Table.config.destroyonebtn, function () {
                    var that = this;
                    var url = $(that).data("url") ? $(that).data("url") : $(that).attr("href");
                    Fast.api.ajax(url, function () {
                        table.bootstrapTable('refresh');
                    });
                    return false;
                });
Karson authored
278 279 280 281 282 283 284 285 286
                // 批量操作按钮事件
                $(toolbar).on('click', Table.config.multibtn, function () {
                    var ids = Table.api.selectedids(table);
                    Table.api.multi($(this).data("action"), ids, table, this);
                });
                // 批量删除按钮事件
                $(toolbar).on('click', Table.config.delbtn, function () {
                    var that = this;
                    var ids = Table.api.selectedids(table);
287
                    Layer.confirm(
Karson authored
288 289 290 291 292 293
                        __('Are you sure you want to delete the %s selected item?', ids.length),
                        {icon: 3, title: __('Warning'), offset: 0, shadeClose: true},
                        function (index) {
                            Table.api.multi("del", ids, table, that);
                            Layer.close(index);
                        }
Karson authored
294 295 296 297 298 299
                    );
                });
                // 拖拽排序
                require(['dragsort'], function () {
                    //绑定拖动排序
                    $("tbody", table).dragsort({
biorz authored
300
                        itemSelector: 'tr:visible',
Karson authored
301
                        dragSelector: "a.btn-dragsort",
302 303
                        dragEnd: function (a, b) {
                            var element = $("a.btn-dragsort", this);
Karson authored
304 305
                            var data = table.bootstrapTable('getData');
                            var current = data[parseInt($(this).data("index"))];
306
                            var options = table.bootstrapTable('getOptions');
Karson authored
307 308
                            //改变的值和改变的ID集合
                            var ids = $.map($("tbody tr:visible", table), function (tr) {
309
                                return data[parseInt($(tr).data("index"))][options.pk];
Karson authored
310
                            });
311
                            var changeid = current[options.pk];
Karson authored
312
                            var pid = typeof current.pid != 'undefined' ? current.pid : '';
313
                            var params = {
Karson authored
314 315 316 317 318 319
                                url: table.bootstrapTable('getOptions').extend.dragsort_url,
                                data: {
                                    ids: ids.join(','),
                                    changeid: changeid,
                                    pid: pid,
                                    field: Table.config.dragsortfield,
320
                                    orderway: options.sortOrder,
321 322
                                    table: options.extend.table,
                                    pk: options.pk
Karson authored
323 324
                                }
                            };
325 326 327 328 329 330 331 332
                            Fast.api.ajax(params, function (data, ret) {
                                var success = $(element).data("success") || $.noop;
                                if (typeof success === 'function') {
                                    if (false === success.call(element, data, ret)) {
                                        return false;
                                    }
                                }
                                table.bootstrapTable('refresh');
333
                            }, function (data, ret) {
334 335 336 337 338 339
                                var error = $(element).data("error") || $.noop;
                                if (typeof error === 'function') {
                                    if (false === error.call(element, data, ret)) {
                                        return false;
                                    }
                                }
Karson authored
340 341 342 343 344 345
                                table.bootstrapTable('refresh');
                            });
                        },
                        placeHolderTemplate: ""
                    });
                });
346
                $(table).on("click", "input[data-id][name='checkbox']", function (e) {
347
                    var ids = $(this).data("id");
348
                    var row = Table.api.getrowbyid(table, ids);
349
                    table.trigger('check.bs.table', [row, this]);
350
                });
351 352 353 354
                $(table).on("click", "[data-id].btn-change", function (e) {
                    e.preventDefault();
                    Table.api.multi($(this).data("action") ? $(this).data("action") : '', [$(this).data("id")], table, this);
                });
355 356
                $(table).on("click", "[data-id].btn-edit", function (e) {
                    e.preventDefault();
357
                    var ids = $(this).data("id");
358
                    var row = Table.api.getrowbyid(table, ids);
359 360 361
                    row.ids = ids;
                    var url = Table.api.replaceurl(options.extend.edit_url, row, table);
                    Fast.api.open(url, __('Edit'), $(this).data() || {});
362 363 364 365 366
                });
                $(table).on("click", "[data-id].btn-del", function (e) {
                    e.preventDefault();
                    var id = $(this).data("id");
                    var that = this;
367
                    Layer.confirm(
Karson authored
368 369 370 371 372 373
                        __('Are you sure you want to delete this item?'),
                        {icon: 3, title: __('Warning'), shadeClose: true},
                        function (index) {
                            Table.api.multi("del", id, table, that);
                            Layer.close(index);
                        }
374 375
                    );
                });
Karson authored
376 377 378 379 380 381 382
                var id = table.attr("id");
                Table.list[id] = table;
                return table;
            },
            // 批量操作请求
            multi: function (action, ids, table, element) {
                var options = table.bootstrapTable('getOptions');
383
                var data = element ? $(element).data() : {};
384
                var ids = ($.isArray(ids) ? ids.join(",") : ids);
385
                var url = typeof data.url !== "undefined" ? data.url : (action == "del" ? options.extend.del_url : options.extend.multi_url);
386
                url = this.replaceurl(url, {ids: ids}, table);
387 388
                var params = typeof data.params !== "undefined" ? (typeof data.params == 'object' ? $.param(data.params) : data.params) : '';
                var options = {url: url, data: {action: action, ids: ids, params: params}};
389 390 391 392 393 394 395
                Fast.api.ajax(options, function (data, ret) {
                    var success = $(element).data("success") || $.noop;
                    if (typeof success === 'function') {
                        if (false === success.call(element, data, ret)) {
                            return false;
                        }
                    }
Karson authored
396
                    table.bootstrapTable('refresh');
397 398 399 400 401 402 403
                }, function (data, ret) {
                    var error = $(element).data("error") || $.noop;
                    if (typeof error === 'function') {
                        if (false === error.call(element, data, ret)) {
                            return false;
                        }
                    }
Karson authored
404 405 406 407 408 409
                });
            },
            // 单元格元素事件
            events: {
                operate: {
                    'click .btn-editone': function (e, value, row, index) {
410
                        e.stopPropagation();
Karson authored
411
                        e.preventDefault();
412 413 414 415 416 417
                        var table = $(this).closest('table');
                        var options = table.bootstrapTable('getOptions');
                        var ids = row[options.pk];
                        row = $.extend({}, row ? row : {}, {ids: ids});
                        var url = options.extend.edit_url;
                        Fast.api.open(Table.api.replaceurl(url, row, table), __('Edit'), $(this).data() || {});
Karson authored
418 419
                    },
                    'click .btn-delone': function (e, value, row, index) {
420
                        e.stopPropagation();
Karson authored
421
                        e.preventDefault();
Karson authored
422
                        var that = this;
423 424
                        var top = $(that).offset().top - $(window).scrollTop();
                        var left = $(that).offset().left - $(window).scrollLeft() - 260;
Karson authored
425
                        if (top + 154 > $(window).height()) {
426 427
                            top = top - 154;
                        }
Karson authored
428
                        if ($(window).width() < 480) {
429 430
                            top = left = undefined;
                        }
431
                        Layer.confirm(
Karson authored
432 433 434 435 436 437 438 439
                            __('Are you sure you want to delete this item?'),
                            {icon: 3, title: __('Warning'), offset: [top, left], shadeClose: true},
                            function (index) {
                                var table = $(that).closest('table');
                                var options = table.bootstrapTable('getOptions');
                                Table.api.multi("del", row[options.pk], table, that);
                                Layer.close(index);
                            }
Karson authored
440 441
                        );
                    }
442 443 444
                },//单元格图片预览
                image: {
                    'click .img-center': function (e, value, row, index) {
445
                        var data = [];
446
                        value = value.toString().split(",");
447 448 449 450 451
                        $.each(value, function (index, value) {
                            data.push({
                                src: Fast.api.cdnurl(value),
                            });
                        });
452
                        Layer.photos({
453
                            photos: {
454
                                "start": $(this).parent().index(),
455 456 457 458 459
                                "data": data
                            },
                            anim: 5 //0-6的选择,指定弹出图片动画类型,默认随机(请注意,3.0之前的版本用shift参数)
                        });
                    },
Karson authored
460 461 462 463 464
                }
            },
            // 单元格数据格式化
            formatter: {
                icon: function (value, row, index) {
Karson authored
465 466
                    if (!value)
                        return '';
467
                    value = value === null ? '' : value.toString();
468
                    value = value.indexOf(" ") > -1 ? value : "fa fa-" + value;
Karson authored
469
                    //渲染fontawesome图标
470
                    return '<i class="' + value + '"></i> ' + value;
Karson authored
471
                },
472
                image: function (value, row, index) {
Karson authored
473
                    value = value ? value : '/assets/img/blank.gif';
474
                    var classname = typeof this.classname !== 'undefined' ? this.classname : 'img-sm img-center';
475
                    return '<a href="javascript:"><img class="' + classname + '" src="' + Fast.api.cdnurl(value) + '" /></a>';
Karson authored
476
                },
477
                images: function (value, row, index) {
478
                    value = value === null ? '' : value.toString();
479
                    var classname = typeof this.classname !== 'undefined' ? this.classname : 'img-sm img-center';
480
                    var arr = value.split(',');
481 482
                    var html = [];
                    $.each(arr, function (i, value) {
Karson authored
483
                        value = value ? value : '/assets/img/blank.gif';
484
                        html.push('<a href="javascript:"><img class="' + classname + '" src="' + Fast.api.cdnurl(value) + '" /></a>');
485 486 487
                    });
                    return html.join(' ');
                },
488 489 490 491
                content: function (value, row, index) {
                    var width = this.width != undefined ? this.width : 250;
                    return "<div style='white-space: nowrap; text-overflow:ellipsis; overflow: hidden; max-width:" + width + "px;'>" + value + "</div>";
                },
492
                status: function (value, row, index) {
493
                    var custom = {normal: 'success', hidden: 'gray', deleted: 'danger', locked: 'info'};
494
                    if (typeof this.custom !== 'undefined') {
495
                        custom = $.extend(custom, this.custom);
Karson authored
496
                    }
497 498 499 500 501 502 503 504 505 506
                    this.custom = custom;
                    this.icon = 'fa fa-circle';
                    return Table.api.formatter.normal.call(this, value, row, index);
                },
                normal: function (value, row, index) {
                    var colorArr = ["primary", "success", "danger", "warning", "info", "gray", "red", "yellow", "aqua", "blue", "navy", "teal", "olive", "lime", "fuchsia", "purple", "maroon"];
                    var custom = {};
                    if (typeof this.custom !== 'undefined') {
                        custom = $.extend(custom, this.custom);
                    }
507
                    value = value === null ? '' : value.toString();
508 509 510 511 512 513 514 515 516
                    var keys = typeof this.searchList === 'object' ? Object.keys(this.searchList) : [];
                    var index = keys.indexOf(value);
                    var color = value && typeof custom[value] !== 'undefined' ? custom[value] : null;
                    var display = index > -1 ? this.searchList[value] : null;
                    var icon = typeof this.icon !== 'undefined' ? this.icon : null;
                    if (!color) {
                        color = index > -1 && typeof colorArr[index] !== 'undefined' ? colorArr[index] : 'primary';
                    }
                    if (!display) {
517
                        display = __(value.charAt(0).toUpperCase() + value.slice(1));
518 519
                    }
                    var html = '<span class="text-' + color + '">' + (icon ? '<i class="' + icon + '"></i> ' : '') + display + '</span>';
520
                    if (this.operate != false) {
521
                        html = '<a href="javascript:;" class="searchit" data-toggle="tooltip" title="' + __('Click to search %s', display) + '" data-field="' + this.field + '" data-value="' + value + '">' + html + '</a>';
522
                    }
Karson authored
523 524
                    return html;
                },
525
                toggle: function (value, row, index) {
526 527
                    var table = this.table;
                    var options = table ? table.bootstrapTable('getOptions') : {};
528 529 530
                    var color = typeof this.color !== 'undefined' ? this.color : 'success';
                    var yes = typeof this.yes !== 'undefined' ? this.yes : 1;
                    var no = typeof this.no !== 'undefined' ? this.no : 0;
531
                    var url = typeof this.url !== 'undefined' ? this.url : '';
532 533 534 535 536
                    var disable = false;
                    if (typeof this.disable !== "undefined") {
                        disable = typeof this.disable === "function" ? this.disable.call(this, value, row, index) : this.disable;
                    }
                    return "<a href='javascript:;' data-toggle='tooltip' title='" + __('Click to toggle') + "' class='btn-change " + (disable ? 'btn disabled' : '') + "' data-id='"
537
                        + row[options.pk] + "' " + (url ? "data-url='" + url + "'" : "") + " data-params='" + this.field + "=" + (value == yes ? no : yes) + "'><i class='fa fa-toggle-on " + (value == yes ? 'text-' + color : 'fa-flip-horizontal text-gray') + " fa-2x'></i></a>";
eviltrue authored
538
                },
Karson authored
539
                url: function (value, row, index) {
540
                    return '<div class="input-group input-group-sm" style="width:250px;margin:0 auto;"><input type="text" class="form-control input-sm" value="' + value + '"><span class="input-group-btn input-group-sm"><a href="' + value + '" target="_blank" class="btn btn-default btn-sm"><i class="fa fa-link"></i></a></span></div>';
Karson authored
541
                },
Karson authored
542
                search: function (value, row, index) {
543 544 545 546 547 548
                    var field = this.field;
                    if (typeof this.customField !== 'undefined' && typeof row[this.customField] !== 'undefined') {
                        value = row[this.customField];
                        field = this.customField;
                    }
                    return '<a href="javascript:;" class="searchit" data-toggle="tooltip" title="' + __('Click to search %s', value) + '" data-field="' + field + '" data-value="' + value + '">' + value + '</a>';
Karson authored
549
                },
550
                addtabs: function (value, row, index) {
551
                    var url = Table.api.replaceurl(this.url, row, this.table);
552
                    var title = this.atitle ? this.atitle : __("Search %s", value);
553
                    return '<a href="' + Fast.api.fixurl(url) + '" class="addtabsit" data-value="' + value + '" title="' + title + '">' + value + '</a>';
Karson authored
554
                },
555
                dialog: function (value, row, index) {
556
                    var url = Table.api.replaceurl(this.url, row, this.table);
557
                    var title = this.atitle ? this.atitle : __("View %s", value);
558 559 560
                    return '<a href="' + Fast.api.fixurl(url) + '" class="dialogit" data-value="' + value + '" title="' + title + '">' + value + '</a>';
                },
                flag: function (value, row, index) {
561
                    var that = this;
562
                    value = value === null ? '' : value.toString();
Karson authored
563
                    var colorArr = {index: 'success', hot: 'warning', recommend: 'danger', 'new': 'info'};
564 565 566
                    //如果字段列有定义custom
                    if (typeof this.custom !== 'undefined') {
                        colorArr = $.extend(colorArr, this.custom);
Karson authored
567
                    }
568
                    var field = this.field;
Karson authored
569 570
                    if (typeof this.customField !== 'undefined' && typeof row[this.customField] !== 'undefined') {
                        value = row[this.customField];
571
                        field = this.customField;
Karson authored
572
                    }
573
Karson authored
574 575
                    //渲染Flag
                    var html = [];
576
                    var arr = value.split(',');
577
                    var color, display, label;
Karson authored
578
                    $.each(arr, function (i, value) {
579
                        value = value === null ? '' : value.toString();
Karson authored
580 581
                        if (value == '')
                            return true;
582 583 584 585 586 587 588 589
                        color = value && typeof colorArr[value] !== 'undefined' ? colorArr[value] : 'primary';
                        display = typeof that.searchList !== 'undefined' && typeof that.searchList[value] !== 'undefined' ? that.searchList[value] : __(value.charAt(0).toUpperCase() + value.slice(1));
                        label = '<span class="label label-' + color + '">' + display + '</span>';
                        if (that.operate) {
                            html.push('<a href="javascript:;" class="searchit" data-toggle="tooltip" title="' + __('Click to search %s', display) + '" data-field="' + field + '" data-value="' + value + '">' + label + '</a>');
                        } else {
                            html.push(label);
                        }
Karson authored
590 591 592
                    });
                    return html.join(' ');
                },
593
                label: function (value, row, index) {
594
                    return Table.api.formatter.flag.call(this, value, row, index);
595
                },
Karson authored
596
                datetime: function (value, row, index) {
597 598 599 600 601 602
                    var datetimeFormat = typeof this.datetimeFormat === 'undefined' ? 'YYYY-MM-DD HH:mm:ss' : this.datetimeFormat;
                    if (isNaN(value)) {
                        return value ? Moment(value).format(datetimeFormat) : __('None');
                    } else {
                        return value ? Moment(parseInt(value) * 1000).format(datetimeFormat) : __('None');
                    }
Karson authored
603
                },
604 605 606 607 608 609
                operate: function (value, row, index) {
                    var table = this.table;
                    // 操作配置
                    var options = table ? table.bootstrapTable('getOptions') : {};
                    // 默认按钮组
                    var buttons = $.extend([], this.buttons || []);
610 611 612 613 614 615
                    // 所有按钮名称
                    var names = [];
                    buttons.forEach(function (item) {
                        names.push(item.name);
                    });
                    if (options.extend.dragsort_url !== '' && names.indexOf('dragsort') === -1) {
616
                        buttons.push(Table.button.dragsort);
617
                    }
618
                    if (options.extend.edit_url !== '' && names.indexOf('edit') === -1) {
619 620
                        Table.button.edit.url = options.extend.edit_url;
                        buttons.push(Table.button.edit);
621
                    }
622
                    if (options.extend.del_url !== '' && names.indexOf('del') === -1) {
623
                        buttons.push(Table.button.del);
624
                    }
625
                    return Table.api.buttonlink(this, buttons, value, row, index, 'operate');
626 627
                }
                ,
628 629 630 631 632 633 634 635 636 637 638
                buttons: function (value, row, index) {
                    // 默认按钮组
                    var buttons = $.extend([], this.buttons || []);
                    return Table.api.buttonlink(this, buttons, value, row, index, 'buttons');
                }
            },
            buttonlink: function (column, buttons, value, row, index, type) {
                var table = column.table;
                type = typeof type === 'undefined' ? 'buttons' : type;
                var options = table ? table.bootstrapTable('getOptions') : {};
                var html = [];
639 640
                var hidden, visible, disable, url, classname, icon, text, title, refresh, confirm, extend,
                    dropdown, link;
641
                var fieldIndex = column.fieldIndex;
642
                var dropdowns = {};
643
644 645
                $.each(buttons, function (i, j) {
                    if (type === 'operate') {
646 647 648
                        if (j.name === 'dragsort' && typeof row[Table.config.dragsortfield] === 'undefined') {
                            return true;
                        }
649 650 651
                        if (['add', 'edit', 'del', 'multi', 'dragsort'].indexOf(j.name) > -1 && !options.extend[j.name + "_url"]) {
                            return true;
                        }
652 653 654
                    }
                    var attr = table.data(type + "-" + j.name);
                    if (typeof attr === 'undefined' || attr) {
655
                        hidden = typeof j.hidden === 'function' ? j.hidden.call(table, row, j) : (typeof j.hidden !== 'undefined' ? j.hidden : false);
Karson authored
656 657 658
                        if (hidden) {
                            return true;
                        }
659
                        visible = typeof j.visible === 'function' ? j.visible.call(table, row, j) : (typeof j.visible !== 'undefined' ? j.visible : true);
Karson authored
660 661 662
                        if (!visible) {
                            return true;
                        }
663
                        dropdown = j.dropdown ? j.dropdown : '';
664
                        url = j.url ? j.url : '';
Karson authored
665
                        url = typeof url === 'function' ? url.call(table, row, j) : (url ? Fast.api.fixurl(Table.api.replaceurl(url, row, table)) : 'javascript:;');
666 667
                        classname = j.classname ? j.classname : 'btn-primary btn-' + name + 'one';
                        icon = j.icon ? j.icon : '';
668 669
                        text = typeof j.text === 'function' ? j.text.call(table, row, j) : j.text ? j.text : '';
                        title = typeof j.title === 'function' ? j.title.call(table, row, j) : j.title ? j.title : text;
670
                        refresh = j.refresh ? 'data-refresh="' + j.refresh + '"' : '';
Ivan authored
671
                        confirm = typeof j.confirm === 'function' ? j.confirm.call(table, row, j) : (typeof j.confirm !== 'undefined' ? j.confirm : false);
672
                        confirm = confirm ? 'data-confirm="' + confirm + '"' : '';
673
                        extend = j.extend ? j.extend : '';
674
                        disable = typeof j.disable === 'function' ? j.disable.call(table, row, j) : (typeof j.disable !== 'undefined' ? j.disable : false);
675 676 677
                        if (disable) {
                            classname = classname + ' disabled';
                        }
678 679 680 681 682 683 684 685 686
                        link = '<a href="' + url + '" class="' + classname + '" ' + (confirm ? confirm + ' ' : '') + (refresh ? refresh + ' ' : '') + extend + ' title="' + title + '" data-table-id="' + (table ? table.attr("id") : '') + '" data-field-index="' + fieldIndex + '" data-row-index="' + index + '" data-button-index="' + i + '"><i class="' + icon + '"></i>' + (text ? ' ' + text : '') + '</a>';
                        if (dropdown) {
                            if (typeof dropdowns[dropdown] == 'undefined') {
                                dropdowns[dropdown] = [];
                            }
                            dropdowns[dropdown].push(link);
                        } else {
                            html.push(link);
                        }
687 688
                    }
                });
689 690 691 692 693 694 695
                if (!$.isEmptyObject(dropdowns)) {
                    var dropdownHtml = [];
                    $.each(dropdowns, function (i, j) {
                        dropdownHtml.push('<div class="btn-group"><button type="button" class="btn btn-primary dropdown-toggle btn-xs" data-toggle="dropdown">' + i + '</button><button type="button" class="btn btn-primary dropdown-toggle btn-xs" data-toggle="dropdown"><span class="caret"></span></button><ul class="dropdown-menu pull-right"><li>' + j.join('</li><li>') + '</li></ul></div>');
                    });
                    html.unshift(dropdownHtml);
                }
696
                return html.join(' ');
Karson authored
697
            },
698
            //替换URL中的数据
699 700 701 702 703 704
            replaceurl: function (url, row, table) {
                var options = table ? table.bootstrapTable('getOptions') : null;
                var ids = options ? row[options.pk] : 0;
                row.ids = ids ? ids : (typeof row.ids !== 'undefined' ? row.ids : 0);
                //自动添加ids参数
                url = !url.match(/\{ids\}/i) ? url + (url.match(/(\?|&)+/) ? "&ids=" : "/ids/") + '{ids}' : url;
705 706 707 708 709 710 711 712 713 714 715 716 717 718
                url = url.replace(/\{(.*?)\}/gi, function (matched) {
                    matched = matched.substring(1, matched.length - 1);
                    if (matched.indexOf(".") !== -1) {
                        var temp = row;
                        var arr = matched.split(/\./);
                        for (var i = 0; i < arr.length; i++) {
                            if (typeof temp[arr[i]] !== 'undefined') {
                                temp = temp[arr[i]];
                            }
                        }
                        return typeof temp === 'object' ? '' : temp;
                    }
                    return row[matched];
                });
719 720
                return url;
            },
Karson authored
721 722
            // 获取选中的条目ID集合
            selectedids: function (table) {
723
                var options = table.bootstrapTable('getOptions');
724 725 726 727 728 729 730 731 732
                if (options.templateView) {
                    return $.map($("input[data-id][name='checkbox']:checked"), function (dom) {
                        return $(dom).data("id");
                    });
                } else {
                    return $.map(table.bootstrapTable('getSelections'), function (row) {
                        return row[options.pk];
                    });
                }
Karson authored
733 734 735 736
            },
            // 切换复选框状态
            toggleattr: function (table) {
                $("input[type='checkbox']", table).trigger('click');
737 738 739 740 741 742
            },
            // 根据行索引获取行数据
            getrowdata: function (table, index) {
                index = parseInt(index);
                var data = table.bootstrapTable('getData');
                return typeof data[index] !== 'undefined' ? data[index] : null;
743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758
            },
            // 根据行索引获取行数据
            getrowbyindex: function (table, index) {
                return Table.api.getrowdata(table, index);
            },
            // 根据主键ID获取行数据
            getrowbyid: function (table, id) {
                var row = {};
                var options = table.bootstrapTable("getOptions");
                $.each(table.bootstrapTable('getData'), function (i, j) {
                    if (j[options.pk] == id) {
                        row = j;
                        return false;
                    }
                });
                return row;
Karson authored
759 760 761 762 763
            }
        },
    };
    return Table;
});