alert_str = ['' ].join(''); import_mask_str =['' ].join(''); upload_dialog_str = [ '' ].join(''); ;(function($) { window.import_url = ""; var upload_file = ""; var upload_dia =$(upload_dialog_str); var import_dia =$(import_mask_str); var alert_dia =$(alert_str); $.fn.data_import = function(opts) { // 上传文件 function ajax_upload(){ var file_input = upload_dia.find('input#file'); var file_data = file_input.prop('files')[0]; var form_data = new FormData(); form_data.append("file", file_data); form_data.append("request", "upload"); $.ajax({ url:import_url, dataType:"json", data:form_data, cache:false, type:"POST", processData:false, contentType: false, success: function(data) { if (data.status == 'error') { upload_dia.find('span#span_upload_file').html(data.msg); upload_dia.find('input#upload_import').hide(); } if (data.status == 'ok') { upload_file = data.upload_file; upload_dia.find('span#span_upload_file').html(upload_file); upload_dia.find('span#span_miss_fields').html(data.miss_fields); if (data.miss_fields) { upload_dia.find('input#upload_import').hide(); upload_dia.find('li#li_miss_fields').show(); } else { upload_dia.find('li#li_miss_fields').hide(); upload_dia.find('input#upload_import').show(); } } } }); } // 导入数据操作 function ajax_import(start, len, file_path, addnum){ $.ajax({ url:import_url, dataType:"json", data:{'file':file_path,'start':start,'len':len, 'request':'import'}, cache:false, type:"GET", success: function(data){ var failListDom = alert_dia.find("#alert_faillist"); import_dia.find("#progressbar").attr("style", "width:"+data['ret']+"%;");//进度条 for(var i in data['faillist']){ //导入错误 var itemDom = "
第"+ data['faillist'][i]['row']+"行, "+ data['faillist'][i]['info']+"
"; failListDom.append(itemDom); } if(data['finished'] == 'finished'){//是否结束 import_dia.modal("hide"); upload_dia.modal("hide"); alert_dia.modal("show"); var success_number = addnum+data["addnum"]; var fail_number = alert_dia.find("#alert_faillist div").length; alert_dia.find("#alert_count_total").html(data["count"]); alert_dia.find("#alert_count_success").html(success_number); alert_dia.find("#alert_count_fail").html(fail_number); failListDom.hide(); if(fail_number > 0) failListDom.show(); } else{ var success_number = addnum+data["addnum"]; ajax_import(start+len, len, file_path, success_number); } } }); } // 字段的Hover提示 function gen_fields_dom(fields, info_json){ var info = {}; try{ info = JSON.parse(info_json); } catch(E){ console.log('info json parse error') }; var rootDom = $("
"); // 每个字段生成Span标签 var fieldsArr = fields.split(", "); for(idx in fieldsArr) { var field = fieldsArr[idx]; var curDom = $("").css({ 'display': 'inline-block', 'color': '#060', 'margin': '0 8px 0 0', 'cursor': 'pointer',}); curDom.html(fieldsArr[idx]); if(info[field]) { curDom.attr('title', info[field]).click(function () { show_message('提示', $(this).attr('title')); }); } // Hover提示 rootDom.append(curDom); } return rootDom; } // 初始化导入对话框 function init_import_dialog(){ // 必选字段提示 var _import_fields = options['import_columns']; var _import_fields_nomust = options['import_columns_nomust']; var _import_info_json = options['import_info_json']; var tempDom = gen_fields_dom(_import_fields, _import_info_json); upload_dia.find('div#import_fields').empty().append(tempDom); // 可选字段提示 tempDom = gen_fields_dom(_import_fields_nomust, _import_info_json); upload_dia.find('div#import_fields_nomust').empty().append(tempDom); if(!_import_fields_nomust) upload_dia.find('div#import_fields_nomust_out').hide(); // 其他附加提示信息 var tips_dom = context.attr('data-tips'); upload_dia.find('#import-tips').html(tips_dom).hide(); if(tips_dom) upload_dia.find('#import-tips').show(); upload_dia.find('li#li_miss_fields').hide(); upload_dia.find('input#upload_import').hide(); } // 设置导出windows.import_url function set_windows_import_url() { // 获取 import_url var import_url = options['import_url']; import_url = import_url || context.attr('href') || 'data_import/'; // import_url get参数 var search = $.query.load(window.location.search), params = $.query.load(import_url); for(var key in params.keys) search = search.set(key, params.get(key)); // 由于自动转换 param[0]=01¶m[1]=02 search_str = search.toString().replace(/%5B(\d+)%5D/g, '%5B%5D'); window.import_url = import_url.split("?")[0] + search_str; } // 下载模板 upload_dia.find('#tempate_download').click(function () { var params = $.query.load(import_url); window.location.href = import_url+params.set('request', 'template'); }); // 上传文件 upload_dia.find('#upload_submit').click(function(e){ ajax_upload(upload_dia); }); // 开始导入 upload_dia.find('#upload_import').click(function (){ alert_dia.find("#alert_faillist div").remove(); // 清空之前状态 import_dia.find("#progressbar").attr("style", "width:0%;"); //进度条 upload_dia.modal("hide"); import_dia.modal("show"); ajax_import(0, 10, upload_file, 0); }); // 将defaults 和 options 参数合并到{} var context = $(this); var options = $.extend({},$.fn.data_import.defaults,opts); $(this).click(function(e){ e.preventDefault(); // 初始化操作 context = $(this); init_import_dialog(); // Dom初始化 set_windows_import_url(); // 设置导入URL upload_dia.modal("show"); }); // each end }; //定义默认 $.fn.data_import.defaults = { }; })(jQuery);