多单位版国产化地质资料管理系统
zs
2026-02-04 fe02f176b512a9d6a4e12437d929e04e99bb7567
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
$.extend($.fn.datagrid.defaults, {
    rowHeight: null,
    onBeforeFetch: function(page){},
    onFetch: function(page, rows){}
});
 
var bufferview = $.extend({}, $.fn.datagrid.defaults.view, {
    render: function(target, container, frozen){
        var state = $.data(target, 'datagrid');
        var opts = state.options;
        var rows = this.rows || [];
        if (!rows.length) {
            return;
        }
        var fields = $(target).datagrid('getColumnFields', frozen);
        
        if (frozen){
            if (!(opts.rownumbers || (opts.frozenColumns && opts.frozenColumns.length))){
                return;
            }
        }
        
        var index = parseInt(opts.finder.getTr(target,'','last',(frozen?1:2)).attr('datagrid-row-index'))+1 || 0;
        var table = ['<table class="datagrid-btable" cellspacing="0" cellpadding="0" border="0"><tbody>'];
        for(var i=0; i<rows.length; i++) {
            // get the class and style attributes for this row
            var css = opts.rowStyler ? opts.rowStyler.call(target, index, rows[i]) : '';
            var classValue = '';
            var styleValue = '';
            if (typeof css == 'string'){
                styleValue = css;
            } else if (css){
                classValue = css['class'] || '';
                styleValue = css['style'] || '';
            }
            
            var cls = 'class="datagrid-row ' + (index % 2 && opts.striped ? 'datagrid-row-alt ' : ' ') + classValue + '"';
            var style = styleValue ? 'style="' + styleValue + '"' : '';
            var rowId = state.rowIdPrefix + '-' + (frozen?1:2) + '-' + index;
            table.push('<tr id="' + rowId + '" datagrid-row-index="' + index + '" ' + cls + ' ' + style + '>');
            table.push(this.renderRow.call(this, target, fields, frozen, index, rows[i]));
            table.push('</tr>');
            index++;
        }
        table.push('</tbody></table>');
        
        $(container).append(table.join(''));
    },
    
    onBeforeRender: function(target){
        var state = $.data(target, 'datagrid');
        var opts = state.options;
        var dc = state.dc;
        var view = this;
        this.renderedCount = 0;
        this.rows = [];
        
        dc.body1.add(dc.body2).empty();
        init();
        
        function init(){
            opts.rowHeight = $(target).datagrid('getRowHeight');
            // erase the onLoadSuccess event, make sure it can't be triggered
            state.onLoadSuccess = opts.onLoadSuccess;
            opts.onLoadSuccess = function(){};
            setTimeout(function(){
                dc.body2.unbind('.datagrid').bind('scroll.datagrid', function(e){
                    if (state.onLoadSuccess){
                        opts.onLoadSuccess = state.onLoadSuccess;    // restore the onLoadSuccess event
                        state.onLoadSuccess = undefined;
                    }
                    if (view.scrollTimer){
                        clearTimeout(view.scrollTimer);
                    }
                    view.scrollTimer = setTimeout(function(){
                        scrolling.call(view);
                    }, 50);
                });
                dc.body2.triggerHandler('scroll.datagrid');
            }, 0);
        }
        
        function scrolling(){
            if (getDataHeight() < dc.body2.height() && view.renderedCount < state.data.total){
                this.getRows.call(this, target, function(rows){
                    this.rows = rows;
                    this.populate.call(this, target);
                    dc.body2.triggerHandler('scroll.datagrid');
                });
            } else if (dc.body2.scrollTop() >= getDataHeight() - dc.body2.height()){
                this.getRows.call(this, target, function(rows){
                    this.rows = rows;
                    this.populate.call(this, target);
                });
            }
        }
        
        function getDataHeight(){
            // var h = 0;
            // dc.body2.children('table.datagrid-btable').each(function(){
            //     h += $(this).outerHeight();
            // });
            // if (!h){
            //     h = view.renderedCount * opts.rowHeight;
            // }
            // return h;
            return view.renderedCount * opts.rowHeight;
        }
    },
    
    getRows: function(target, callback){
        var state = $.data(target, 'datagrid');
        var opts = state.options;
        var page = Math.floor(this.renderedCount/opts.pageSize) + 1;
        
        if (this.renderedCount >= state.data.total){return;}
        if (opts.onBeforeFetch.call(target, page) == false){return}
        
//        var rows = state.data.rows.slice(this.renderedCount, this.renderedCount+opts.pageSize);
        var index = (page-1)*opts.pageSize;
        var rows = state.data.rows.slice(index, index+opts.pageSize);
        if (rows.length){
            opts.onFetch.call(target, page, rows);
            callback.call(this, rows);
        } else {
            var param = $.extend({}, opts.queryParams, {
                page: Math.floor(this.renderedCount/opts.pageSize)+1,
                rows: opts.pageSize
            });
            if (opts.sortName){
                $.extend(param, {
                    sort: opts.sortName,
                    order: opts.sortOrder
                });
            }
            if (opts.onBeforeLoad.call(target, param) == false){return;}
            
            $(target).datagrid('loading');
            var result = opts.loader.call(target, param, function(data){
                $(target).datagrid('loaded');
                var data = opts.loadFilter.call(target, data);
                opts.onFetch.call(target, page, data.rows);
                if (data.rows && data.rows.length){
                    state.data.rows = state.data.rows.concat(data.rows);
                    callback.call(opts.view, data.rows);
                } else {
                    opts.onLoadSuccess.call(target, data);
                }
            }, function(){
                $(target).datagrid('loaded');
                opts.onLoadError.apply(target, arguments);
            });
            if (result == false){
                $(target).datagrid('loaded');
                if (!state.data.rows.length){
                    opts.onFetch.call(target, page, state.data.rows);
                    opts.onLoadSuccess.call(target, state.data);
                }
            }
        }
    },
    
    populate: function(target){
        var state = $.data(target, 'datagrid');
        var opts = state.options;
        var dc = state.dc;
        if (this.rows.length){
            this.renderedCount += this.rows.length;
            this.render.call(this, target, dc.body2, false);
            this.render.call(this, target, dc.body1, true);
            opts.onLoadSuccess.call(target, {
                total: state.data.total,
                rows: this.rows
            });
//            for(var i=this.renderedCount-this.rows.length; i<this.renderedCount; i++){
//                $(target).datagrid('fixRowHeight', i);
//            }
        }
    }
});
$.extend($.fn.datagrid.methods, {
    getRowHeight: function(jq){
        var opts = jq.datagrid('options');
        if (!opts.rowHeight){
            var d = $('<div style="position:absolute;top:-1000px;width:100px;height:100px;padding:5px"><table><tr class="datagrid-row"><td>cell</td></tr></table></div>').appendTo('body');
            var rowHeight = d.find('tr').outerHeight();
            d.remove();
            opts.rowHeight = rowHeight;
        }
        return opts.rowHeight;
    }
});