Statement.js 48.1 KB
Newer Older
U
unknown 已提交
1 2 3 4 5
MWF.xApplication.query = MWF.xApplication.query || {};
MWF.xApplication.query.Query = MWF.xApplication.query.Query || {};
MWF.xDesktop.requireApp("query.Query", "Viewer", null, false);
MWF.xApplication.query.Query.Statement = MWF.QStatement = new Class({
    Extends: MWF.QViewer,
U
unknown 已提交
6 7 8 9
    options: {
        "moduleEvents": ["queryLoad", "postLoad", "postLoadPageData", "postLoadPage", "selectRow", "unselectRow",
            "queryLoadItemRow", "postLoadItemRow", "queryLoadCategoryRow", "postLoadCategoryRow", "export", "exportRow"]
    },
10
    initialize: function (container, json, options, app, parentMacro) {
U
unknown 已提交
11 12 13 14 15 16 17 18
        //本类有三种事件,
        //一种是通过 options 传进来的事件,包括 loadView、openDocument、select
        //一种是用户配置的 事件, 在this.options.moduleEvents 中定义的作为类事件
        //还有一种也是用户配置的事件,不在this.options.moduleEvents 中定义的作为 this.node 的DOM事件

        this.setOptions(options);

        this.path = "../x_component_query_Query/$Viewer/";
19
        this.cssPath = "../x_component_query_Query/$Viewer/" + this.options.style + "/css.wcss";
U
unknown 已提交
20 21 22 23 24 25
        this._loadCss();
        this.lp = MWF.xApplication.query.Query.LP;

        this.app = app;

        this.container = $(container);
U
unknown 已提交
26 27

        debugger;
U
unknown 已提交
28
        this.json = json || {};
U
unknown 已提交
29 30 31 32 33 34 35 36 37 38 39 40 41 42

        this.parentMacro = parentMacro;

        this.originalJson = Object.clone(json);

        this.viewJson = null;
        this.filterItems = [];
        this.searchStatus = "none"; //none, custom, default


        this.items = [];
        this.selectedItems = [];
        this.hideColumns = [];
        this.openColumns = [];
U
unknown 已提交
43
        this.parameter = {};
U
unknown 已提交
44 45 46

        this.gridJson = null;

47 48
        if (this.options.isload) {
            this.init(function () {
U
unknown 已提交
49 50 51 52 53
                this.load();
            }.bind(this));
        }

    },
54 55
    init: function (callback) {
        if (this.json.view) {
U
unknown 已提交
56
            this.viewJson = JSON.decode(this.json.view);
U
unknown 已提交
57
            this.statementJson = this.json;
U
unknown 已提交
58
            this.statementJson.viewJson = this.viewJson;
U
unknown 已提交
59
            if (callback) callback();
60
        } else {
U
unknown 已提交
61 62 63 64 65 66 67 68 69
            this.getView(callback);
        }
    },
    loadMacro: function (callback) {
        MWF.require("MWF.xScript.Macro", function () {
            this.Macro = new MWF.Macro.ViewContext(this);
            if (callback) callback();
        }.bind(this));
    },
70
    createActionbarNode: function () {
U
unknown 已提交
71
        this.actionbarAreaNode.empty();
72 73 74
        if (typeOf(this.json.showActionbar) === "boolean" && this.json.showActionbar !== true) return;
        if (typeOf(this.viewJson.actionbarHidden) === "boolean") {
            if (this.viewJson.actionbarHidden === true || !this.viewJson.actionbarList || !this.viewJson.actionbarList.length) return;
U
unknown 已提交
75 76 77 78
            this.actionbar = new MWF.xApplication.query.Query.Statement.Actionbar(this.actionbarAreaNode, this.viewJson.actionbarList[0], this, {});
            this.actionbar.load();
        }
    },
79
    _loadPageNode: function () {
U
unknown 已提交
80
        this.viewPageAreaNode.empty();
81
        if (!this.paging) {
U
unknown 已提交
82
            var json;
83
            if (!this.viewJson.pagingList || !this.viewJson.pagingList.length) {
U
unknown 已提交
84 85 86 87
                json = {
                    "firstPageText": this.lp.firstPage,
                    "lastPageText": this.lp.lastPage
                };
88
            } else {
U
unknown 已提交
89 90 91 92
                json = this.viewJson.pagingList[0];
            }
            this.paging = new MWF.xApplication.query.Query.Statement.Paging(this.viewPageAreaNode, json, this, {});
            this.paging.load();
93
        } else {
U
unknown 已提交
94 95 96
            this.paging.reload();
        }
    },
U
unknown 已提交
97 98 99 100 101 102
    // _initPage: function(){
    //     var i = this.count/this.json.pageSize;
    //     this.pages = (i.toInt()<i) ? i.toInt()+1 : i;
    //     this.currentPage = this.options.defaultPage || 1;
    //     this.options.defaultPage = null;
    // },
103 104
    lookup: function (data, callback) {
        if (this.lookuping) return;
U
unknown 已提交
105
        this.lookuping = true;
U
unknown 已提交
106 107
        // this.getLookupAction(function(){
        //     if (this.json.application){
U
unknown 已提交
108

U
unknown 已提交
109 110 111 112
        var d = data || {};
        // d.count = this.json.count;
        // this.lookupAction.bundleView(this.json.id, d, function(json){
        //     this.bundleItems = json.data.valueList;
U
unknown 已提交
113

U
unknown 已提交
114
        // this._initPage();
U
unknown 已提交
115

U
unknown 已提交
116
        debugger;
117 118
        this.loadParameter(d);
        this.loadFilter(d);
U
unknown 已提交
119 120 121 122

        this.currentPage = this.options.defaultPage || 1;
        this.options.defaultPage = null;

123 124 125
        if (this.noDataTextNode) this.noDataTextNode.destroy();
        this.loadCurrentPageData(function (json) {
            if (this.count) {
U
unknown 已提交
126 127
                this.fireEvent("postLoad"); //用户配置的事件
                this.lookuping = false;
128 129
                if (callback) callback(this);
            } else {
U
unknown 已提交
130
                this.viewPageAreaNode.empty();
131
                if (this.viewJson.noDataText) {
U
unknown 已提交
132
                    var noDataTextNodeStyle = this.css.noDataTextNode;
133
                    if (this.viewJson.viewStyles && this.viewJson.viewStyles["noDataTextNode"]) {
U
unknown 已提交
134 135
                        noDataTextNodeStyle = this.viewJson.viewStyles["noDataTextNode"];
                    }
136
                    this.noDataTextNode = new Element("div", {
U
unknown 已提交
137
                        "styles": noDataTextNodeStyle,
138 139
                        "text": this.viewJson.noDataText
                    }).inject(this.contentAreaNode);
U
unknown 已提交
140 141 142 143 144 145 146
                }
                // if (this.loadingAreaNode){
                //     this.loadingAreaNode.destroy();
                //     this.loadingAreaNode = null;
                // }
                this.fireEvent("postLoad"); //用户配置的事件
                this.lookuping = false;
147
                if (callback) callback(this);
U
unknown 已提交
148 149 150
            }


151
        }.bind(this), true, "all");
U
unknown 已提交
152 153

        // }.bind(this));
U
unknown 已提交
154 155 156
        // }
        // }.bind(this));
    },
157
    loadFilter: function (data) {
U
unknown 已提交
158
        debugger;
U
unknown 已提交
159
        this.filterList = [];
160
        (data.filterList || []).each(function (d) {
U
unknown 已提交
161
            var parameterName = d.path.replace(/\./g, "_");
U
unknown 已提交
162
            var value = d.value;
U
unknown 已提交
163 164 165
            // if( d.code && d.code.code ){
            //     value = this.Macro.exec( d.code.code, this);
            // }
166 167 168 169 170 171 172 173 174 175 176
            if (d.comparison === "like" || d.comparison === "notLike") {
                if (value.substr(0, 1) !== "%") value = "%" + value;
                if (value.substr(value.length - 1, 1) !== "%") value = value + "%";
                this.parameter[parameterName] = value; //"%"+value+"%";
            } else {
                if (d.formatType === "dateTimeValue" || d.formatType === "datetimeValue") {
                    value = "{ts '" + value + "'}"
                } else if (d.formatType === "dateValue") {
                    value = "{d '" + value + "'}"
                } else if (d.formatType === "timeValue") {
                    value = "{t '" + value + "'}"
U
unknown 已提交
177
                }
178
                this.parameter[parameterName] = value;
U
unknown 已提交
179 180 181
            }
            d.value = parameterName;

182
            this.filterList.push(d);
U
unknown 已提交
183 184
        }.bind(this))
    },
185
    loadParameter: function () {
U
unknown 已提交
186
        this.parameter = {};
U
unknown 已提交
187
        debugger;
U
unknown 已提交
188
        var parameter = this.json.parameter ? Object.clone(this.json.parameter) : {};
U
unknown 已提交
189
        //系统默认的参数
190
        (this.viewJson.parameterList || []).each(function (f) {
U
unknown 已提交
191
            var value = f.value;
192 193 194
            if (parameter && parameter[f.parameter]) {
                value = parameter[f.parameter];
                delete parameter[f.parameter];
U
unknown 已提交
195
            }
196
            if (typeOf(value) === "date") {
U
unknown 已提交
197 198
                value = value.format("db");
            }
199 200 201
            if (f.valueType === "script") {
                value = this.Macro.exec(f.valueScript ? f.valueScript.code : "", this);
            } else {
U
unknown 已提交
202
                var user = layout.user;
203
                switch (f.value) {
U
unknown 已提交
204
                    case "@person":
U
unknown 已提交
205
                        value = user.distinguishedName;
U
unknown 已提交
206 207
                        break;
                    case "@identityList":
208 209
                        value = user.identityList.map(function (d) {
                            return d.distinguishedName;
U
unknown 已提交
210
                        });
U
unknown 已提交
211 212
                        break;
                    case "@unitList":
213 214
                        o2.Actions.load("x_organization_assemble_express").UnitAction.listWithPerson({"personList": [user.distinguishedName]}, function (json) {
                            value = json.unitList;
U
unknown 已提交
215
                        }, null, false);
U
unknown 已提交
216 217
                        break;
                    case "@unitAllList":
218 219
                        o2.Actions.load("x_organization_assemble_express").UnitAction.listWithIdentitySupNested({"personList": [user.distinguishedName]}, function (json) {
                            value = json.unitList;
U
unknown 已提交
220
                        }, null, false);
U
unknown 已提交
221 222 223 224 225
                        break;
                    case "@year":
                        value = (new Date().getFullYear()).toString();
                        break;
                    case "@season":
U
unknown 已提交
226
                        var m = new Date().format("%m");
227
                        if (["01", "02", "03"].contains(m)) {
U
unknown 已提交
228
                            value = "1"
229
                        } else if (["04", "05", "06"].contains(m)) {
U
unknown 已提交
230
                            value = "2"
231
                        } else if (["07", "08", "09"].contains(m)) {
U
unknown 已提交
232
                            value = "3"
233
                        } else {
U
unknown 已提交
234 235
                            value = "4"
                        }
U
unknown 已提交
236 237 238 239 240 241 242 243
                        break;
                    case "@month":
                        value = new Date().format("%Y-%m");
                        break;
                    case "@time":
                        value = new Date().format("db");
                        break;
                    case "@date":
U
unknown 已提交
244
                        value = new Date().format("%Y-%m-%d");
U
unknown 已提交
245 246 247 248
                        break;
                    default:
                }
            }
249 250 251 252 253 254
            if (f.formatType === "dateTimeValue" || f.formatType === "datetimeValue") {
                value = "{ts '" + value + "'}"
            } else if (f.formatType === "dateValue") {
                value = "{d '" + value + "'}"
            } else if (f.formatType === "timeValue") {
                value = "{t '" + value + "'}"
U
unknown 已提交
255
            }
256
            this.parameter[f.parameter] = value;
U
unknown 已提交
257 258
        }.bind(this));
        //传入的参数
259
        for (var p in parameter) {
U
unknown 已提交
260
            var value = parameter[p];
261 262
            if (typeOf(value) === "date") {
                value = "{ts '" + value.format("db") + "'}"
U
unknown 已提交
263
            }
264
            this.parameter[p] = value;
U
unknown 已提交
265
        }
U
unknown 已提交
266
    },
267
    loadCurrentPageData: function (callback, async, type) {
U
unknown 已提交
268 269
        //是否需要在翻页的时候清空之前的items ?

U
unknown 已提交
270 271
        debugger;

272
        if (this.pageloading) return;
U
unknown 已提交
273 274 275 276 277
        this.pageloading = true;

        this.items = [];

        var p = this.currentPage;
U
unknown 已提交
278
        var d = {
279 280
            "filterList": this.filterList,
            "parameter": this.parameter
U
unknown 已提交
281
        };
U
unknown 已提交
282

283
        while (this.viewTable.rows.length > 1) {
U
unknown 已提交
284 285 286 287
            this.viewTable.deleteRow(-1);
        }
        //this.createLoadding();

U
unknown 已提交
288
        this.loadViewRes = o2.Actions.load("x_query_assemble_surface").StatementAction.executeV2(
U
unknown 已提交
289
            this.options.statementId || this.options.statementName || this.json.statementId || this.json.statementName,
290
            type || "data", p, this.json.pageSize, d, function (json) {
U
unknown 已提交
291

292 293
                if (type === "all" || type === "count") {
                    if (typeOf(json.count) === "number") {
U
unknown 已提交
294
                        this.count = json.count;
295 296
                        var i = this.count / this.json.pageSize;
                        this.pages = (i.toInt() < i) ? i.toInt() + 1 : i;
U
unknown 已提交
297 298 299 300
                    }
                }

                this.gridJson = json.data;
U
unknown 已提交
301
                this.setSelectedableFlag();
U
unknown 已提交
302

U
unknown 已提交
303
                this.fireEvent("postLoadPageData");
U
unknown 已提交
304

U
unknown 已提交
305 306 307 308
                // if (this.viewJson.group.column){
                //     this.gridJson = json.data.groupGrid;
                // }else{
                //     this.gridJson = json.data.grid;
U
unknown 已提交
309
                this.loadData();
U
unknown 已提交
310 311
                // }
                if (this.gridJson.length) this._loadPageNode();
312
                if (this.loadingAreaNode) {
U
unknown 已提交
313 314 315
                    this.loadingAreaNode.destroy();
                    this.loadingAreaNode = null;
                }
U
unknown 已提交
316

U
unknown 已提交
317
                this.pageloading = false;
U
unknown 已提交
318

U
unknown 已提交
319 320
                this.fireEvent("loadView"); //options 传入的事件
                this.fireEvent("postLoadPage");
U
unknown 已提交
321

322 323
                if (callback) callback(json);
            }.bind(this), null, async === false ? false : true);
U
unknown 已提交
324
    },
325 326
    getView: function (callback) {
        this.getViewRes = o2.Actions.load("x_query_assemble_surface").StatementAction.get(this.json.statementId || this.json.statementName, function (json) {
U
unknown 已提交
327
            debugger;
U
unknown 已提交
328
            var viewData = JSON.decode(json.data.view);
329
            if (!this.json.pageSize) this.json.pageSize = viewData.pageSize || "20";
U
unknown 已提交
330
            this.viewJson = viewData.data;
U
unknown 已提交
331 332 333
            this.json.application = json.data.query;
            //this.json = Object.merge(this.json, json.data);
            this.statementJson = json.data;
U
unknown 已提交
334
            this.statementJson.viewJson = this.viewJson;
U
unknown 已提交
335 336 337
            if (callback) callback();
        }.bind(this));
    },
U
unknown 已提交
338

339 340 341
    loadData: function () {
        if (this.getSelectFlag() === "multi" && this.viewJson.allowSelectAll) {
            if (this.selectTitleCell && this.selectTitleCell.retrieve("selectAllLoaded")) {
U
unknown 已提交
342
                this.setUnSelectAllStyle();
343
            } else {
U
unknown 已提交
344 345
                this.createSelectAllNode();
            }
346
        } else if (this.selectAllNode) {
U
unknown 已提交
347 348 349
            this.clearSelectAllStyle();
        }

350
        if (this.gridJson.length) {
U
unknown 已提交
351
            // if( !this.options.paging ){
352
            this.gridJson.each(function (line, i) {
U
unknown 已提交
353 354 355 356 357
                this.items.push(new MWF.xApplication.query.Query.Statement.Item(this, line, null, i));
            }.bind(this));
            // }else{
            //     this.loadPaging();
            // }
358
        } else {
U
unknown 已提交
359 360 361
            if (this.viewPageAreaNode) this.viewPageAreaNode.empty();
        }
    },
362 363 364 365 366
    loadDataByPaging: function () {
        if (this.isItemsLoading) return;
        if (!this.isItemsLoaded) {
            var from = Math.min(this.pageNumber * this.options.perPageCount, this.gridJson.length);
            var to = Math.min((this.pageNumber + 1) * this.options.perPageCount + 1, this.gridJson.length);
U
unknown 已提交
367
            this.isItemsLoading = true;
368
            for (var i = from; i < to; i++) {
U
unknown 已提交
369 370 371
                this.items.push(new MWF.xApplication.query.Query.Statement.Item(this, this.gridJson[i], null, i));
            }
            this.isItemsLoading = false;
372 373
            this.pageNumber++;
            if (to == this.gridJson.length) this.isItemsLoaded = true;
U
unknown 已提交
374 375
        }
    },
376
    getFilter: function () {
U
unknown 已提交
377
        var filterData = [];
378 379 380
        if (this.searchStatus === "custom") {
            if (this.filterItems.length) {
                this.filterItems.each(function (filter) {
U
unknown 已提交
381 382 383 384
                    filterData.push(filter.data);
                }.bind(this));
            }
        }
385
        if (this.searchStatus === "default") {
U
unknown 已提交
386
            var key = this.viewSearchInputNode.get("value");
387 388 389
            if (key && key !== this.lp.searchKeywork) {
                this.viewJson.customFilterList.each(function (entry) {
                    if (entry.formatType === "textValue") {
U
unknown 已提交
390 391 392 393
                        var d = {
                            "path": entry.path,
                            "value": key,
                            "formatType": entry.formatType,
U
unknown 已提交
394
                            "logic": "or",
U
unknown 已提交
395 396 397 398
                            "comparison": "like"
                        };
                        filterData.push(d);
                    }
399
                    if (entry.formatType === "numberValue") {
U
unknown 已提交
400
                        var v = key.toFloat();
401
                        if (!isNaN(v)) {
U
unknown 已提交
402 403 404 405
                            var d = {
                                "path": entry.path,
                                "value": v,
                                "formatType": entry.formatType,
U
unknown 已提交
406
                                "logic": "or",
U
unknown 已提交
407 408 409 410 411 412 413 414 415 416
                                "comparison": "like"
                            };
                            filterData.push(d);
                        }
                    }
                }.bind(this));
            }
        }
        return (filterData.length) ? filterData : null;
    },
417
    viewSearchCustomAddToFilter: function () {
U
unknown 已提交
418 419
        var pathIdx = this.viewSearchCustomPathListNode.selectedIndex;
        var comparisonIdx = this.viewSearchCustomComparisonListNode.selectedIndex;
420 421 422 423 424
        if (pathIdx === -1) {
            MWF.xDesktop.notice("error", {
                "x": "left",
                "y": "top"
            }, this.lp.filterErrorTitle, this.viewSearchCustomPathListNode, {"x": 0, "y": 85});
U
unknown 已提交
425 426
            return false;
        }
427 428 429 430 431
        if (comparisonIdx === -1) {
            MWF.xDesktop.notice("error", {
                "x": "left",
                "y": "top"
            }, this.lp.filterErrorComparison, this.viewSearchCustomComparisonListNode, {"x": 0, "y": 85});
U
unknown 已提交
432 433 434 435
            return false;
        }
        var pathOption = this.viewSearchCustomPathListNode.options[pathIdx];
        var entry = pathOption.retrieve("entry");
436
        if (entry) {
U
unknown 已提交
437 438 439 440 441 442
            var pathTitle = entry.title;
            var path = entry.path;
            var comparison = this.viewSearchCustomComparisonListNode.options[comparisonIdx].get("value");
            var comparisonTitle = this.viewSearchCustomComparisonListNode.options[comparisonIdx].get("text");
            var value = "";

443
            if (entry.valueType === "script" && entry.valueScript && entry.valueScript.code) {
U
unknown 已提交
444
                var idx = this.viewSearchCustomValueNode.selectedIndex;
445
                if (idx !== -1) {
U
unknown 已提交
446
                    var v = this.viewSearchCustomValueNode.options[idx].get("value");
447
                    value = entry.formatType === "booleanValue" ? (v === "true") : v;
U
unknown 已提交
448
                }
449 450
            } else {
                switch (entry.formatType) {
U
unknown 已提交
451 452 453 454 455 456 457 458
                    case "numberValue":
                        value = this.viewSearchCustomValueNode.get("value");
                        break;
                    case "dateTimeValue":
                        value = this.viewSearchCustomValueNode.get("value");
                        break;
                    case "booleanValue":
                        var idx = this.viewSearchCustomValueNode.selectedIndex;
459
                        if (idx !== -1) {
U
unknown 已提交
460
                            var v = this.viewSearchCustomValueNode.options[idx].get("value");
461
                            value = (v === "true");
U
unknown 已提交
462 463 464 465 466 467 468
                        }
                        break;
                    default:
                        value = this.viewSearchCustomValueNode.get("value");
                }
            }

469 470 471 472 473
            if (value === "") {
                MWF.xDesktop.notice("error", {
                    "x": "left",
                    "y": "top"
                }, this.lp.filterErrorValue, this.viewSearchCustomValueContentNode, {"x": 0, "y": 85});
U
unknown 已提交
474 475 476 477 478 479 480 481 482 483
                return false;
            }

            this.filterItems.push(new MWF.xApplication.query.Query.Statement.Filter(this, {
                "logic": "and",
                "path": path,
                "title": pathTitle,
                "comparison": comparison,
                "comparisonTitle": comparisonTitle,
                "value": value,
484
                "formatType": (entry.formatType == "datetimeValue") ? "dateTimeValue" : entry.formatType
U
unknown 已提交
485 486 487 488
            }, this.viewSearchCustomFilterContentNode));

            this.searchCustomView();
        }
U
unknown 已提交
489
    },
U
unknown 已提交
490
    //搜索相关结束
491
    getStatementInfor: function () {
U
unknown 已提交
492 493 494
        debugger;
        return this.statementJson;
    },
495
    getPageInfor: function () {
U
unknown 已提交
496
        return {
497 498 499
            pages: this.pages,
            perPageCount: this.json.pageSize,
            currentPageNumber: this.currentPage
U
unknown 已提交
500
        };
U
unknown 已提交
501
    },
502
    switchStatement: function (json) {
U
unknown 已提交
503
        this.switchView(json);
U
unknown 已提交
504
    },
505 506 507 508
    setFilter: function (filter, parameter, callback) {
        if (this.lookuping || this.pageloading) return;
        if (!filter) filter = [];
        if (!parameter) parameter = {};
U
unknown 已提交
509 510
        this.json.filter = filter;
        this.json.parameter = parameter;
511 512
        if (this.viewAreaNode) {
            this.createViewNode({"filterList": this.json.filter.clone()}, callback);
U
unknown 已提交
513
        }
U
unknown 已提交
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
    },


    // getExportTotalCount: function(){
    //     return this.count || 0;
    // },
    // getExportMaxCount: function(){
    //     return 2000;
    // },
    exportView: function(){

        // var excelName = this.statementJson.name + "(" + start + "-" + end + ").xlsx";

        var excelName = this.statementJson.name + ".xlsx";

        var p = this.currentPage;
        var d = {
            "filterList": this.filterList,
            "parameter": this.parameter
        };

        this.createLoadding();

        debugger;

        var exportArray = [];

        var titleArray = [];
        var colWidthArr = [];
        var dateIndexArray = [];
        var idx = 0;
        Object.each(this.entries, function (c, k) {
            if (this.hideColumns.indexOf(k) === -1) {
                titleArray.push(c.displayName);

                colWidthArr.push(c.exportWidth || 200);
                if( c.isTime )dateIndexArray.push(idx);
                idx++;
            }
        }.bind(this));
        exportArray.push(titleArray);

        o2.Actions.load("x_query_assemble_surface").StatementAction.executeV2(
            this.options.statementId || this.options.statementName || this.json.statementId || this.json.statementName,
            "data", 1, 100000, d, function (json) {

                json.data.each(function (d, i) {
                    var dataArray = [];
                    Object.each(this.entries, function (c, k) {
                        if (this.hideColumns.indexOf(k) === -1) {
                            var text = this.getExportText(c, k, d);
                            dataArray.push( text );
                        }
                    }.bind(this));
                    //exportRow事件
                    var argu = {"index":i, "source": d, "data":dataArray};
                    this.fireEvent("exportRow", [argu]);
                    exportArray.push( argu.data || dataArray );
                }.bind(this));

                //export事件
                var arg = {
                    data : exportArray,
                    colWidthArray : colWidthArr,
                    title : excelName
                };
                this.fireEvent("export", [arg]);

                if (this.loadingAreaNode) {
                    this.loadingAreaNode.destroy();
                    this.loadingAreaNode = null;
                }

                new MWF.xApplication.query.Query.Statement.ExcelUtils().exportToExcel(
                    arg.data || exportArray,
                    arg.title || excelName,
                    arg.colWidthArray || colWidthArr,
                    dateIndexArray  //日期格式列下标
                );

            }.bind(this));
    },
    getDataByPath: function (obj, path) {
        var pathList = path.split(".");
        for (var i = 0; i < pathList.length; i++) {
            var p = pathList[i];
            if ((/(^[1-9]\d*$)/.test(p))) p = p.toInt();
            if (obj[p]) {
                obj = obj[p];
            } else {
                obj = "";
                break;
            }
        }
        return obj
    },
    getExportText: function (c, k, data) {
        var path = c.path, code = c.code, obj = data;
        if (!path) {
            return ""
        } else if (path === "$all") {
        } else {
            obj = this.getDataByPath(obj, path);
        }

        try{
            if (code && code.trim()) obj = this.view.Macro.exec(code, {
                "value": obj,
                "data": data,
                "entry": c,
                "json": c
            });
        }catch (e) {}

        var toName = function (value) {
            if (typeOf(value) === "array") {
                Array.each(value, function (v, idx) {
                    value[idx] = toName(v)
                })
            } else if (typeOf(value) === "object") {
                Object.each(value, function (v, key) {
                    value[key] = toName(v);
                })
            } else if (typeOf(value) === "string") {
                value = o2.name.cn(value)
            }
            return value;
        };

        var d;
        if (obj != undefined && obj != null) {
            if (typeOf(obj) === "array") {
                d = c.isName ? JSON.stringify(toName(Array.clone(obj))) : JSON.stringify(obj);
            } else if (typeOf(obj) === "object") {
                d = c.isName ? JSON.stringify(toName(Object.clone(obj))) : JSON.stringify(obj);
            } else {
                d = c.isName ? o2.name.cn(obj.toString()) : obj;
            }
        }
        return d;
U
unknown 已提交
654
    }
U
unknown 已提交
655 656 657
});

MWF.xApplication.query.Query.Statement.Item = new Class({
658 659
    Extends: MWF.xApplication.query.Query.Viewer.Item,
    initialize: function (view, data, prev, i, category) {
U
unknown 已提交
660 661
        this.view = view;
        this.data = data;
662
        this.dataString = JSON.stringify(data);
U
unknown 已提交
663 664
        this.css = this.view.css;
        this.isSelected = false;
U
unknown 已提交
665
        this.category = category;
U
unknown 已提交
666 667 668 669 670 671
        this.prev = prev;
        this.idx = i;
        this.clazzType = "item";

        this.load();
    },
672
    load: function () {
U
unknown 已提交
673 674 675
        this.view.fireEvent("queryLoadItemRow", [null, this]);

        var viewStyles = this.view.viewJson.viewStyles;
676
        var viewContentTdNode = (viewStyles && viewStyles["contentTd"]) ? viewStyles["contentTd"] : this.css.viewContentTdNode;
U
unknown 已提交
677 678

        this.node = new Element("tr", {
679
            "styles": (viewStyles && viewStyles["contentTr"]) ? viewStyles["contentTr"] : this.css.viewContentTrNode
U
unknown 已提交
680
        });
681
        if (this.prev) {
U
unknown 已提交
682
            this.node.inject(this.prev.node, "after");
683
        } else {
U
unknown 已提交
684 685 686 687
            this.node.inject(this.view.viewTable);
        }

        //if (this.view.json.select==="single" || this.view.json.select==="multi"){
688
        this.selectTd = new Element("td", {"styles": viewContentTdNode}).inject(this.node);
U
unknown 已提交
689 690
        this.selectTd.setStyles({"cursor": "pointer"});
        if (this.view.json.itemStyles) this.selectTd.setStyles(this.view.json.itemStyles);
U
unknown 已提交
691 692 693

        //var selectFlag = this.view.json.select || this.view.viewJson.select ||  "none";
        var selectFlag = this.view.getSelectFlag();
694
        if (this.data.$selectedEnable && ["multi", "single"].contains(selectFlag) && this.view.viewJson.selectBoxShow === "always") {
U
unknown 已提交
695 696 697 698 699 700 701 702 703 704 705 706 707 708
            var viewStyles = this.view.viewJson.viewStyles;
            if (viewStyles) {
                if (selectFlag === "single") {
                    this.selectTd.setStyles(viewStyles["radioNode"]);
                } else {
                    this.selectTd.setStyles(viewStyles["checkboxNode"]);
                }
            } else {
                var iconName = "checkbox";
                if (selectFlag === "single") iconName = "radiobox";
                this.selectTd.setStyles({"background": "url(" + "../x_component_query_Query/$Viewer/default/icon/" + iconName + ".png) center center no-repeat"});
            }
        }

709
        if (this.view.isSelectTdHidden()) {
U
unknown 已提交
710 711 712 713 714
            this.selectTd.hide();
        }
        //}

        //序号
715
        if (this.view.viewJson.isSequence === "yes") {
U
unknown 已提交
716 717
            this.sequenceTd = new Element("td", {"styles": viewContentTdNode}).inject(this.node);
            this.sequenceTd.setStyle("width", "10px");
718
            var s = 1 + this.view.json.pageSize * (this.view.currentPage - 1) + this.idx;
U
unknown 已提交
719 720 721 722 723
            this.sequenceTd.set("text", s);
        }

        debugger;

724
        Object.each(this.view.entries, function (c, k) {
U
unknown 已提交
725
            //if (cell){
726
            if (this.view.hideColumns.indexOf(k) === -1) {
U
unknown 已提交
727
                var td = new Element("td", {"styles": viewContentTdNode}).inject(this.node);
U
unknown 已提交
728

729
                var cell = this.getText(c, k, td); //this.data[k];
U
unknown 已提交
730 731 732 733
                if (cell === undefined || cell === null) cell = "";

                // if (k!== this.view.viewJson.group.column){
                var v = cell;
734
                if (c.isHtml) {
U
unknown 已提交
735
                    td.set("html", v);
736
                } else {
U
unknown 已提交
737
                    td.set("text", v);
U
unknown 已提交
738 739
                }

740
                if (typeOf(c.contentProperties) === "object") td.setProperties(c.contentProperties);
U
unknown 已提交
741
                if (this.view.json.itemStyles) td.setStyles(this.view.json.itemStyles);
742
                if (typeOf(c.contentStyles) === "object") td.setStyles(c.contentStyles);
U
unknown 已提交
743 744 745 746
                // }else{
                //     if (this.view.json.itemStyles) td.setStyles(this.view.json.itemStyles);
                // }

747
                if (this.view.openColumns.indexOf(k) !== -1) {
U
unknown 已提交
748 749 750
                    this.setOpenWork(td, c)
                }

U
unknown 已提交
751
                // if (k!== this.view.viewJson.group.column){
752 753 754 755 756 757 758
                Object.each(c.events || {}, function (e, key) {
                    if (e.code) {
                        if (key === "loadContent") {
                            this.view.Macro.fire(e.code,
                                {"node": td, "json": c, "data": v, "view": this.view, "row": this});
                        } else if (key !== "loadTitle") {
                            td.addEvent(key, function (event) {
U
unknown 已提交
759 760
                                return this.view.Macro.fire(
                                    e.code,
761
                                    {"node": td, "json": c, "data": v, "view": this.view, "row": this},
U
unknown 已提交
762 763 764
                                    event
                                );
                            }.bind(this));
U
unknown 已提交
765
                        }
U
unknown 已提交
766 767 768
                    }
                }.bind(this));
                // }
U
unknown 已提交
769 770 771 772 773
            }
            //}
        }.bind(this));

        //默认选中
774 775
        var selectedFlag;

U
unknown 已提交
776
        var defaultSelectedScript = this.view.json.defaultSelectedScript || this.view.viewJson.defaultSelectedScript;
777
        if (!this.isSelected && defaultSelectedScript) {
U
unknown 已提交
778 779 780 781
            // var flag = this.view.json.select || this.view.viewJson.select ||  "none";
            // if ( flag ==="single" || flag==="multi"){
            //
            // }
782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802
            selectedFlag = this.view.Macro.exec(defaultSelectedScript,
                {"node": this.node, "data": this.data, "view": this.view, "row": this});
        }

        //判断是不是在selectedItems中,用户手工选择
        if (!this.isSelected && this.view.selectedItems.length) {
            for (var i = 0; i < this.view.selectedItems.length; i++) {
                if (this.view.selectedItems[i].dataString === this.dataString) {
                    selectedFlag = "true";
                    break;
                }
            }
        }

        if (selectedFlag) {
            if (selectedFlag === "multi" || selectedFlag === "single") {
                this.select(selectedFlag);
            } else if (selectedFlag.toString() === "true") {
                var f = this.view.json.select || this.view.viewJson.select || "none";
                if (f === "single" || f === "multi") {
                    this.select();
U
unknown 已提交
803 804 805 806 807 808 809 810
                }
            }
        }

        this.setEvent();

        this.view.fireEvent("postLoadItemRow", [null, this]);
    },
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
    selected: function( from ){
        for(var i=0; i<this.view.selectedItems.length; i++){
            var item = this.view.selectedItems[i];
            if( item.dataString === this.dataString ){
                this.view.selectedItems.erase(item);
                break;
            }
        }
        this.view.selectedItems.push(this);
        var viewStyles = this.view.viewJson.viewStyles;
        if( viewStyles ){
            this.selectTd.setStyles( viewStyles["checkedCheckboxNode"] );
            this.node.setStyles( viewStyles["contentSelectedTr"] );
        }else{
            this.selectTd.setStyles({"background": "url("+"../x_component_query_Query/$Viewer/default/icon/checkbox_checked.png) center center no-repeat"});
            this.node.setStyles(this.css.viewContentTrNode_selected);
        }
        this.isSelected = true;
        if( from !== "view" && from !=="category" && this.view.viewJson.allowSelectAll ){
            this.view.checkSelectAllStatus();
            if( this.category )this.category.checkSelectAllStatus();
        }
        this.view.fireEvent("selectRow", [this]);
    },
    unSelected: function( from ){
        for(var i=0; i<this.view.selectedItems.length; i++){
            var item = this.view.selectedItems[i];
            if( item.dataString === this.dataString ){
                this.view.selectedItems.erase(item);
                break;
            }
        }
        var viewStyles = this.view.viewJson.viewStyles;
        if( this.view.viewJson.selectBoxShow !=="always" ){
            this.selectTd.setStyles({"background": "transparent"});
        }else{
            if (viewStyles) {
                this.selectTd.setStyles(viewStyles["checkboxNode"]);
            }else{
                this.selectTd.setStyles({"background": "url(" + "../x_component_query_Query/$Viewer/default/icon/checkbox.png) center center no-repeat"});
            }
        }
        if( viewStyles ){
            this.node.setStyles( viewStyles["contentTr"] );
        }else{
            this.node.setStyles(this.css.viewContentTrNode);
        }
        this.isSelected = false;
        if( from !== "view" && from !=="category" && this.view.viewJson.allowSelectAll ){
            this.view.checkSelectAllStatus();
            if( this.category )this.category.checkSelectAllStatus();
        }
        this.view.fireEvent("unselectRow", [this]);
    },
    getDataByPath: function (obj, path) {
U
unknown 已提交
866 867 868 869 870 871 872 873 874 875 876 877 878
        var pathList = path.split(".");
        for (var i = 0; i < pathList.length; i++) {
            var p = pathList[i];
            if ((/(^[1-9]\d*$)/.test(p))) p = p.toInt();
            if (obj[p]) {
                obj = obj[p];
            } else {
                obj = "";
                break;
            }
        }
        return obj
    },
879
    getText: function (c, k, td) {
U
unknown 已提交
880
        var path = c.path, code = c.code, obj = this.data;
881
        if (!path) {
U
unknown 已提交
882
            return ""
883 884
        } else if (path === "$all") {
        } else {
U
unknown 已提交
885
            obj = this.getDataByPath(obj, path);
U
unknown 已提交
886 887
        }

888 889 890 891 892 893 894 895
        if (code && code.trim()) obj = this.view.Macro.exec(code, {
            "value": obj,
            "data": this.data,
            "entry": c,
            "node": td,
            "json": c,
            "row": this
        });
U
unknown 已提交
896 897

        var toName = function (value) {
898 899
            if (typeOf(value) === "array") {
                Array.each(value, function (v, idx) {
U
unknown 已提交
900 901
                    value[idx] = toName(v)
                })
902 903
            } else if (typeOf(value) === "object") {
                Object.each(value, function (v, key) {
U
unknown 已提交
904 905
                    value[key] = toName(v);
                })
906 907
            } else if (typeOf(value) === "string") {
                value = o2.name.cn(value)
U
unknown 已提交
908 909 910 911 912
            }
            return value;
        };

        var d;
913 914
        if (obj != undefined && obj != null) {
            if (typeOf(obj) === "array") {
U
unknown 已提交
915
                d = c.isName ? JSON.stringify(toName(Array.clone(obj))) : JSON.stringify(obj);
916
            } else if (typeOf(obj) === "object") {
U
unknown 已提交
917
                d = c.isName ? JSON.stringify(toName(Object.clone(obj))) : JSON.stringify(obj);
918 919
            } else {
                d = c.isName ? o2.name.cn(obj.toString()) : obj;
U
unknown 已提交
920 921 922 923 924
            }
        }

        return d;
    },
925
    setOpenWork: function (td, column) {
U
unknown 已提交
926
        td.setStyle("cursor", "pointer");
927 928
        if (column.clickCode) {
            if (!this.view.Macro) {
U
unknown 已提交
929 930
                MWF.require("MWF.xScript.Macro", function () {
                    this.view.businessData = {};
931
                    this.view.Macro = new MWF.Macro.ViewContext(this.view);
U
unknown 已提交
932 933
                }.bind(this), false);
            }
934
            td.addEvent("click", function (ev) {
U
unknown 已提交
935 936 937 938
                var result = this.view.Macro.fire(column.clickCode, this, ev);
                ev.stopPropagation();
                return result;
            }.bind(this));
939 940 941 942 943
        } else if (this.view.statementJson.entityCategory === "official" && column.idPath) {
            var id = this.getDataByPath(this.data, column.idPath);
            if (id) {
                if (this.view.statementJson.entityClassName === "com.x.cms.core.entity.Document") {
                    td.addEvent("click", function (ev) {
U
unknown 已提交
944 945 946
                        this.openCms(ev, id);
                        ev.stopPropagation();
                    }.bind(this));
947 948
                } else {
                    td.addEvent("click", function (ev) {
U
unknown 已提交
949 950 951 952 953
                        this.openWork(ev, id);
                        ev.stopPropagation();
                    }.bind(this));
                }
            }
U
unknown 已提交
954
        }
U
unknown 已提交
955
    },
956
    openCms: function (e, id) {
U
unknown 已提交
957 958 959 960
        var options = {"documentId": id};
        this.view.fireEvent("openDocument", [options, this]); //options 传入的事件
        layout.desktop.openApplication(e, "cms.Document", options);
    },
961
    openWork: function (e, id) {
U
unknown 已提交
962 963 964
        var options = {"workId": id};
        this.view.fireEvent("openDocument", [options, this]); //options 传入的事件
        layout.desktop.openApplication(e, "process.Work", options);
U
unknown 已提交
965 966 967 968
    }
});

MWF.xApplication.query.Query.Statement.Filter = new Class({
969
    Extends: MWF.xApplication.query.Query.Viewer.Filter
U
unknown 已提交
970 971 972
});

MWF.xApplication.query.Query.Statement.Actionbar = new Class({
973
    Extends: MWF.xApplication.query.Query.Viewer.Actionbar
U
unknown 已提交
974 975 976
});

MWF.xApplication.query.Query.Statement.Paging = new Class({
977
    Extends: MWF.xApplication.query.Query.Viewer.Paging
U
unknown 已提交
978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226
});


MWF.xApplication.query.Query.Statement.ExcelUtils = new Class({
    initialize: function(){
        // this.datatemplate = datatemplate;
        // this.form = datatemplate.form;
        if (!FileReader.prototype.readAsBinaryString) {
            FileReader.prototype.readAsBinaryString = function (fileData) {
                var binary = "";
                var pt = this;
                var reader = new FileReader();
                reader.onload = function (e) {
                    var bytes = new Uint8Array(reader.result);
                    var length = bytes.byteLength;
                    for (var i = 0; i < length; i++) {
                        binary += String.fromCharCode(bytes[i]);
                    }
                    //pt.result  - readonly so assign binary
                    pt.content = binary;
                    pt.onload();
                };
                reader.readAsArrayBuffer(fileData);
            }
        }
    },
    _loadResource : function( callback ){
        if( !window.XLSX || !window.xlsxUtils ){
            var uri = "../x_component_Template/framework/xlsx/xlsx.full.js";
            var uri2 = "../x_component_Template/framework/xlsx/xlsxUtils.js";
            COMMON.AjaxModule.load(uri, function(){
                COMMON.AjaxModule.load(uri2, function(){
                    callback();
                }.bind(this))
            }.bind(this))
        }else{
            callback();
        }
    },
    _openDownloadDialog: function(url, saveName){
        /**
         * 通用的打开下载对话框方法,没有测试过具体兼容性
         * @param url 下载地址,也可以是一个blob对象,必选
         * @param saveName 保存文件名,可选
         */
        if( Browser.name !== 'ie' ){
            if(typeof url == 'object' && url instanceof Blob){
                url = URL.createObjectURL(url); // 创建blob地址
            }
            var aLink = document.createElement('a');
            aLink.href = url;
            aLink.download = saveName || ''; // HTML5新增的属性,指定保存文件名,可以不要后缀,注意,file:///模式下不会生效
            var event;
            if(window.MouseEvent && typeOf( window.MouseEvent ) == "function" ) event = new MouseEvent('click');
            else
            {
                event = document.createEvent('MouseEvents');
                event.initMouseEvent('click', true, false, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);
            }
            aLink.dispatchEvent(event);
        }else{
            window.navigator.msSaveBlob( url, saveName);
        }
    },

    index2ColName : function( index ){
        if (index < 0) {
            return null;
        }
        var num = 65;// A的Unicode码
        var colName = "";
        do {
            if (colName.length > 0)index--;
            var remainder = index % 26;
            colName =  String.fromCharCode(remainder + num) + colName;
            index = (index - remainder) / 26;
        } while (index > 0);
        return colName;
    },

    upload : function ( dateColIndexArray, callback ) {
        var dateColArray = [];
        dateColIndexArray.each( function (idx) {
            dateColArray.push( this.index2ColName( idx ));
        }.bind(this))


        var uploadFileAreaNode = new Element("div");
        var html = "<input name=\"file\" type=\"file\" accept=\"csv, application/vnd.openxmlformats-officedocument.spreadsheetml.sheet, application/vnd.ms-excel\" />";
        uploadFileAreaNode.set("html", html);

        var fileUploadNode = uploadFileAreaNode.getFirst();
        fileUploadNode.addEvent("change", function () {
            var files = fileNode.files;
            if (files.length) {
                var file = files.item(0);
                // if( file.name.indexOf(" ") > -1 ){
                // 	this.form.notice( MWF.xApplication.process.Xform.LP.uploadedFilesCannotHaveSpaces, "error");
                // 	return false;
                // }

                //第三个参数是日期的列
                this.importFromExcel( file, function(json){
                    //json为导入的结果
                    if(callback)callback(json);
                    uploadFileAreaNode.destroy();
                }.bind(this), dateColArray ); //["E","F"]

            }
        }.bind(this));
        var fileNode = uploadFileAreaNode.getFirst();
        fileNode.click();
    },
    exportToExcel : function(array, fileName, colWidthArr, dateIndexArray){
        // var array = [["姓名","性别","学历","专业","出生日期","毕业日期"]];
        // array.push([ "张三","男","大学本科","计算机","2001-1-2","2019-9-2" ]);
        // array.push([ "李四","男","大学专科","数学","1998-1-2","2018-9-2" ]);
        // this.exportToExcel(array, "导出数据"+(new Date).format("db"));
        this._loadResource( function(){
            var data = window.xlsxUtils.format2Sheet(array, 0, 0, null);//偏移3行按keyMap顺序转换
            var wb = window.xlsxUtils.format2WB(data, "sheet1", undefined);
            var wopts = { bookType: 'xlsx', bookSST: false, type: 'binary' };
            var dataInfo = wb.Sheets[wb.SheetNames[0]];

            var widthArray = [];
            array[0].each( function( v, i ){ //设置标题行样式

                if( !colWidthArr )widthArray.push( {wpx: 100} );

                var at = String.fromCharCode(97 + i).toUpperCase();
                var di = dataInfo[at+"1"];
                // di.v = v;
                // di.t = "s";
                di.s = {  //设置副标题样式
                    font: {
                        //name: '宋体',
                        sz: 12,
                        color: {rgb: "#FFFF0000"},
                        bold: true,
                        italic: false,
                        underline: false
                    },
                    alignment: {
                        horizontal: "center" ,
                        vertical: "center"
                    }
                };
            }.bind(this));

            if( dateIndexArray && dateIndexArray.length ){
                dateIndexArray.each( function( value, index ){
                    dateIndexArray[ index ] = this.index2ColName(value);
                }.bind(this))
            }

            for( var key in dataInfo ){
                //设置所有样式,wrapText=true 后 /n会被换行
                if( key.substr(0, 1) !== "!" ){
                    var di = dataInfo[key];
                    if( !di.s )di.s = {};
                    if( !di.s.alignment )di.s.alignment = {};
                    di.s.alignment.wrapText = true;

                    if( dateIndexArray && dateIndexArray.length ){
                        var colName = key.replace(/\d+/g,''); //清除数字
                        var rowNum = key.replace( colName, '');
                        if( rowNum > 1 && dateIndexArray.contains( colName ) ){
                            //di.s.numFmt = "yyyy-mm-dd HH:MM:SS"; //日期列 两种方式都可以
                            di.z = 'yyyy-mm-dd HH:MM:SS'; //日期列
                        }
                    }
                }

            }

            if( colWidthArr ){
                colWidthArr.each( function (w) {
                    widthArray.push( {wpx: w} );
                })
            }
            dataInfo['!cols'] = widthArray; //列宽度

            this._openDownloadDialog(window.xlsxUtils.format2Blob(wb), fileName +".xlsx");
        }.bind(this))
    },
    importFromExcel : function( file, callback, dateColArray ){
        this._loadResource( function(){
            var reader = new FileReader();
            var workbook, data;
            reader.onload = function (e) {
                //var data = data.content;
                if (!e) {
                    data = reader.content;
                }else {
                    data = e.target.result;
                }
                workbook = window.XLSX.read(data, { type: 'binary' });
                //wb.SheetNames[0]是获取Sheets中第一个Sheet的名字
                //wb.Sheets[Sheet名]获取第一个Sheet的数据
                var sheet = workbook.SheetNames[0];
                if (workbook.Sheets.hasOwnProperty(sheet)) {
                    // fromTo = workbook.Sheets[sheet]['!ref'];
                    // console.log(fromTo);
                    var worksheet = workbook.Sheets[sheet];

                    if( dateColArray && typeOf(dateColArray) == "array" && dateColArray.length ){
                        var rowCount;
                        if( worksheet['!range'] ){
                            rowCount = worksheet['!range'].e.r;
                        }else{
                            var ref = worksheet['!ref'];
                            var arr = ref.split(":");
                            if(arr.length === 2){
                                rowCount = parseInt( arr[1].replace(/[^0-9]/ig,"") );
                            }
                        }
                        if( rowCount ){
                            for( var i=0; i<dateColArray.length; i++ ){
                                for( var j=1; j<=rowCount; j++ ){
                                    var cell = worksheet[ dateColArray[i]+j ];
                                    if( cell ){
                                        delete cell.w; // remove old formatted text
                                        cell.z = 'yyyy-mm-dd'; // set cell format
                                        window.XLSX.utils.format_cell(cell); // this refreshes the formatted text.
                                    }
                                }
                            }
                        }
                    }

                    var json = window.XLSX.utils.sheet_to_json( worksheet );
                    //var data = window.XLSX.utils.sheet_to_row_object_array(workbook.Sheets[sheet], {dateNF:'YYYY-MM-DD'});
                    if(callback)callback(json);
                    // console.log(JSON.stringify(json));
                    // break; // 如果只取第一张表,就取消注释这行
                }
                // for (var sheet in workbook.Sheets) {
                //     if (workbook.Sheets.hasOwnProperty(sheet)) {
                //         fromTo = workbook.Sheets[sheet]['!ref'];
                //         console.log(fromTo);
                //         var json = window.XLSX.utils.sheet_to_json(workbook.Sheets[sheet]);
                //         console.log(JSON.stringify(json));
                //         // break; // 如果只取第一张表,就取消注释这行
                //     }
                // }
            };
            reader.readAsBinaryString(file);
        })
    }
U
unknown 已提交
1227
});