組件js
(function($){ //自定義去除字符串兩邊空白 String.prototype.trim=function(){ return this.replace(/(^\s*)|(\s*$)/g, ""); } //自定義導(dǎo)入組件 $.fn.customImport = function(methodOroptions,value){ if(typeof methodOroptions == "string"){//存在方法時(shí),調(diào)用方法 return $.fn.customImport.methods[methodOroptions](this, value); } var optionsObj = methodOroptions||{}; //不存在方法時(shí),那么傳遞的是屬性定義。 return this.each(function() { $.data(this, "customImport", { options : $.extend({}, $.fn.customImport.defaults, optionsObj) }); initCustomImport(this); }); } //定義組件默認(rèn)屬性 $.fn.customImport.defaults={ width:400, height:90, enctype:'multipart/form-data', action:'', //導(dǎo)入方法調(diào)用 method:'post', //請(qǐng)求方式 fileType:'.XLS,.xlsx', //文件類(lèi)型,默認(rèn)為xls格式 xmlName:'', //導(dǎo)入模版XML參數(shù)名 xmlValue:'', //導(dǎo)入模版XML參數(shù)值 filePath:'', //文件路徑參數(shù)名 uploadTemplateUrl:'', //下載模版的路徑 onSubmit:function(param){ } } //定義組件方法 $.fn.customImport.methods = { submit :function(obj,options){ if($(obj).customImport("validate")){ var formOptions = {}; if(options.action){ formOptions.url = options.action; } if(options.onSubmit){ formOptions.onSubmit = options.onSubmit; } if(options.success){ formOptions.success = options.success; } $CommonUI.getForm("#importForm").form("submit",formOptions); } }, clear:function(obj){ //獲取當(dāng)前文件框 var fileInput = $(obj).find(".real-file"); //在當(dāng)前文件框后克隆一個(gè)相同的元素,并設(shè)置值為"",IE默認(rèn)克隆的值為空,谷歌火狐會(huì)將值一起克隆 fileInput.after(fileInput.clone().val("")); //刪除當(dāng)前文件框 fileInput.remove(); //為新的文件框綁定onchange事件 $(obj).find(".real-file").on("change",function(){ changeFile(obj); }); //清空文件顯示路徑 $(obj).find(".file-pathname").val(""); //取消校驗(yàn)提示 $(obj).find(".validatebox-invalid").removeClass("validatebox-invalid"); }, validate:function(obj){ var validateState = $(obj).find(".file-pathname").validatebox("isValid"); return validateState; } } function initCustomImport(obj){ var options = $.data(obj,"customImport").options; $(obj).width(options.width); $(obj).height(options.height); $(obj).attr("enctype",options.enctype); $(obj).attr("action",options.action); $(obj).attr("method",options.method); if(!flag){ //初始化組件 $(obj).append('<div class="choose-file"><div class="choose-title">瀏覽目錄</div></div>');//添加文件選擇按鈕 $(obj).find(".choose-file").append('<input class="real-file" type="file"/>'); //真實(shí)文件控件 $(obj).find(".choose-file").append('<div class="file-path"><input class="file-pathname validatebox" type="text" readonly="readonly" data-options="required:true,missingMessage:"請(qǐng)選擇導(dǎo)入文件",validType:"importFormatValidate""/></div>');//文件路徑顯示框 $(obj).append('<div class="import-template"><a class="upload-template" href="javascrip:void(0);">導(dǎo)入模版下載</a></div>');//模版下載按鈕 $(obj).append('<div class="import-xml"><input class="xml-config" type="hidden"></div>'); $(obj).find('.import-xml').append('<input class="websocket-config" type="hidden" name="dto.code">'); //綁定文件名改變事件 $(obj).find(".real-file").on("change",function(){ changeFile(obj); }); } //綁定組件屬性和事件 $(obj).find(".real-file").attr("name",options.filePath);//為文本框綁定name屬性 $(obj).find(".real-file").attr("accept",options.fileType);//文件接收類(lèi)型 $(obj).find(".real-file").width(options.width*0.3-6); $(obj).find(".import-xml .xml-config").attr("name",options.xmlName);//導(dǎo)入的xml參數(shù)名 $(obj).find(".import-xml .xml-config").val(options.xmlValue);//導(dǎo)入的xml參數(shù)值 //綁定下載模版的url $(obj).find(".upload-template").attr("href",options.uploadTemplateUrl); } //初始化導(dǎo)入框 var flag = false; if($(".custom-import").length>0){ $(".custom-import").customImport(); flag = true; } //選擇文件改變時(shí)觸發(fā) function changeFile(obj){ var filePath = $(obj).find(".real-file").val(); if(filePath&&filePath.trim()!=""){ var fileNamePosition = filePath.lastIndexOf('\\'); var fileName=filePath.substring(fileNamePosition+1); $(obj).find(".file-pathname").val(fileName); $(obj).find(".file-pathname").removeClass("validatebox-invalid"); } } })(jQuery); $(function(){ $.extend($.fn.validatebox.defaults.rules, { importFormatValidate : {// 驗(yàn)證導(dǎo)入格式是否是excel validator : function(value,param) { var fileTypeIndex = value.lastIndexOf("."); var fileType = value.substring(fileTypeIndex); if(fileType!=".xls"&&fileType!=".xlsx"){ return false; } return true; }, message : '請(qǐng)選擇.xls或者.xlsx文件!' } }); })
組件css
.choose-file{ padding:10px; } .choose-title{ width: 30%; height: 30px; line-height: 30px; font-size: 20px; text-align: center; background: #337AB7; color: #fff; border-radius: 6px 0 0 6px; cursor: pointer; float:left; } .choose-title:hover{ background: #36577D; } .real-file{ height: 30px; width: 27%; position: absolute; left: 25px; opacity: 0; filter: alpha(opacity=0); } .file-path { width: 70%; height: 30px; float:left; } .file-pathname{ width: 100%; height: 26px; border-radius: 0 6px 6px 0; border: 1px solid #337AB7; } .import-template{ float: right; margin: 10px; background: #cbcbcc; border-radius: 6px; } .import-template:hover{ background:#BEB89D; } .upload-template{ text-decoration: none; color: #fff; padding: 7px; display: inline-block } .import-xml{ display:none; clear:both; } .other-title{ width: 30%; height: 30px; line-height: 30px; font-size: 20px; text-align: center; background: #337AB7; color: #fff; border-radius: 6px 0 0 6px; float:left; } .other-param{ padding:10px; } .other-content{ width: 70%; height: 30px; float:left; } .other-text{ border-radius: 0 6px 6px 0; border: 1px solid #337AB7; }
組件引用
html部分
<div id="importExcelWin" class="dialog"> <form id="importForm" class="custom-import dhccform"></form> </div> <link rel="stylesheet" type="text/css" href="<%=request.getContextPath()%>/js/customComponent/customImport.css"> <script type="text/javascript" src="<%=request.getContextPath()%>/js/customComponent/customImport.js"></script>
js部分
$CommonUI.getDialog("#importExcelWin").dialog({ title : '導(dǎo)入字典', width :430, height :185, closed : true, modal : true, buttons:[{ text:'保存', handler:function(){ importData(); } },{ text:'取消', handler:function(){ $CommonUI.getDialog("#importExcelWin").dialog("close"); } } ] }) //初始化導(dǎo)入框 $("#importForm").customImport({ action:$WEB_ROOT_PATH+"/excel/excelCtrl.htm?BLHMI=importExcel", xmlName:'dto.exportFileName', //導(dǎo)入模版XML參數(shù)名 xmlValue:'systemDictionaryImport', //導(dǎo)入模版XML參數(shù)值 filePath:'dto.uploadFile', //文件路徑參數(shù)名 uploadTemplateUrl:$WEB_ROOT_PATH+'/exportexcel/exportExcelCtrl!uploadExcelTemplate.htm?filename=systemDictionary' });
組件效果
1.該組件使用了easyui-validatebox,使用者也需引用該組件不然校驗(yàn)會(huì)出錯(cuò)。
2.該組件是結(jié)合后端定制的一個(gè)組件,以減少前端html重復(fù)配置而導(dǎo)致的出錯(cuò)。值得學(xué)習(xí)的僅僅是組件定義的方法而不是組件本身。
3.為了滿足IE組件有2處特殊處理,第一:是用文件框覆蓋在選擇目錄之上以保證IE安全校驗(yàn)只識(shí)別鼠標(biāo)直接點(diǎn)擊的文本框。第二:IE不能直接清除文件框的內(nèi)容,這里采用克隆刪除的方式清空文件框以存在的內(nèi)容。

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Detailed explanation of jQuery reference method: Quick start guide jQuery is a popular JavaScript library that is widely used in website development. It simplifies JavaScript programming and provides developers with rich functions and features. This article will introduce jQuery's reference method in detail and provide specific code examples to help readers get started quickly. Introducing jQuery First, we need to introduce the jQuery library into the HTML file. It can be introduced through a CDN link or downloaded

How to use PUT request method in jQuery? In jQuery, the method of sending a PUT request is similar to sending other types of requests, but you need to pay attention to some details and parameter settings. PUT requests are typically used to update resources, such as updating data in a database or updating files on the server. The following is a specific code example using the PUT request method in jQuery. First, make sure you include the jQuery library file, then you can send a PUT request via: $.ajax({u

jQuery is a fast, small, feature-rich JavaScript library widely used in front-end development. Since its release in 2006, jQuery has become one of the tools of choice for many developers, but in practical applications, it also has some advantages and disadvantages. This article will deeply analyze the advantages and disadvantages of jQuery and illustrate it with specific code examples. Advantages: 1. Concise syntax jQuery's syntax design is concise and clear, which can greatly improve the readability and writing efficiency of the code. for example,

Title: jQuery Tips: Quickly modify the text of all a tags on the page In web development, we often need to modify and operate elements on the page. When using jQuery, sometimes you need to modify the text content of all a tags in the page at once, which can save time and energy. The following will introduce how to use jQuery to quickly modify the text of all a tags on the page, and give specific code examples. First, we need to introduce the jQuery library file and ensure that the following code is introduced into the page: <

Title: Use jQuery to modify the text content of all a tags. jQuery is a popular JavaScript library that is widely used to handle DOM operations. In web development, we often encounter the need to modify the text content of the link tag (a tag) on ??the page. This article will explain how to use jQuery to achieve this goal, and provide specific code examples. First, we need to introduce the jQuery library into the page. Add the following code in the HTML file:

How to remove the height attribute of an element with jQuery? In front-end development, we often encounter the need to manipulate the height attributes of elements. Sometimes, we may need to dynamically change the height of an element, and sometimes we need to remove the height attribute of an element. This article will introduce how to use jQuery to remove the height attribute of an element and provide specific code examples. Before using jQuery to operate the height attribute, we first need to understand the height attribute in CSS. The height attribute is used to set the height of an element

jQuery is a popular JavaScript library that is widely used to handle DOM manipulation and event handling in web pages. In jQuery, the eq() method is used to select elements at a specified index position. The specific usage and application scenarios are as follows. In jQuery, the eq() method selects the element at a specified index position. Index positions start counting from 0, i.e. the index of the first element is 0, the index of the second element is 1, and so on. The syntax of the eq() method is as follows: $("s

jQuery is a popular JavaScript library widely used in web development. During web development, it is often necessary to dynamically add new rows to tables through JavaScript. This article will introduce how to use jQuery to add new rows to a table, and provide specific code examples. First, we need to introduce the jQuery library into the HTML page. The jQuery library can be introduced in the tag through the following code:
