xuhy
2025-01-09 7134287cdccf3d10371bdfc52405acae5a0d7f3b
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
/**
 * 初始化 BootStrap Table 的封装
 *
 * 约定:toolbar的id为 (bstableId + "Toolbar")
 *
 * @author fengshuonan
 */
(function () {
    var BSTable = function (bstableId, url, columns) {
        this.btInstance = null;                    //jquery和BootStrapTable绑定的对象
        this.bstableId = bstableId;
        this.url = Feng.ctxPath + url;
        this.method = "post";
        this.paginationType = "server";            //默认分页方式是服务器分页,可选项"client"
        this.toolbarId = bstableId + "Toolbar";
        this.columns = columns;
        this.height = 665;                        //默认表格高度665
        this.data = {};
        this.queryParams = {}; // 向后台传递的自定义参数
    };
 
    BSTable.prototype = {
        /**
         * 初始化bootstrap table
         */
        init: function () {
            var tableId = this.bstableId;
            var me = this;
            this.btInstance =
                $('#' + tableId).bootstrapTable({
                    contentType: "application/x-www-form-urlencoded",
                    url: this.url,                //请求地址
                    method: this.method,        //ajax方式,post还是get
                    ajaxOptions: {                //ajax请求的附带参数
                        data: this.data
                    },
                    toolbar: "#" + this.toolbarId,//顶部工具条
                    striped: true,                 //是否显示行间隔色
                    cache: false,                  //是否使用缓存,默认为true
                    pagination: true,             //是否显示分页(*)
                    sortable: true,              //是否启用排序
                    sortOrder: "desc",             //排序方式
                    pageNumber: 1,                  //初始化加载第一页,默认第一页
                    pageSize: 14,                  //每页的记录行数(*)
                    pageList: [14, 50, 100],      //可供选择的每页的行数(*)
                    queryParamsType: 'limit',     //默认值为 'limit' ,在默认情况下 传给服务端的参数为:offset,limit,sort
                    queryParams: function (param) {
                        return $.extend(me.queryParams, param);
                    }, // 向后台传递的自定义参数
                    sidePagination: this.paginationType,   //分页方式:client客户端分页,server服务端分页(*)
                    search: false,              //是否显示表格搜索,此搜索是客户端搜索,不会进服务端
                    strictSearch: true,            //设置为 true启用 全匹配搜索,否则为模糊搜索
                    showColumns: true,             //是否显示所有的列
                    showRefresh: true,             //是否显示刷新按钮
                    minimumCountColumns: 2,        //最少允许的列数
                    clickToSelect: true,        //是否启用点击选中行
                    searchOnEnterKey: true,        //设置为 true时,按回车触发搜索方法,否则自动触发搜索方法
                    columns: this.columns,        //列数组
                    pagination: true,            //是否显示分页条
                    height: this.height,
                    icons: {
                        refresh: 'glyphicon-repeat',
                        toggle: 'glyphicon-list-alt',
                        columns: 'glyphicon-list'
                    },
                    iconSize: 'outline'
                });
            return this;
        },
        /**
         * 向后台传递的自定义参数
         * @param param
         */
        setQueryParams: function (param) {
            this.queryParams = param;
        },
        /**
         * 设置分页方式:server 或者 client
         */
        setPaginationType: function (type) {
            this.paginationType = type;
        },
 
        /**
         * 设置ajax post请求时候附带的参数
         */
        set: function (key, value) {
            if (typeof key == "object") {
                for (var i in key) {
                    if (typeof i == "function")
                        continue;
                    this.data[i] = key[i];
                }
            } else {
                this.data[key] = (typeof value == "undefined") ? $("#" + key).val() : value;
            }
            return this;
        },
 
        /**
         * 设置ajax post请求时候附带的参数
         */
        setData: function (data) {
            this.data = data;
            return this;
        },
 
        /**
         * 清空ajax post请求参数
         */
        clear: function () {
            this.data = {};
            return this;
        },
 
        /**
         * 刷新 bootstrap 表格
         * Refresh the remote server data,
         * you can set {silent: true} to refresh the data silently,
         * and set {url: newUrl} to change the url.
         * To supply query params specific to this request, set {query: {foo: 'bar'}}
         */
        refresh: function (parms) {
            if (typeof parms != "undefined") {
                this.btInstance.bootstrapTable('refresh', parms);
            } else {
                this.btInstance.bootstrapTable('refresh');
            }
        }
    };
 
    window.BSTable = BSTable;
 
}());