xiaoyong931011
2021-04-12 a13a93a493e7e94e28b2225c26e7e13b52d3288c
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
/**
 * 封装bootstrap-table
 *
 * 1、对bootstrap-table的属性进行一些默认的配置, 2、扩展了对搜索按钮、go按钮 3、处理搜索表单的值添加到bootstrap-table
 * 的默的queryParams中 3.提供一些与表单相关的操作
 * @author 姜友瑶
 * @date 2016-06-24
 */
 
 
//公用表格对象
var MGrid = function () {
 
    "use strict";
 
    return {
 
        /** 初始化表格 */
        initGrid: function (_initParam) {
 
            var tablbeHeaight = function () {
                var height = ($(document).height() - $("#serchform").height() - 70);
                if (height < 100) {
                    height = 300;
                    console.log("小于100")
                }
                return height;
            }
 
            /** 表格默认属性 */
            var option = {
                tableSelecter: "#mgrid",// 表格选择器 请保持唯一
                serchFormSelecter: "#serchform",// 搜索表单选择器
                searchListSelecter: ".search-list",
                striped: true,
                //height:tablbeHeaight(),
                $table: null,// 当前表单的jQuery对象
                pagination: true,// 显示分页栏
                sidePagination: 'server',// 服务器分页
                dataSearch: true,// 显示搜索框
                selectItemName: "keys", // 设置checkbox的值
                pageList: [15, 50,100,200,300,400,500,1000],// 设置分页显示条数下拉控件的值
                showColumns: false,// 显示选择显示列按钮
                showRefresh: false,// 显示刷新按钮
                seniorSerch: ".senior-serch",//高级搜索点击侧滑事件
                serchClose: ".serch-close",//关闭高级搜索框
                showToggle: false,// 显示卡片切换
                showToolbar: false,
                clickToSelect: false,// 点击行时选中
                mutexClick:true,//开启互斥点击
                delUrl: "",// 数据删除地址
                fixedColumns: false,// 是否固定列
                fixedNumber: 0, // 固定列数
                pageSize:15,
 
                queryParams: function (params) {
                    return $.extend(params, MForm
                        .toJson(this.serchFormSelecter));
                },// 添加搜索值
                method: "post",// 数据提交方式
                contentType: "application/x-www-form-urlencoded",
            };
            option = $.extend(option, _initParam);
            var mygrid = new grid(option);
            mygrid.init();
 
            //初始化下拉框切换的选择条件
            var $dropdown_menu=$(".dropdown-menu");
            $dropdown_menu.find("li").click(function(){
                var node=$(this);
                var $search_text= $("#search-text")
                $search_text.attr("name",node.data("field"));
                $dropdown_menu.prev("button").html(node.html()+'<span class="caret "></span>');
 
 
            });
 
 
            return mygrid;
        },
 
        /** 表格序号调用 */
        indexfn: function (value, row, index) {
            return index + 1;
        },
 
        /** 获取url */
        getUrl: function (value, row, index) {
            return "<a href=" + value + " target='blanck' >" + value + "</a>";
        },
        getYk: function (value, row, index) {
            if(value){
                if(value>=0){
                    return '<span style="color: green" >'+'+'+value+'</span>';
                }else{
                    return '<span style="color: red" >'+value+'</span>';
                }
 
            }
        },
        getImage: function (value, row, index) {
            if (!value && value == '') {
                return "";
            }
            return "<img width='80px;' height='80'  src='" + value + "' />";
        },
        /** 获取时间 */
        getTime: function (value, row, index) {
            var temp = "";
            try {
                temp = value
                var date = new Date(temp);
                if (!temp) {
                    return "";
                } else {
                    return date.getFullYear() + "-" + setFomartZero((date.getMonth() + 1))
                        + "-" + setFomartZero(date.getDate()) + " " + setFomartZero(date.getHours())
                        + ":" + setFomartZero(date.getMinutes());
                }
            } catch (e) {
                console.err('MGrid getTime执行失败');
            }
            return temp;
        },
        /** 获取时间 */
        getTimeDD: function (value, row, index) {
            var temp = "";
            try {
                temp = value
                var date = new Date(temp);
                if (!temp) {
                    return "";
                } else {
                    return date.getFullYear() + "-" + setFomartZero((date.getMonth() + 1))
                        + "-" + setFomartZero(date.getDate());
                }
            } catch (e) {
                console.err('MGrid getTime执行失败');
            }
            return temp;
        },
        /** 获取时间 */
        getTimeHHSS: function (value, row, index) {
            var temp = "";
            try {
                temp = value
                var date = new Date(temp);
                if (!temp) {
                    return "";
                } else {
                    return setFomartZero(date.getHours())
                        + ":" + setFomartZero(date.getMinutes());
                }
            } catch (e) {
                console.err('MGrid getTime执行失败');
            }
            return temp;
        },
        goPage: function (node, max, tableId) {
            var $node = $(node);
            var page = $(node).prev("input").val();
            $("#" + tableId).bootstrapTable('selectPage', page);
            var pages = $(node).next(".pagination").find(".page-number");
            //改变分页样式,但是没效果好奇怪
            pages.each(function () {
                var li = $(this);
                li.attr("class", "page-number")
                if (li.find("a").html() == page) {
                    li.attr("class", "page-number active")
                }
            });
 
        }
    }
}();
 
function setFomartZero(date) {
    if (date < 10) {
        return "0" + date;
    }
    return date;
}
 
//私有对象
function grid(option) {
 
    this.initParam = option;
    this.initParam.$table = $(this.initParam.tableSelecter);
    this.initParam.$table.bootstrapTable(this.initParam);
 
    /** 初始化表格 */
    this.init = function () {
        //处理搜索组件
        _table = this;
        _table.initParam.$table.find("li").click(function () {
            $searlist = $(_table.initParam.searchListSelecter);
            $input = $("#" + $(_table.initParam.searchListSelecter).data("for"));
            $button = $(_table.initParam.searchListSelecter).find("button");
            $input.attr("name", $(this).data("field"));
            $button.html($(this).find("a").html() + ' <span class="caret"></span>');
        });
 
        if(_table.initParam.mutexClick){
            //注册互斥点击事件
            _table.initParam.$table.on("click-row.bs.table", function (row, $element, field) {
                _table.initParam.$table.bootstrapTable('uncheckAll')
                _table.initParam.$table.bootstrapTable('check', field[0].rowIndex - 1);
            });
        }
 
 
 
        //高级搜索
        $(_table.initParam.seniorSerch).click(function () {
            $(".senior-content").animate({right: '0'});
        });
        $(_table.initParam.serchClose).click(function () {
            $(".senior-content").animate({right: '-350px'});
        });
        $(_table.initParam.serchFormSelecter).keypress(function (e) {
            if (e.keyCode == 13) {
                e.preventDefault();
                window.event.returnValue = false;
                _table.serchData(1);
                return false;
            }
        });
    };
 
 
    /** 搜索按钮方法
     * page 说明要跳转到的页数
     *  */
    this.serchData = function (page) {
        if (page) {
            this.initParam.$table.bootstrapTable('selectPage', +page);
        } else {
            this.initParam.$table.bootstrapTable('refresh', {
                silent: false
            });
            /*确认后延时关闭高级搜索*/
            setTimeout(function () {
                $(".senior-content").animate({right: '-30%'});
            }, 200)
        }
 
    };
 
    /** 跳转页面 */
    this.goPage = function (node, max, tableId) {
        var page = 1;
        var pageNow = $(node).prev().val();
        if (pageNow > max) {
            page = max;
        } else {
            page = pageNow <= 0 ? "1" : pageNow;
        }
        this.initParam.$table.bootstrapTable('selectPage', +page);
    };
 
    /**
     * 删除表格单条数据
     */
    this.delItem = function (id) {
        var _this = this;
        MTools.handleItem(this.initParam.delUrl + "?keys=" + id, "确认要删除这条数据吗?",
            function () {
                _this.serchData();
            });
    };
 
    /**
     * 批量删除表格单条数据
     */
    this.delItems = function (idFiledName) {
        var count = this.initParam.$table.bootstrapTable('getSelections').length;
        if (count < 1) {
            layer.msg("请选择您要删除的数据", {
                icon: 5
            });
            return false;
        }
        var _this = this;
        MTools.handleItem(this.initParam.delUrl + "?keys="
            + this.getSelectItemsIds(idFiledName), "确认要删除这" + count + "条数据吗?",
            function () {
                _this.serchData();
            });
    };
 
    this.getSelectItem = function () {
 
        var items = this.initParam.$table.bootstrapTable('getSelections');
        if (items.length != 1) {
            if (items.length < 1) {
                layer.msg('请选择一条要操作的数据', {
                    icon: 7
                });
            } else {
                layer.msg('一次只能选择一条数据', {
                    icon: 7
                });
            }
            e.preventDefault();
            window.event.returnValue = false;
            return false;
        } else {
            return items[0];
        }
    };
 
 
    /**
     * 获取被选中项的id 转换为用逗号分开的id返回
     *
     * @param idFiledName
     *            id 字段的字段名称
     */
    this.getSelectItemsIds = function (idFiledName) {
        if (!idFiledName) {
            idFiledName = "id";
        }
        var items = this.initParam.$table.bootstrapTable('getSelections');
        var keys = "";
        for (var i = 0; i < items.length; i++) {
            if (i == items.length - 1) {
                keys += items[i][idFiledName];
            } else {
                keys += items[i][idFiledName] + ",";
            }
        }
        return keys;
    };
 
    /**
     * 返回一个id
     */
    this.getSelectItemId = function (idFiledName) {
        if (!idFiledName) {
            idFiledName = "id";
        }
        var items = this.initParam.$table.bootstrapTable('getSelections');
        if (items.length != 1) {
            if (items.length < 1) {
                layer.msg('请选择一条要操作的数据', {
                    icon: 7
                });
            } else {
                layer.msg('一次只能选择一条数据', {
                    icon: 7
                });
            }
            e.preventDefault();
            window.event.returnValue = false;
            return false;
        } else {
            var keys = "";
            for (var i = 0; i < items.length; i++) {
                if (i == items.length - 1) {
                    keys += items[i][idFiledName];
                } else {
                    keys += items[i][idFiledName] + ",";
                }
            }
            return keys;
        }
    };
 
}