This repository has been archived by the owner on Sep 21, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 37
/
extjs-ajax.html
89 lines (87 loc) · 2.8 KB
/
extjs-ajax.html
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
<!--
@homepage https://github.com/stevenrskelton/sortable-table
@element extjs-ajax
@demo http://files.stevenskelton.ca/sortable-table/examples/extjs-ajax
-->
<link rel="import" href="../polymer/polymer.html">
<link rel="import" href="../core-ajax/core-ajax.html">
<polymer-element hidden
name="extjs-ajax"
attributes="url withCredentials start length search sortColumn sortDescending filters filterOps columns data dataSize loading"
>
<script>
"use strict";
Polymer({
url: null,
withCredentials: false,
start: 0,
length: -1,
search: "",
sortColumn: null,
sortDescending: false,
filters: [],
filterOps: [{title:'=',op:'eq',html:'='},{title:'<',op:'lt',html:'<'},{title:'>',op:'gt',html:'>'},{title:'In',op:'in',html:'In'}],
columns: [],
ajax: null,
loading: false,
openTransactionCount: 0,
ready: function() {
var ajax = document.createElement('core-ajax');
ajax.handleAs = 'json';
var self = this;
ajax.addEventListener("core-response", function(e){ self.handleResponse.call(self, e); });
this.ajax = ajax;
this.ajax.url = this.url;
this.ajax.withCredentials = this.withCredentials;
this.job('go',this.go);
},
handleResponse: function(e){
var response = e.detail.response;
var columns;
this.dataSize = response.recordsTotal;
var self = this;
if(this.columns.length>0){
this.data = response.data.map(function(currentValue){
var obj = {};
self.columns.forEach(function(column){
obj[column.name] = currentValue[column.name];
});
return obj;
});
}else this.data = response.data;
this.dataSize = +response.total;
this.openTransactionCount--;
if(this.openTransactionCount === 0) this.loading = false;
this.fire('data-updated', { data: this.data, dataSize: this.dataSize });
},
go: function(){
if(this.columns.length>0){
var params = {
_dc: (new Date).getTime(),
page: 0,
start: this.start
};
if(this.length > -1) params.limit = this.length;
if(this.sortColumn){
var dir = this.sortDescending ? 'DESC' : 'ASC';
params.sort = JSON.stringify([{ property: this.sortColumn, direction: dir }]);
}
if(this.filters && this.filters.length > 0){
params.filter = JSON.stringify(this.filters.map(function(filter){
return { "comparison": filter.op, "value":filter.value, "field": filter.column, "type": "numeric" };
}));
}
this.ajax.params = params;
this.openTransactionCount++;
if(!this.loading) this.loading = true;
this.ajax.go();
}
},
columnsChanged: function(){ this.job('go',this.go); },
startChanged: function(){ this.job('go',this.go); },
lengthChanged: function(){ this.job('go',this.go); },
sortColumnChanged: function(){ this.job('go',this.go); },
sortDescendingChanged: function(){ this.job('go',this.go); }
});
</script>
</polymer-element>