Administrator
2022-10-09 23f93897d618a5253a18b65b124196f95891f240
提交 | 用户 | 时间
58d006 1 /**
A 2 * date: 
3 * author: neeler 
4 */
5
6 //test data start
7 // var data_doDel = Mock.mock('/enterprise/position/delItem.htm', {code: 0, errmsg: '此岗位正在使用中不能删除!'})
8 // var data_doSave = Mock.mock('/enterprise/position/saveItem.htm', {code: 0, errmsg: '此岗位正在使用中不能删除!'})
9 // var data_doBind = Mock.mock('/basicconfig/mapmaterial/bind.htm', {code: 0, })
10 // var data_doUnbind = Mock.mock('/basicconfig/mapmaterial/unbind.htm', {code: 0, })
11 //test data end
12
13 var TYPE = [{key: '全部', value: 0}, {key: '已映射', value: 1}, {key: '未映射', value: 2}]
14
15 var Obj = {
16     // sysDepartNo: '',
17     sysCompanyNo: '',
18     sysname: '',
19     companyNo: '',
20     cname: ''
21 }
22
23 window.I = {
24     PZ: 30,
25     keyword: $('#keyword'),
26     searchBtn: $('#searchBtn'),
27     newObj: $('#newObj')
28 }
29
30 var ViewModel = function() {
31     var self = this;
32     self.tList = ko.observableArray(TYPE);
33     self.skeyword = ko.observable();
34     self.ckeyword = ko.observable();
35     self.type = ko.observable();
36     self.list = ko.observableArray();
37
38     self.del = function(item) {
39         doDel(item);
40     }
41     self.unbind = function(item) {
42         doUnbind(item);
43     }
44     self.bind = function(item) {
45         ksdoBind(item);        
46         if (item.cnorms() != '' && item.cnorms() != undefined) {
47             ksdoBind(item);
48         }
49     }
50 }
51
52 $(function () {
53     window.vm = new ViewModel();
54     ko.applyBindings(vm);
55     doSearch();
56     pageInit();
57 });
58
59 function pageInit() {
60     I.newObj.click(function (e) { 
61         e.preventDefault();
62         vm.list.push(ko.mapping.fromJS(Obj));
63         refreshTypeahead();
64     });
65     I.searchBtn.click(function(event) {
66         doSearch();
67     });
68     I.keyword.on(ISIE ? 'keydown' : 'keyup', function (event) {
69         if (event.keyCode == 13) {
70             doSearch();
71         }
72     });
73 }
74
75 function doSearch() {
76     getList(vm.skeyword(), vm.ckeyword(), vm.type(), 0, I.PZ, 1);
77 }
78
79 function getList(skeyword, ckeyword, type, page, pagesize, pages) {
80     $.post('/basicconfig/mapmaterial/getList.htm', {
81         skeyword: skeyword,
82         ckeyword: ckeyword,
83         type: type,
84         page: page,
85         pagesize: pagesize,
86         pages: pages
87     }, function(data, textStatus, xhr) {
88         vm.list.removeAll();
89         if (data.code >= 1) {
90            // parent.showErrmsg(data.errmsg);
91             swal({title:'',text:data.errmsg,type:'error',confirmButtonText:'确定'});
92             return;
93         }
94
95         if (data.code == 0) {
96             if (isList(data.items)) {
97                 $.each(data.items, function(index, val) {
98                     val.cname = ko.observable(val.cname);
99                     val.cnorms = ko.observable(val.cnorms);
100                     val.cMaterielNo = ko.observable(val.cMaterielNo);
101                     val.cunitname = ko.observable(val.cunitname);
102                     val.num = ko.observable(val.cnum);
103                     vm.list.push(val);
104                 });
105                 refreshTypeahead();
106             }
107             if (!!pages) {
108                 pages = data.pages;
109                 $('#pagdiv').unbind('page').empty();
110                 if (pages > 1) {
111                     $('#pagdiv').bootpag({total: pages, maxVisible: 10, page: page + 1}).on('page',function(event,num){
112                         getList(skeyword, ckeyword, type, num - 1, pagesize, 0);
113                     });
114                 }
115             }
116             return;
117         }
118     }, 'json');
119 }
120
121 function valid() {
122     return true;
123 }
124
125 function doSave() {
126 }
127
128 function doDel(item) {
129     var obj = ko.mapping.toJS(item);
130     console.log(obj);return;
131     $.post("/basicinfo/mapCompany/doDel.htm", {sysCompanyNo: obj.sysCompanyNo},
132         function (data, textStatus, jqXHR) {
133            if (data.code >= 1) {
134                //showErrmsg(data.errmsg);
135                swal({title:'',text:data.errmsg,type:'error',confirmButtonText:'确定'}); 
136                return;
137            }
138            if (data.code == 0) {
139                vm.list.remove(item);
140                return;
141            } 
142         },
143         "json"
144     );
145 }
146
147 function validMsg(msg) {
148     $("#modalContent").html(msg);
149     $("#dialog-confirm").removeClass('hide').dialog({
150         resizable: false,
151         width: '320',
152         modal: true,
153         title: "<div class='widget-header'><h4 class='smaller'><i class='ace-icon fa fa-exclamation-triangle red'></i>确认信息</h4></div>",
154         title_html: true,
155         position: { my: "center", at: "center", of: window },
156         buttons: [
157             // {
158             //     html: "<i class='ace-icon fa fa-trash-o bigger-110'></i>&nbsp; 确定",
159             //     "class" : "btn btn-danger btn-minier",
160             //     click: function() {
161             //         $( this ).dialog( "close" );
162             //         doDel(item);
163             //     }
164             // }
165             // ,
166             {
167                 html: "<i class='ace-icon fa fa-times bigger-110'></i>&nbsp; 确定",
168                 "class" : "btn btn-minier",
169                 click: function() {
170                     $( this ).dialog( "close" );
171                 }
172             }
173         ]
174     });
175 }
176
177 function refreshTypeahead() {
178     $('.mtableTypeahead').typeahead('destroy');
179     $('.mtableTypeahead').unbind('typeahead:select');
180     // $('.mtableTypeahead').unbind('typeahead:change');
181     var reception = new Bloodhound({
182         datumTokenizer: Bloodhound.tokenizers.whitespace,
183         queryTokenizer: Bloodhound.tokenizers.whitespace,
184         remote: {
185             url: '/basicconfig/mapmaterial/typeahead.htm?query=%QUERY',
186             wildcard: '%QUERY'
187         }
188     });
189
190     $('.mtableTypeahead').typeahead(null, {
191         display: 'cname',
192         limit: 15,
193         source: reception,
194         templates: {
195             header: '<div id="bx"><span class="bh">编号</span><span class="mc">名称</span><span class="gg">规格</span><span class="dw">单位</span></div>',
196             suggestion: function(data) {
197                 return '<div><span class="bh">' + data.cMaterielNo + '</span><span class="mc">' + data.cname + '</span><span class="gg">' + data.cnorms + '</span><span class="dw">' + data.cunitname + '</span></div>';
198             },
199         }
200     });
201
202     $('.mtableTypeahead').bind('typeahead:select', function(ev, suggestion) {
203         var index = $('.mtableTypeahead.tt-input').index($(this));
204         // index = Math.floor(index / 2);
205         // console.log(index);
206         doBind(index, suggestion);   
207         // vm.list()[index].cname(suggestion.name);
208         // vm.list()[index].companyNo(suggestion.companyNo);
209         // $(this).data('_mlname', suggestion.name);
210     });
211 }
212
213 function ksdoBind(item) {
214     // console.log(item);
215     $.post("/basicconfig/mapmaterial/bind.htm", {
216         id: item.id,
217         erpID: item.erpid,
218         cname: item.cname(),
219         cnorms: item.cnorms(),
220         cMaterielNo: item.cMaterielNo(),
221         cnum: item.num()
222     },
223         function (data, textStatus, jqXHR) {
224             if (data.code >= 1) {
225                 //showErrmsg(data.errmsg);
226                 swal({title:'',text:data.errmsg,type:'error',confirmButtonText:'确定'}); 
227                 return;
228             }
229             if (data.code == 0) {
230                 console.log('映射成功!');
231                 return;
232             }
233         },
234         "json"
235     );
236 }
237
238 function doBind(index, obj) {
239     var id = vm.list()[index].id;
240     var erpID = obj.id;
241     $.post("/basicconfig/mapmaterial/bind.htm", {
242         id: id,
243         erpID: obj.erpid,
244         cname: obj.cname,
245         cnorms: obj.cnorms,
246         cMaterielNo: obj.cMaterielNo,
247         num: vm.list()[index].num()
248     },
249         function (data, textStatus, jqXHR) {
250             if (data.code >= 1) {
251                 //showErrmsg(data.errmsg);
252                 swal({title:'',text:data.errmsg,type:'error',confirmButtonText:'确定'}); 
253                 return;
254             }
255             if (data.code == 0) {
256                 console.log('映射成功!');
257                 vm.list()[index].erpid = erpID;
258                 vm.list()[index].cname(obj.cname);
259                 vm.list()[index].cnorms(obj.cnorms);
260                 vm.list()[index].cMaterielNo(obj.cMaterielNo);
261                 vm.list()[index].cunitname(obj.cunitname);
262                 return;
263             }
264         },
265         "json"
266     );
267 }
268
269 function doUnbind(item) {
270     var id = item.id;
271     var erpID = item.erpID;
272     $.post("/basicconfig/mapmaterial/unbind.htm", {
273         id: id,
274         erpID: erpID
275     },
276         function (data, textStatus, jqXHR) {
277             if (data.code >= 1) {
278                 //showErrmsg(data.errmsg);
279                 swal({title:'',text:data.errmsg,type:'error',confirmButtonText:'确定'}); 
280                 return;
281             }
282             if (data.code == 0) {
283                 item.cname('');
284                 item.cnorms('');
285                 item.cMaterielNo('');
286                 item.cunitname('');
287                 item.num('');
288                 console.log('解除成功!');
289                 return;
290             }
291         },
292         "json"
293     );
294 }