-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbootstrap-table.js
161 lines (127 loc) · 4.08 KB
/
bootstrap-table.js
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
(function($){
$.table = function(el, options){
// To avoid scope issues, use 'base' instead of 'this'
// to reference this class from internal events and functions.
var base = this;
base.error = false;
base.message
base.message = {
nourl : "<strong>Oh snap!</strong> We couldn't finde a url to load this table.",
loadFail : "<strong>Oh snap!</strong> The table failes to load from the remote url.",
requestError : "<strong>Oh snap!</strong> The server responded incorectaly.",
noRecords : "<strong>Heads up!</strong> No records where found"
};
//the number of columns in this table
base.th;
base.url;
base.requestType = 'POST';
base.parameters;
base.data = {};
// Access to jQuery and DOM versions of element
base.$el = $(el);
base.el = el;
// Add a reverse reference to the DOM object
base.$el.data("table", base);
base.init = function(){
base.options = $.extend({},$.table.defaultOptions, options);
base.th = base.$el.find('thead th');
base.url = base.$el.attr('url');
var params = base.$el.attr('parameters');
if(params != null){
base.data = jQuery.parseJSON('{' + params + '}');
}
if(base.url == null){
base.error = true;
base.errorMessage(base.message.nourl);
}
//if there are no errors load the table
if(!base.error){
base.initHead();
base.load();
}
//console.log(base.el.attributes);
};
base.initHead = function(){
$th = base.$el.find('thead th');
$th.click(function(){
var derection = $(this).attr("data-derection");
$(this).attr("data-derection","ASC");
//$(this).find('i').removeClass("icon-arrow-up icon-arrow-up").addClass("icon-arrow-down");
base.load();
});
}
// Sample Function, Uncomment to use
base.load = function(){
var request = $.ajax({
context: base,
type: 'POST',
url: base.url,
data: base.data,
dataType: 'json',
success: function(data) {
if(!data){
base.errorMessage(base.message.requestError);
return;
}
if(!data.success){
( data.message ) ? base.errorMessage(data.message) : base.errorMessage(base.message.requestError);
return;
}
if(data.records <= 0) {
base.noticeMessage(base.message.noRecords);
return;
}
base.processRecords(data.data);
}
});
request.fail(function(jqXHR, textStatus) {
base.errorMessage(base.message.loadFail);
});
};
base.processRecords = function(records){
base.$el.find('tbody tr').remove();
for (var i in records) {
base.createRow(1, records[i]);
}
};
base.createRow = function(dataId, record){
var rowHtml = '<tr row-id="' + dataId + '">';
base.th.each(function(key, value){
var id = $(this).attr("data-id") || key;
var value = record[id] || ' ';
rowHtml = rowHtml + '<td>' + value + '</td>';
});
rowHtml = rowHtml + '</tr>';
base.$el.find('tbody:last').append(rowHtml);
};
base.noticeMessage = function(message){
var columnCount = base.th.length;
base.$el.find('tbody tr').remove();
base.$el.find('tbody:last').append('<tr class="alert-info"><td colspan="' + columnCount + '">' + message + '</td></tr>');
}
base.errorMessage = function(message){
var columnCount = base.th.length;
base.$el.find('tbody tr').remove();
base.$el.find('tbody:last').append('<tr class="alert-error"><td colspan="' + columnCount + '">' + message + '</td></tr>');
}
// Run initializer
base.init();
};
$.table.defaultOptions = {
initSelector: "table[data-role='table']"
};
$.fn.table = function(options){
return this.each(function(){
(new $.table(this, options));
});
//return (new $.table(this, options));
};
//auto self-init
$(document).ready(function(){
//$("table[data-role='table']").each(function(index) {
// var a = $(this).table();
// console.log(a);
//})
$("table[data-role='table']").table();
});
})(jQuery);