Subversion-Projekte lars-tiefland.cienc

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
9 lars 1
var $table = $('#table'),
2
        $remove = $('#remove'),
3
        selections = [];
4
 
5
    function initTable() {
6
        $table.bootstrapTable({
7
            height: getHeight(),
8
            columns: [
9
                [
10
                    {
11
                        field: 'state',
12
                        checkbox: true,
13
                        rowspan: 2,
14
                        align: 'center',
15
                        valign: 'middle'
16
                    }, {
17
                        title: 'Item ID',
18
                        field: 'id',
19
                        rowspan: 2,
20
                        align: 'center',
21
                        valign: 'middle',
22
                        sortable: true,
23
                        footerFormatter: totalTextFormatter
24
                    }, {
25
                        title: 'Item Detail',
26
                        colspan: 3,
27
                        align: 'center'
28
                    }
29
                ],
30
                [
31
                    {
32
                        field: 'name',
33
                        title: 'Item Name',
34
                        sortable: true,
35
                        editable: true,
36
                        footerFormatter: totalNameFormatter,
37
                        align: 'center'
38
                    }, {
39
                        field: 'price',
40
                        title: 'Item Price',
41
                        sortable: true,
42
                        align: 'center',
43
                        editable: {
44
                            type: 'text',
45
                            title: 'Item Price',
46
                            validate: function (value) {
47
                                value = $.trim(value);
48
                                if (!value) {
49
                                    return 'This field is required';
50
                                }
51
                                if (!/^$/.test(value)) {
52
                                    return 'This field needs to start width $.'
53
                                }
54
                                var data = $table.bootstrapTable('getData'),
55
                                    index = $(this).parents('tr').data('index');
56
                                console.log(data[index]);
57
                                return '';
58
                            }
59
                        },
60
                        footerFormatter: totalPriceFormatter
61
                    }, {
62
                        field: 'operate',
63
                        title: 'Item Operate',
64
                        align: 'center',
65
                        events: operateEvents,
66
                        formatter: operateFormatter
67
                    }
68
                ]
69
            ]
70
        });
71
        // sometimes footer render error.
72
        setTimeout(function () {
73
            $table.bootstrapTable('resetView');
74
        }, 200);
75
        $table.on('check.bs.table uncheck.bs.table ' +
76
                'check-all.bs.table uncheck-all.bs.table', function () {
77
            $remove.prop('disabled', !$table.bootstrapTable('getSelections').length);
78
 
79
            // save your data, here just save the current page
80
            selections = getIdSelections();
81
            // push or splice the selections if you want to save all data selections
82
        });
83
        $table.on('expand-row.bs.table', function (e, index, row, $detail) {
84
            if (index % 2 == 1) {
85
                $detail.html('Loading from ajax request...');
86
                $.get('LICENSE', function (res) {
87
                    $detail.html(res.replace(/\n/g, '<br>'));
88
                });
89
            }
90
        });
91
        $table.on('all.bs.table', function (e, name, args) {
92
            console.log(name, args);
93
        });
94
        $remove.click(function () {
95
            var ids = getIdSelections();
96
            $table.bootstrapTable('remove', {
97
                field: 'id',
98
                values: ids
99
            });
100
            $remove.prop('disabled', true);
101
        });
102
        $(window).resize(function () {
103
            $table.bootstrapTable('resetView', {
104
                height: getHeight()
105
            });
106
        });
107
    }
108
 
109
    function getIdSelections() {
110
        return $.map($table.bootstrapTable('getSelections'), function (row) {
111
            return row.id
112
        });
113
    }
114
 
115
    function responseHandler(res) {
116
        $.each(res.rows, function (i, row) {
117
            row.state = $.inArray(row.id, selections) !== -1;
118
        });
119
        return res;
120
    }
121
 
122
    function detailFormatter(index, row) {
123
        var html = [];
124
        $.each(row, function (key, value) {
125
            html.push('<p><b>' + key + ':</b> ' + value + '</p>');
126
        });
127
        return html.join('');
128
    }
129
 
130
    function operateFormatter(value, row, index) {
131
        return [
132
            '<a class="like" href="javascript:void(0)" title="Like">',
133
            '<i class="glyphicon glyphicon-heart"></i>',
134
            '</a>  ',
135
            '<a class="remove" href="javascript:void(0)" title="Remove">',
136
            '<i class="glyphicon glyphicon-remove"></i>',
137
            '</a>'
138
        ].join('');
139
    }
140
 
141
    window.operateEvents = {
142
        'click .like': function (e, value, row, index) {
143
            alert('You click like action, row: ' + JSON.stringify(row));
144
        },
145
        'click .remove': function (e, value, row, index) {
146
            $table.bootstrapTable('remove', {
147
                field: 'id',
148
                values: [row.id]
149
            });
150
        }
151
    };
152
 
153
    function totalTextFormatter(data) {
154
        return 'Total';
155
    }
156
 
157
    function totalNameFormatter(data) {
158
        return data.length;
159
    }
160
 
161
    function totalPriceFormatter(data) {
162
        var total = 0;
163
        $.each(data, function (i, row) {
164
            total += +(row.price.substring(1));
165
        });
166
        return '$' + total;
167
    }
168
 
169
    function getHeight() {
170
        return $(window).height() - $('h1').outerHeight(true);
171
    }
172
 
173
    $(function () {
174
        var scripts = [
175
                location.search.substring(1) || 'assets/bootstrap-table/src/bootstrap-table.js',
176
                'assets/bootstrap-table/src/extensions/export/bootstrap-table-export.js',
177
                'http://rawgit.com/hhurz/tableExport.jquery.plugin/master/tableExport.js',
178
                'assets/bootstrap-table/src/extensions/editable/bootstrap-table-editable.js',
179
                'http://rawgit.com/vitalets/x-editable/master/dist/bootstrap3-editable/js/bootstrap-editable.js'
180
            ],
181
            eachSeries = function (arr, iterator, callback) {
182
                callback = callback || function () {};
183
                if (!arr.length) {
184
                    return callback();
185
                }
186
                var completed = 0;
187
                var iterate = function () {
188
                    iterator(arr[completed], function (err) {
189
                        if (err) {
190
                            callback(err);
191
                            callback = function () {};
192
                        }
193
                        else {
194
                            completed += 1;
195
                            if (completed >= arr.length) {
196
                                callback(null);
197
                            }
198
                            else {
199
                                iterate();
200
                            }
201
                        }
202
                    });
203
                };
204
                iterate();
205
            };
206
 
207
        eachSeries(scripts, getScript, initTable);
208
    });
209
 
210
    function getScript(url, callback) {
211
        var head = document.getElementsByTagName('head')[0];
212
        var script = document.createElement('script');
213
        script.src = url;
214
 
215
        var done = false;
216
        // Attach handlers for all browsers
217
        script.onload = script.onreadystatechange = function() {
218
            if (!done && (!this.readyState ||
219
                    this.readyState == 'loaded' || this.readyState == 'complete')) {
220
                done = true;
221
                if (callback)
222
                    callback();
223
 
224
                // Handle memory leak in IE
225
                script.onload = script.onreadystatechange = null;
226
            }
227
        };
228
 
229
        head.appendChild(script);
230
 
231
        // We handle everything using the script element injection
232
        return undefined;
233
    }