mrzcc
2020-03-06 ecdac8a738f254db2b2ea28cbe3248f7a6a7eb2b
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
(function (vc) {
    vc.extends({
        propTypes: {
            parentModal: vc.propTypes.string,
            callBackListener: vc.propTypes.string, //父组件名称
            callBackFunction: vc.propTypes.string //父组件监听方法
        },
        data: {
            machineSelect2Info: {
                machines: [],
                machineId: '-1',
                machineCode: '',
                machineName: '',
                machineSelector: {},
            }
        },
        watch: {
            machineSelect2Info: {
                deep: true,
                handler: function () {
                    vc.emit($props.callBackListener, $props.callBackFunction, this.machineSelect2Info);
                }
            }
        },
        _initMethod: function () {
            this._initMachineSelect2();
        },
        _initEvent: function () {
            //监听 modal 打开
            /* $('#'+$props.parentModal).on('show.bs.modal', function () {
                  vc.component._initFloorSelect2();
             })*/
            vc.on('machineSelect2', 'setMachine', function (_param) {
                console.log("执行:edit machineSelect2"+_param);
                vc.copyObject(_param, this.machineSelect2Info);
                /* $("#floorSelector").val({
                     id:param.floorId,
                     text:_param.floorNum
                 }).select2();*/
 
                var option = new Option(_param.machineName, _param.machineId, true, true);
                this.machineSelect2Info.machineSelector.append(option);
            });
 
            vc.on('machineSelect2', 'clearMachine', function (_param) {
                this.machineSelect2Info = {
                    machines: [],
                    machineId: '-1',
                    machineCode: '',
                    machineName: '',
                    machineSelector: {},
                };
            });
        },
        methods: {
            _initMachineSelect2: function () {
                console.log("调用_initMachineSelect2 方法");
                $.fn.modal.Constructor.prototype.enforceFocus = function () {
                };
                $.fn.select2.defaults.set('width', '100%');
                this.machineSelect2Info.machineSelector = $('#machineSelector').select2({
                    placeholder: '必填,请选择楼栋',
                    allowClear: true,//允许清空
                    escapeMarkup: function (markup) {
                        return markup;
                    }, // 自定义格式化防止xss注入
                    ajax: {
                        url: "/callComponent/machineSelect2/list",
                        dataType: 'json',
                        delay: 250,
                        data: function (params) {
                            console.log("param", params);
                            var _term = "";
                            if (params.hasOwnProperty("term")) {
                                _term = params.term;
                            }
                            return {
                                machineName: _term,
                                page: 1,
                                row: 10,
                                communityId: vc.getCurrentCommunity().communityId
                            };
                        },
                        processResults: function (data) {
                            console.log(data, this._filterMachineData(data.machines));
                            return {
                                results: this._filterMachineData(data.machines)
                            };
                        },
                        cache: true
                    }
                });
                $('#machineSelector').on("select2:select", function (evt) {
                    //这里是选中触发的事件
                    //evt.params.data 是选中项的信息
                    console.log('select', evt);
                    this.machineSelect2Info.machineId = evt.params.data.id;
                    this.machineSelect2Info.machineName = evt.params.data.text;
                });
 
                $('#machineSelector').on("select2:unselect", function (evt) {
                    //这里是取消选中触发的事件
                    //如配置allowClear: true后,触发
                    console.log('unselect', evt);
                    this.machineSelect2Info.machineId = '-1';
                    this.machineSelect2Info.machineName = '';
 
                });
            },
            _filterMachineData: function (_machines) {
                var _tmpMachines = [];
                for (var i = 0; i < _machines.length; i++) {
                    var _tmpMachine = {
                        id: _machines[i].machineId,
                        text: _machines[i].machineName
                    };
                    _tmpMachines.push(_tmpMachine);
                }
                return _tmpMachines;
            }
        }
    });
})(window.vc);