This repository was archived by the owner on Jun 18, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsat.core.js
More file actions
912 lines (825 loc) · 22 KB
/
Copy pathsat.core.js
File metadata and controls
912 lines (825 loc) · 22 KB
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
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
var SATable = {}
SATable.getFormData = function(form) {
var array = $(form).serializeArray()
var data = {}
for (var i in array) {
var it = array[i]
if (data[it.name]) {
if (!(data[it.name] instanceof Array)) {
var oldValue = data[it.name]
data[it.name] = []
data[it.name].push(oldValue)
}
data[it.name].push(it.value)
} else {
data[it.name] = it.value
}
}
return data
}
SATable.getDom = {}
SATable.getDom.Br = function(class_) {
var br = $('<br/>')
if (class_) {
$(br).addClass(class_)
}
return br
}
SATable.getDom.Div = function(class_) {
var div = $('<div></div>')
if (class_) {
$(div).addClass(class_)
}
return div
}
SATable.getDom.Container = function(class_) {
classes = 'container'
if (class_) {
classes += ' ' + class_
}
return SATable.getDom.Div(classes)
}
SATable.getDom.Row = function(class_) {
classes = 'row'
if (class_) {
classes += ' ' + class_
}
return SATable.getDom.Div(classes)
}
SATable.getDom.RowBetween = function(class_) {
classes = 'row justify-content-between'
if (class_) {
classes += ' ' + class_
}
return SATable.getDom.Div(classes)
}
SATable.getDom.BtnGroup = function(class_) {
var div = SATable.getDom.Div('btn-group')
$(div).attr('role', 'group')
if (class_) {
$(div).addClass(class_)
}
return div
}
SATable.getDom.H2 = function(text, class_) {
var h2 = $('<h2></h2>')
if (text) {
$(h2).text(text)
}
if (class_) {
$(h2).addClass(class_)
}
return h2
}
SATable.getDom.H3 = function(text, class_) {
var h3 = $('<h3></h3>')
if (text) {
$(h3).text(text)
}
if (class_) {
$(h3).addClass(class_)
}
return h3
}
SATable.getDom.ButtonOutline = function(name, class_) {
var btn = $('<button type="button" class="btn btn-outline-primary"></button>')
if (name) {
$(btn).text(name)
}
if (class_) {
$(btn).addClass(class_)
}
return btn
}
SATable.getDom.Button = function(name, class_) {
var btn = $('<button type="button" class="btn btn-primary"></button>')
if (name) {
$(btn).text(name)
}
if (class_) {
$(btn).addClass(class_)
}
return btn
}
SATable.getDom.ButtonSubmit = function(name) {
var span = $('<span class="input-group-btn"></span>')
var btn = SATable.getDom.Button(name)
$(span).append(btn)
return span
}
SATable.getDom.A = function(name, href, class_) {
var a = $('<a></a>')
if (name) {
$(a).text(name)
}
if (href) {
$(a).attr('href', href)
}
if (class_) {
$(a).addClass(class_)
}
return a
}
SATable.getDom.AButton = function(name, href, class_) {
var a = SATable.getDom.A(name, 'javascript:void(0)', 'btn btn-primary')
if (href) {
$(a).attr('href', href)
}
if (class_) {
$(a).addClass(class_)
}
return a
}
SATable.getDom.Form = function(class_) {
var form = $('<form class="form" onsubmit="return false;"></form>')
if (class_) {
form.addClass(class_)
}
return form
}
SATable.getDom.Span = function(class_) {
var span = $('<span></span>')
if (class_) {
$(span).addClass(class_)
}
return span
}
SATable.getDom.Label = function(text, class_) {
var label = $('<label></label>')
if (text) {
$(label).text(text)
}
if (class_) {
$(label).addClass(class_)
}
return label
}
SATable.getDom.Input = function(name, placeholder, class_) {
var input = $('<input type="text" class="form-control" name="'+name+'" placeholder="'+placeholder+'"/>')
if (class_) {
$(input).addClass(class_)
}
return input
}
SATable.getDom.InputText = function(name, caption, placeholder, class_) {
var div = SATable.getDom.Div('form-group row')
var label = $('<label class="col-md-3 col-form-label">' + caption + '</label>')
var divInput = SATable.getDom.Div('col-md-9')
var input = $('<input class="form-control" type="text" name="' + name + '"/>')
if (placeholder) {
$(input).attr('placeholder', placeholder)
}
$(divInput).append(input)
$(div).append(label).append(divInput)
if (class_) {
$(div).addClass(class_)
}
return div
}
SATable.getDom.InputSelect = function(name, caption, options, class_) {
var div = SATable.getDom.Div('form-group row')
var label = $('<label class="col-md-3 col-form-label">' + caption + '</label>')
var divInput = SATable.getDom.Div('col-md-9')
var select = $('<select class="form-control">')
if (name) {
$(select).attr('name', name)
}
for (var i in options) {
var it = options[i]
var option = $('<option></option>')
if (it.value) {
$(option).val(it.value)
}
if (it.text) {
$(option).text(it.text)
}
if (it.selected) {
$(option).attr('selected', 'selected')
}
$(select).append(option)
}
$(divInput).append(select)
$(div).append(label).append(divInput)
if (class_) {
$(div).addClass(class_)
}
return div
}
SATable.getDom.Nav = function(class_) {
var nav = $('<nav></nav>')
if (class_) {
$(nav).addClass(class_)
}
return nav
}
SATable.getDom.Ul = function(class_) {
var ul = $('<ul></ul>')
if (class_) {
$(ul).addClass(class_)
}
return ul
}
SATable.getDom.Li = function(class_) {
var li = $('<li></li>')
if (class_) {
$(li).addClass(class_)
}
return li
}
SATable.getDom.Table = function(class_) {
var table = $('<table class="table table-hover"></table>')
if (class_) {
$(table).addClass(class_)
}
return table
}
SATable.getDom.THead = function(class_) {
var tHead = $('<thead></thead>')
if (class_) {
$(tHead).addClass(class_)
}
return tHead
}
SATable.getDom.TBody = function(class_) {
var tBody = $('<tbody></tbody>')
if (class_) {
$(tBody).addClass(class_)
}
return tBody
}
SATable.getDom.Tr = function(class_) {
var tr = $('<tr></tr>')
if (class_) {
$(tr).addClass(class_)
}
return tr
}
SATable.getDom.Td = function(class_) {
var td = $('<td></td>')
if (class_) {
$(td).addClass(class_)
}
return td
}
SATable.getDom.Th = function(class_) {
var th = $('<th></th>')
if (class_) {
$(th).addClass(class_)
}
return th
}
SATable.getDom.IconLoading = function(class_) {
var icon = $('<i class="fa fa-circle-o-notch fa-spin fa-2x fa-fw"></i>')
if (class_) {
$(icon).addClass(class_)
}
return icon
}
SATable.getDom.IconDown = function(class_) {
var icon = $('<i class="fa fa-chevron-circle-down fa-fw"></i>')
if (class_) {
$(icon).addClass(class_)
}
return icon
}
SATable.getDom.IconUp = function(class_) {
var icon = $('<i class="fa fa-chevron-circle-up fa-fw"></i>')
if (class_) {
$(icon).addClass(class_)
}
return icon
}
SATable.getDom.IconLast = function(class_) {
var icon = $('<i class="fa fa-angle-left"></i>')
if (class_) {
$(icon).addClass(class_)
}
return icon
}
SATable.getDom.IconNext = function(class_) {
var icon = $('<i class="fa fa-angle-right"></i>')
if (class_) {
$(icon).addClass(class_)
}
return icon
}
SATable.getDom.IconNoDirection = function(class_) {
var icon = $('<i class="fa fa-minus-circle fa-fw"></i>')
if (class_) {
$(icon).addClass(class_)
}
return icon
}
SATable.getDom.IconYes = function(class_) {
var icon = $('<i class="fa fa-check fa-fw"></i>')
if (class_) {
$(icon).addClass(class_)
}
return icon
}
SATable.getDom.IconNo = function(class_) {
var icon = $('<i class="fa fa-close fa-fw"></i>')
if (class_) {
$(icon).addClass(class_)
}
return icon
}
SATable.getDom.IconQuestion = function(class_) {
var icon = $('<i class="fa fa-question fa-fw"></i>')
if (class_) {
$(icon).addClass(class_)
}
return icon
}
SATable.getDom.IconEdit = function(class_) {
var icon = $('<i class="fa fa-pencil-square-o fa-fw"></i>')
if (class_) {
$(icon).addClass(class_)
}
return icon
}
SATable.getDom.IconDelete = function(class_) {
var icon = $('<i class="fa fa-trash fa-fw"></i>')
if (class_) {
$(icon).addClass(class_)
}
return icon
}
SATable.types = {}
SATable.types.Boolean = function(content, column, info) {
return content ? SATable.getDom.IconYes : SATable.getDom.IconNo
}
SATable.types.Datetime = function(content, column, info) {
var date = new Date(content)
return date.toLocaleString()
}
SATable.types.Link = function(content, column, info) {
var typeInfo = column.typeInfo
var url = typeInfo.base_url + info[typeInfo.key] + '/'
return SATable.getDom.A(content, url)
}
SATable.types.Text = function(content, column, info) {
return content
}
SATable.InputType = {
'text': SATable.getDom.InputText
}
SATable.hasFilter = function() {
return this.filters.length > 0
}
SATable.hasSearch = function() {
return this.search
}
SATable.createFilterForm = function() {
var self = this
var getDom = SATable.getDom
var divContainer = getDom.Container('jumbotron bg-faded collapse')
var formFilter = getDom.Form()
for (var i in self.filters) {
var filter = self.filters[i]
var input = SATable.InputType[filter.type](filter.name, filter.caption, filter.placeholder)
$(formFilter).append(input)
}
var btnSubmit = getDom.ButtonSubmit('筛 选')
$(formFilter).append(getDom.Br()).append(btnSubmit)
$(divContainer).attr('id', self.tableIDs.filterDiv).append(formFilter)
self.dom.filter.form = formFilter
self.dom.filter.div = divContainer
self.dom.filter.submit = btnSubmit
$(btnSubmit).click(function() {
self.update()
})
return divContainer
}
SATable.createHead = function() {
var self = this
var getDom = SATable.getDom
var divBetween = getDom.RowBetween()
var divTitle = getDom.Div()
var h2Title = getDom.H2(self.title)
var divBtn = getDom.BtnGroup()
$(divTitle).addClass('col-6').append(h2Title)
if (self.hasFilter()) {
var btnFilter = getDom.ButtonOutline('筛选')
$(btnFilter).attr('data-toggle', 'collapse').attr('data-target', '#' + self.tableIDs.filterDiv)
$(divBtn).append(btnFilter)
self.dom.filter.button = btnFilter
}
if (self.createURL != null) {
var btnCreate = getDom.AButton('创建', self.createURL)
$(divBtn).append(btnCreate)
}
$(divBetween).append(divTitle).append(divBtn)
return divBetween
}
SATable.createBarUp = function() {
var self = this
var getDom = SATable.getDom
var divBetween = getDom.RowBetween()
var divSearch = getDom.Div('col-md-5 col-sm-12')
var divPage = getDom.Div('col-md-6 col-sm-12')
$(divBetween).append(divSearch).append(divPage)
var nav = getDom.Nav()
var ul = getDom.Ul('pagination ' + self.tableIDs.pageClass)
$(nav).attr('style', 'float:right;').append(ul)
$(divPage).append(nav)
self.dom.pagination.up = {}
self.dom.pagination.up.div = divPage
self.dom.pagination.up.nav = nav
self.dom.pagination.up.ul = ul
if (self.search) {
var form = getDom.Form()
var divInput = getDom.Div('form-group row')
var divInput2 = getDom.Div('col-xs-12 input-group')
var input = getDom.Input('search', '搜索')
var span = getDom.Span('input-group-btn')
var button = getDom.Button('')
var i = $('<i class="fa fa-search"></i>')
$(button).append(i)
$(span).append(button)
$(divInput2).append(input).append(span)
$(divInput).append(divInput2)
$(form).append(divInput)
$(divSearch).append(form)
self.dom.search.form = form
self.dom.search.input = input
self.dom.search.submit = button
$(form).submit(function() {
self.update()
})
$(button).click(function() {
self.update()
})
}
return divBetween
}
SATable.getLimit = function() {
var self = this
var pageNumberData = SATable.getFormData(self.dom.pageNumber.form)
var pageNumber = pageNumberData.pageNumber
return pageNumber
}
SATable.createBarBottom = function() {
var self = this
var getDom = SATable.getDom
var divBetween = getDom.RowBetween()
var divPageNumber = getDom.Div('col-md-5')
var divPage = $(self.dom.pagination.up.div).clone(true)
$(divBetween).append(divPageNumber).append(divPage)
self.dom.pagination.bottom = {}
self.dom.pagination.bottom.div = divPage
var formPageNumber = getDom.Form()
var divSelect = getDom.InputSelect('pageNumber', '每页', [
{ text: '1', value: '1' },
{ text: '2', value: '2' },
{ text: '5', value: '5' },
{ text: '10', value: '10' },
{ text: '20', value: '20', selected: true },
{ text: '50', value: '50' },
{ text: '100', value: '100' },
{ text: '200', value: '200' }
])
$(formPageNumber).append(divSelect)
$(divPageNumber).append(formPageNumber)
self.dom.pageNumber.div = divPageNumber
self.dom.pageNumber.form = formPageNumber
$(formPageNumber).change(function() {
self.limit = self.getLimit()
self.update()
})
return divBetween
}
SATable.createTable = function() {
var self = this
var getDom = SATable.getDom
var div = getDom.Div()
var table = getDom.Table()
var tHead = getDom.THead()
var tBody = getDom.TBody()
var tr = getDom.Tr()
$(tHead).append(tr)
$(table).append(tHead).append(tBody)
$(div).append(table)
self.dom.table.head = tHead
self.dom.table.headTr = tr
self.dom.table.body = tBody
return div
}
SATable.initTable = function() {
var self = this
var getDom = SATable.getDom
var divContainer = getDom.Container()
// head
$(divContainer).append(self.createHead())
if (self.hasFilter()) {
$(divContainer).append(getDom.Br()).append(self.createFilterForm())
} else {
$(divContainer).append(getDom.Br())
}
// bar up
$(divContainer).append(self.createBarUp())
// table
$(divContainer).append(self.createTable())
// bar bottom
%(divContainer).append(self.createBarBottom())
// 每页显示结果数量
self.limit = self.getLimit()
$('#' + self.id).append(divContainer)
}
SATable.initData = function(tableInfo) {
var self = this
self.id = tableInfo.id
self.title = tableInfo.title
self.columns = tableInfo.columns ? tableInfo.columns : []
self.filters = tableInfo.filters ? tableInfo.filters : []
self.search = tableInfo.search ? tableInfo.search : false
self.requestGenerator = tableInfo.requestGenerator
self.dataGenerator = tableInfo.dataGenerator
self.page = tableInfo.page ? 1 : tableInfo.page
self.createURL = tableInfo.createURL ? tableInfo.createURL : null
self.extraData = tableInfo.extraData ? tableInfo.extraData : {}
self.curPage = 1
var cols = self.columns
for (var i in cols) {
var col = cols[i]
if (col.sort && (col.ordering != -1 && col.ordering != 0 && col.ordering != 1)) {
col.ordering = 0
}
}
var tableIDs = {}
tableIDs.id = self.id + 'SAT'
tableIDs.filterBtn = tableIDs.id + 'FB'
tableIDs.filterDiv = tableIDs.id + 'FD'
tableIDs.filterForm = tableIDs.id + 'F'
self.tableIDs = tableIDs
var dom = {}
dom.filter = {}
dom.search = {}
dom.pagination = {}
dom.table = {}
dom.pageNumber = {}
self.dom = dom
}
SATable.getPaginationLi = function(page, curPage) {
var getDom = this.getDom
var li = getDom.Li('page-item')
var a = getDom.A(page, 'javascript:void(0)', 'page-link')
$(li).append(a)
if (page == curPage) {
$(li).addClass('active')
}
return li
}
SATable.getPaginationLiIgnore = function() {
var getDom = SATable.getDom
var li = getDom.Li('page-item disabled')
var a = getDom.A('…', 'javascript:void(0)', 'page-link')
$(li).append(a)
return li
}
SATable.fillPagination = function(data) {
var self = this
var getDom = SATable.getDom
var curPage = self.curPage
var page_max = Math.ceil(data.count / self.limit)
var liLast = getDom.Li('page-item page-prev')
var liNext = getDom.Li('page-item page-next')
var aLast = getDom.A(i, 'javascript:void(0)', 'page-link')
var aNext = $(aLast).clone()
$(aLast).append(getDom.IconLast())
$(aNext).append(getDom.IconNext())
$(liLast).append(aLast)
$(liNext).append(aNext)
var ul = self.dom.pagination.up.ul
$(ul).append(liLast)
if (page_max <= 7) {
for (var i = 1; i <= page_max; ++i) {
ul.append(SATable.getPaginationLi(i, curPage))
}
} else if (curPage <= 3) {
for (var i = 1; i <= 4; ++i) {
ul.append(SATable.getPaginationLi(i, curPage))
}
ul.append(SATable.getPaginationLiIgnore)
ul.append(SATable.getPaginationLi(page_max, curPage))
} else if (curPage >= page_max - 2) {
ul.append(SATable.getPaginationLi(1, curPage))
ul.append(SATable.getPaginationLiIgnore)
for (var i = page_max - 3; i <= page_max; ++i) {
ul.append(SATable.getPaginationLi(i, curPage))
}
} else {
ul.append(SATable.getPaginationLi(1, curPage))
ul.append(SATable.getPaginationLiIgnore)
ul.append(SATable.getPaginationLi(curPage - 1, curPage))
ul.append(SATable.getPaginationLi(curPage, curPage))
ul.append(SATable.getPaginationLi(curPage + 1, curPage))
ul.append(SATable.getPaginationLiIgnore)
ul.append(SATable.getPaginationLi(page_max, curPage))
}
$(ul).append(liNext)
if (curPage == 1) {
$(liLast).addClass('disabled')
}
if (curPage == page_max) {
$(liNext).addClass('disabled')
}
var buttonsDom = $(self.dom.pagination.up.ul).children(':not(.page-prev):not(.page-next):not(.disabled)').find('.page-link')
$(buttonsDom).click(function() {
var number = parseInt($(this).text())
self.curPage = number
if (curPage != number) {
self.update()
}
})
$(aLast).click(function() {
if (curPage > 1) {
self.curPage -= 1
self.update()
}
})
$(aNext).click(function() {
if (curPage < page_max) {
self.curPage += 1
self.update()
}
})
var navBottom = $(self.dom.pagination.up.nav).clone(true)
$(self.dom.pagination.bottom.div).append(navBottom)
}
SATable.flushPagination = function() {
var self = this
$(self.dom.pagination.up.ul).empty()
$(self.dom.pagination.bottom.div).empty()
}
SATable.flushTable = function() {
$(this.dom.table.body).empty()
$(this.dom.table.headTr).empty()
}
SATable.setLoading = function() {
var self = this
var getDom = SATable.getDom
self.flushTable()
var div = getDom.Div('row justify-content-sm-center jumbotron bg-faded')
var divContent = getDom.Div('col-1')
var icon = getDom.IconLoading()
$(divContent).append(icon)
$(div).append(divContent)
$(self.dom.table.headTr).append(div)
}
SATable.fillTableHead = function() {
var self = this
var getDom = SATable.getDom
var columns = self.columns
var headTr = self.dom.table.headTr
for(var i in columns) {
var column = columns[i]
// 名称
var th = getDom.Th()
$(th).text(column.caption)
// 分析排序
if (column.sort) {
var icon = null
if (column.ordering == 1) { // 升序
icon = getDom.IconUp()
} else if (column.ordering == -1) { // 降序
icon = getDom.IconDown()
} else { // 无序
icon = getDom.IconNoDirection()
}
var link = $('<a class="ordering" href="javascript:void(0)"></a>')
$(link).append(icon)
$(th).append(link)
}
$(headTr).append(th)
}
$(headTr).find('a.ordering').click(function() {
var index = $(this).parent().index()
var column = self.columns[index]
column.ordering += 1
if (column.ordering > 1) {
column.ordering = -1
}
self.update()
})
}
SATable.fillTableBody = function(data) {
var self = this
var getDom = SATable.getDom
var types = SATable.types
var body = self.dom.table.body
for (var i in data) {
var obj = data[i]
var tr = getDom.Tr()
var columns = self.columns
for (var j in columns) {
var column = columns[j]
var content = obj[column.name]
var ret = content
if (types[column.type]) {
ret = types[column.type](content, column, obj)
}
var td = getDom.Td()
$(td).append(ret)
$(tr).append(td)
}
$(body).append(tr)
}
}
SATable.fillTable = function(data) {
var self = this
self.flushPagination()
self.fillPagination(data)
self.flushTable()
self.fillTableHead()
self.fillTableBody(data.data)
}
SATable.getRequest = function() {
var self = this
var dom = self.dom
var request = {}
// 生成筛选信息
if (self.hasFilter()) {
var filterData = SATable.getFormData(dom.filter.form)
var filters = {}
for (var i in filterData) {
var it = filterData[i]
if (it) {
filters[i] = it
}
}
request.filters = filters
}
// 生成搜索信息
if (self.hasSearch()) {
var searchData = SATable.getFormData(dom.search.form)
var search = searchData.search
if (search) {
request.search = search
}
}
// 生成排序信息
var ordering = []
var columns = self.columns
for (var i in columns) {
var column = columns[i]
if (column.sort && column.ordering != 0) {
ordering.push((column.ordering == 1 ? '' : '-') + column.name)
}
}
if (ordering.length > 0) {
request.ordering = ordering
}
// 获得每页显示结果数量
request.limit = self.limit
// 获得欲显示的页数
var curPage = self.curPage
request.page = curPage ? curPage : 1
return request
}
SATable.update = function() {
var self = this
var getDom = getDom
var dom = self.dom
// 显示载入界面
self.setLoading()
// 获得数据
var request = self.requestGenerator(self.getRequest())
$.ajax({
type: request.type,
url: request.url,
dataType: request.dataType,
data: request.data,
success: function(ret) {
var data = self.dataGenerator(ret)
self.fillTable(data)
}
})
}
SATable.SimpleAjaxTable = function(tableInfo) {
saTable = {
hasFilter: SATable.hasFilter,
hasSearch: SATable.hasSearch,
createFilterForm: SATable.createFilterForm,
createHead: SATable.createHead,
createBarUp: SATable.createBarUp,
createBarBottom: SATable.createBarBottom,
createTable: SATable.createTable,
getLimit: SATable.getLimit,
fillTableHead: SATable.fillTableHead,
fillTableBody: SATable.fillTableBody,
fillTable: SATable.fillTable,
flushTable: SATable.flushTable,
fillPagination: SATable.fillPagination,
flushPagination: SATable.flushPagination,
getRequest: SATable.getRequest,
initTable: SATable.initTable,
initData: SATable.initData,
setLoading: SATable.setLoading,
update: SATable.update,
}
saTable.initData(tableInfo)
saTable.initTable()
saTable.update()
return saTable
}