jeecg3.8在子表頁面中使用WebUploader組件

bcAbout-update.jsp改動以下:javascript

 由於默認子表的上傳組件不能回顯,因此改形成WebUploadercss

  1.在更新頁面註銷掉生成代碼html

<%--<script type="text/javascript">--%>
<%--$(document).ready(function(){--%>
<%--$('#tt').tabs({ 此段代碼會形成子頁面重複加載,致使回顯異常且驗證失效,第一次加載代碼的位置未知。--%>
<%--onSelect:function(title){--%>
<%--$('#tt .panel-body').css('width','auto');--%>
<%--}--%>
<%--});--%>
<%--$(".tabs-wrap").css('width','100%');--%>
<%--});--%>
<%--</script>--%>

<t:tabs id="tt" iframe="false" tabPosition="top" fit="false">
<t:tab href="bcAboutController.do?bcAboutSubList&id=${bcAboutPage.id}" icon="icon-search" title="***團隊" id="bcAboutSub" ></t:tab>
</t:tabs>


2.附代表細 模版 默認組件位置替換爲WebUploader組件的模板
 
<%-- 上傳組件模板start --%>
<link rel="stylesheet" type="text/css" href="plug-in/webuploader/custom.css">
<div id="bcAboutSubList_indeximgUrluploader" class="wu-example">
<div id="bcAboutSubList_indeximgUrlthelist" class="uploader-list">

</div>
<div id="bcAboutSubList_indeximgUrl_progress_bar" class="progress-bar-ty " style="display:none">
<div class="progress-ty">
<span class="upload-label-ty" style="display:none;">正在加載...<b class="value">79%</b></span>
</div>
</div>
<div class="btns">
<div id="bcAboutSubList_indeximgUrlpicker" class="webuploader-container">
選擇文件
</div>
<%--<input nullmsg="請上傳專家圖片" datatype="*" type="hidden" id="bcAboutSubList_indeximgUrldataTypeInp">--%>
</div>
</div>
<div id="tempdiv_bcAboutSubList_indeximgUrl" class="tempIMGdiv"></div>
<%-- 上傳組件模板end --%>

3.添加表單提交前的方法,用於修正新增行的name名稱,不然提交報錯
<t:formvalid formid="formobj" dialog="true" usePlugin="password" layout="table" tiptype="1" action="bcAboutController.do?doUpdate" beforeSubmit="fixImgData" callback="jeecgFormFileCallBack@Override">
 
function fixImgData() {
// 將上傳組件圖片的name下標修正再提交表單
resetTrNum('add_bcAboutSub_table');//此方法jeecg自帶,用於修正行順序改變後元素的name屬性值
}
 
bcAboutSubList.jsp子表頁面修改以下:
  1.定義組件默認js,和計數全局變量(看着長,實際上是默認組件生成的,生成後拷貝過來便可,注意js需改一個位置TODO那裏)
推薦在線格式化代碼工具http://tool.oschina.net/codeformat/js
//WebUploader組件js
var code="Array.prototype.removeItem = function(val) {\n" +
" var index = this.indexOf(val);\n" +
" if (index > -1) {\n" +
" this.splice(index, 1);\n" +
" }\n" +
"};\n" +
"var exsitPathArr_bcAboutSubList_indeximgUrl = new Array();\n" +
"$(function() {\n" +
" var state = 'pending';\n" +
" var $list = $('#bcAboutSubList_indeximgUrlthelist');\n" +
" var uploader = WebUploader.create({\n" +
" swf: 'plug-in/webuploader/Uploader.swf',\n" +
" server: 'systemController/filedeal.do',\n" +
" pick: '#bcAboutSubList_indeximgUrlpicker',\n" +
" duplicate: false,\n" +
" resize: false,\n" +
" auto: true,\n" +
" fileVal: 'file',\n" +
" fileNumLimit: 1,\n" +
" fileSingleSizeLimit: 10485760,\n" +
" accept: {\n" +
" extensions: 'jpg,png,gif,bmp,jpeg'\n" +
" },\n" +
" formData: {\n" +
" isup: '1',\n" +
" swfTransform: 'false',\n" +
" bizType: 'null'\n" +
" }\n" +
" });\n" +
" var imageAdd_bcAboutSubList_indeximgUrl = true;\n" +
" $('#bcAboutSubList_indeximgUrlpicker').find('div:eq(0)').addClass('webuploader-pick btn-blue btn-S');\n" +
" $('#tempdiv_bcAboutSubList_indeximgUrl').addClass('tempIMGdiv').append('<ul></ul>');\n" +
" $list.append('<table class=\"temptable\"></table>');\n" +
" var showUploadProgress = function(progress, mycallback, obj) {\n" +
" if (!obj) {\n" +
" obj = $('#bcAboutSubList_indeximgUrl_progress_bar').find('.progress-ty');\n" +
" }\n" +
" if (!$('#bcAboutSubList_indeximgUrl_progress_bar').hasClass('active')) {\n" +
" $('#bcAboutSubList_indeximgUrl_progress_bar').addClass('active');\n" +
" }\n" +
" obj.animate({\n" +
" width: progress + '%'\n" +
" },\n" +
" {\n" +
" duration: 100,\n" +
" easing: 'swing',\n" +
" complete: function(scope, i, elem) {\n" +
" if ( !! mycallback) {\n" +
" mycallback();\n" +
" }\n" +
" }\n" +
" })\n" +
" };\n" +
" var isSupportBase64 = function() {\n" +
" var data = new Image();\n" +
" var support = true;\n" +
" data.onload = data.onerror = function() {\n" +
" if (this.width != 1 || this.height != 1) {\n" +
" support = false;\n" +
" }\n" +
" } //data['src'] = 'data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///ywAAAAAAQABAAACAUwAOw==';\n" +
" return support;\n" +
" };\n" +
" var ratio = window.devicePixelRatio || 1;\n" +
" var thumbnailWidth = 100 * ratio;\n" +
" var thumbnailHeight = 100 * ratio;\n" +
" var bcAboutSubList_indeximgUrladdImgli = function(src, name, xpath, flag) {\n" +
" if (imageAdd_bcAboutSubList_indeximgUrl) {\n" +
" var titleclass = 'hidetitle'; //if(flag==1){titleclass='hidetitle';}\n" +
" var img = '<li><img name=\"' + name + 'img\" class=\"tempimg\" src=\"' + src + '\"><div class=\"' + titleclass + '\"><span';\n" +
" img += ' class=\"titledel\">' + xpath + '</span><span';\n" +
" img += xpath == 0 ? ' style=\"display:none;\"': ' ';\n" +
" img += ' class=\"titledown\">' + xpath + '</span></div></li>';\n" +
" $('#tempdiv_bcAboutSubList_indeximgUrl').find('ul').append(img);\n" +
" }\n" +
" }\n" +
" var addtrFile = function(id, name, text, downsrc, delflag) {\n" +
" var namet = name;\n" +
" if (name.length > 15) {\n" +
" name = name.substring(0, 15) + '...';\n" +
" }\n" +
" var trhtml = '<tr class=\"item\" id=\"' + id + '\"><td title = ' + namet + '>' + name + '</td><td class=\"state\">' + text + '</td><td class=\"icontd\"><span';\n" +
" trhtml += downsrc == 0 ? ' style=\"display:none;\"': ' ';\n" +
" trhtml += ' class=\"down icon-down\">' + downsrc + '</span></td><td class=\"icontd\"><span';\n" +
" trhtml += ' class=\"del icon-cha\" style=\"overflow:hidden;\">' + delflag + '</span></td>';\n" +
" trhtml += '<td></td></tr>';\n" +
" $list.children('table').append(trhtml);\n" +
" }\n" +
" $('#bcAboutSubList_indeximgUrluploader').find('div.btns').append('<input nullMsg=\"請上傳專家圖片\" datatype=\"*\" type=\"hidden\" id= \"bcAboutSubList_indeximgUrldataTypeInp\" />');\n" +
" var reset_bcAboutSubList_indeximgUrl_dataTypeInpVal = function(addOrdel) {\n" +
" var obj = $(\"#bcAboutSubList_indeximgUrldataTypeInp\");\n" +
" if (obj.length > 0) {\n" +
" var objval = obj.val() || '';\n" +
" if (addOrdel == 1) {\n" +
" if (objval == '') {\n" +
" obj.val('1');\n" +
" } else {\n" +
" obj.val(objval.toString() + (parseInt(objval.length) + 1));\n" +
" }\n" +
" } else {\n" +
" if (objval.length <= 1) {\n" +
" obj.val('');\n" +
" } else {\n" +
" obj.val(objval.substr(0, objval.length - 1));\n" +
" }\n" +
" }\n" +
" obj.blur();\n" +
" }\n" +
" }\n" +
" var imgDelReq = function(delpath, spanobj) {\n" +
" $.post('systemController/filedeal.do', {\n" +
" path: delpath,\n" +
" swfTransform: 'false',\n" +
" isdel: \"1\"\n" +
" },\n" +
" function(aj) {\n" +
" var data = JSON.parse(aj);\n" +
" if (data.success) {\n" +
" reset_bcAboutSubList_indeximgUrl_dataTypeInpVal(0);\n" +
" exsitPathArr_bcAboutSubList_indeximgUrl.removeItem(delpath);\n" +
" if (exsitPathArr_bcAboutSubList_indeximgUrl.length > 0) {\n" +
" $list.children('.fordel').children('input').val(exsitPathArr_bcAboutSubList_indeximgUrl.join(','));\n" +
" } else {\n" +
" $list.children('.fordel').children('input').remove();\n" +
" }\n" +
" var myimgli = $(spanobj).closest('li');\n" +
" myimgli.off().find('.hidetitle').off().end().remove();\n" +
" }\n" +
" });\n" +
" }\n" +
" var bcAboutSubList_indeximgUrladdFile = function(file, filepath) {\n" +
" uploader.makeThumb(file,\n" +
" function(error, src) {\n" +
" if (error) {\n" +
" return false;\n" +
" }\n" +
" if (isSupportBase64()) {\n" +
" if (filepath == '') {\n" +
" bcAboutSubList_indeximgUrladdImgli(src, file.id, 0, 0);\n" +
" }\n" +
" } else if (filepath != '') {\n" +
" var actSrc = \"\" + filepath;\n" +
" bcAboutSubList_indeximgUrladdImgli(actSrc, file.id, 0, 0);\n" +
" }\n" +
" },\n" +
" thumbnailWidth, thumbnailHeight);\n" +
" }\n" +
" var updatetdState = function(id, content) {\n" +
" $list.children('table').find('#bcAboutSubList_indeximgUrl' + id).find('.state').text('--' + content + '--');\n" +
" }\n" +
" var bcAboutSubList_indeximgUrl_oneLimit = 0;\n" +
" uploader.on('beforeFileQueued',\n" +
" function(file) {\n" +
" if (bcAboutSubList_indeximgUrl_oneLimit >= 1) {\n" +
" return false;\n" +
" } else {\n" +
" bcAboutSubList_indeximgUrl_oneLimit++;\n" +
" };\n" +
" var currLi = $('#tempdiv_bcAboutSubList_indeximgUrl>ul').find('li:last');\n" +
" if (currLi.length > 0) {\n" +
" currLi.addClass('wait-remove');\n" +
" var abcfile = currLi.find('img').attr('name');\n" +
" if (abcfile.indexOf('name') == 0) {} else {\n" +
" abcfile = abcfile.substring(0, abcfile.length - 3);\n" +
" uploader.removeFile(abcfile)\n" +
" }\n" +
" }\n" +
" });\n" +
" uploader.on('fileQueued',\n" +
" function(file) {\n" +
" var id = 'bcAboutSubList_indeximgUrl' + file.id;\n" +
" var name = file.name;\n" +
" var text = '--等待上傳--';\n" +
" addtrFile(id, name, text, 0, 0);\n" +
" bcAboutSubList_indeximgUrladdFile(file, '');\n" +
" });\n" +
" uploader.on('uploadProgress',\n" +
" function(file, percentage) {\n" +
" var $li = $('#bcAboutSubList_indeximgUrl' + file.id + ' td:last'),\n" +
" $percent = $li.find('.progress .progress-bar');\n" +
" if (!$percent.length) {\n" +
" $percent = $('<div class=\"progress progress-striped active\"><div class=\"progress-bar\" role=\"progressbar\" style=\"width: 0%\"></div></div>').appendTo($li).find('.progress-bar');\n" +
" }\n" +
" updatetdState(file.id, '上傳中');\n" +
" $percent.css('width', percentage * 100 + '%');\n" +
" });\n" +
" uploader.on('uploadStart',\n" +
" function(file) {\n" +
" $('#bcAboutSubList_indeximgUrl_progress_bar').find('.progress-ty').css('width', '1%');\n" +
" var temprd = Math.floor(Math.random() * 7 + 1);\n" +
" if (temprd < 4) {\n" +
" temprd = Number(temprd) + 3\n" +
" }\n" +
" temprd = Number(temprd) * 10;\n" +
" showUploadProgress(temprd,\n" +
" function() {\n" +
" showUploadProgress(Number(temprd) + 15);\n" +
" })\n" +
" });\n" +
" uploader.on('uploadSuccess',\n" +
" function(file, response) {\n" +
" showUploadProgress(100,\n" +
" function() {\n" +
" if (response.success) {\n" +
" $('#bcAboutSubList_indeximgUrl_progress_bar').removeClass('active');\n" +
" updatetdState(file.id, '上傳成功');\n" +
" reset_bcAboutSubList_indeximgUrl_dataTypeInpVal(1);\n" +
" var filepath = response['bcAboutSubList_indeximgUrl'] || response.obj;\n" +
" $('#bcAboutSubList_indeximgUrl' + file.id + ' td:first').append('<input type=\"hidden\" name=\"bcAboutSubList[_index].imgUrl\" value=\"' + filepath + '\" />');\n" +
" bcAboutSubList_indeximgUrladdFile(file, filepath);\n" +
" bcAboutSubList_indeximgUrl_oneLimit = 0;\n" +
" $('#tempdiv_bcAboutSubList_indeximgUrl>ul').find('li.wait-remove').find('.titledel').click()\n" +
" } else {\n" +
" $('#bcAboutSubList_indeximgUrl_progress_bar').removeClass('active');\n" +
" updatetdState(file.id, '上傳出錯' + response.msg);\n" +
" }\n" +
" });\n" +
" });\n" +
" uploader.on('uploadError',\n" +
" function(file, reason) {\n" +
" updatetdState(file.id, '上傳出錯-code:' + reason);\n" +
" });\n" +
" uploader.on('error',\n" +
" function(type) {\n" +
" bcAboutSubList_indeximgUrl_oneLimit--;\n" +
" if (type == 'Q_TYPE_DENIED') {\n" +
" tip('文件類型不識別');\n" +
" }\n" +
" if (type == 'Q_EXCEED_NUM_LIMIT') {\n" +
" tip('文件數量超標');\n" +
" }\n" +
" if (type == 'F_DUPLICATE') {\n" +
" tip('相同文件請不要重複上傳');\n" +
" }\n" +
" if (type == 'F_EXCEED_SIZE') {\n" +
" tip('單個文件大小超標');\n" +
" }\n" +
" if (type == 'Q_EXCEED_SIZE_LIMIT') {\n" +
" tip('文件大小超標');\n" +
" }\n" +
" });\n" +
" uploader.on('uploadComplete',\n" +
" function(file) {\n" +
" $('#bcAboutSubList_indeximgUrl' + file.id).find('.progress').fadeOut('slow');\n" +
" });\n" +
" $('#tempdiv_bcAboutSubList_indeximgUrl').on('mouseenter', 'li',\n" +
" function() {\n" +
" $(this).find('.hidetitle').slideDown(500);\n" +
" });\n" +
" $('#tempdiv_bcAboutSubList_indeximgUrl').on('mouseleave', 'li',\n" +
" function() {\n" +
" $(this).find('.hidetitle').slideUp(500);\n" +
" });\n" +
" $('#tempdiv_bcAboutSubList_indeximgUrl').on('click', 'span',\n" +
" function() {\n" +
" var spanopt = $(this).attr('class');\n" +
" var optpath = $(this).text();\n" +
" if (spanopt.indexOf('titledel') >= 0) {\n" +
" if (0 == optpath) {\n" +
" var optimgname = $(this).parent('.hidetitle').prev('img').attr('name');\n" +
" var img_file_div = 'bcAboutSubList_indeximgUrl' + optimgname.substring(0, optimgname.indexOf('img'));\n" +
" $('#' + img_file_div).find('.del').trigger('click');\n" +
" } else {\n" +
" imgDelReq(optpath, this);\n" +
" }\n" +
" }\n" +
" if (spanopt.indexOf('titledown') >= 0) {\n" +
" var downsrc = \"\" + optpath + '?down=true';\n" +
" location.href = downsrc; //$(this).find('a').click(function(event){event.stopPropagation()});\n" +
" }\n" +
" });\n" +
" $list.on(\"click\", \".down\",\n" +
" function() {\n" +
" var optpath = $(this).text();\n" +
" if (0 != optpath) {\n" +
" var downsrc = \"\" + optpath + '?down=true';\n" +
" location.href = downsrc;\n" +
" }\n" +
" });\n" +
" $list.on(\"click\", \".del\",\n" +
" function() {\n" +
" var delspantext = $(this).text();\n" +
" var itemObj = $(this).closest(\".item\");\n" +
"var strIndex=itemObj.attr(\"id\").indexOf(\"imgUrl\")+6;\n"+//TODO 自定義行,修復刪除圖標綁定方法不可用的問題(默認是寫死的下標)
" var id = itemObj.attr(\"id\").substring(strIndex);\n" +//TODO 自定義strIndex
" var delpath = itemObj.find(\"input[name='bcAboutSubList[_index].imgUrl']\").val();\n" +
" if (undefined == delpath || null == delpath) {\n" +
" delpath = delspantext;\n" +
" if (delspantext == 0) {\n" +
" itemObj.remove();\n" +
" uploader.removeFile(id);\n" +
" var myimgli = $('#tempdiv_bcAboutSubList_indeximgUrl').find(\"img[name='\" + id + \"img']\").closest('li');\n" +
" myimgli.off().find('.hidetitle').off().end().remove();\n" +
" return false;\n" +
" }\n" +
" }\n" +
" $.post('systemController/filedeal.do', {\n" +
" path: delpath,\n" +
" swfTransform: 'false',\n" +
" isdel: \"1\"\n" +
" },\n" +
" function(aj) {\n" +
" var data = JSON.parse(aj);\n" +
" if (data.success) {\n" +
" reset_bcAboutSubList_indeximgUrl_dataTypeInpVal(0);\n" +
" var fordelInput = $list.children('.fordel').children('input');\n" +
" itemObj.remove();\n" +
" if (delspantext == 0) {\n" +
" uploader.removeFile(id);\n" +
" var myimgli = $('#tempdiv_bcAboutSubList_indeximgUrl').find(\"img[name='\" + id + \"img']\").closest('li');\n" +
" myimgli.off().find('.hidetitle').off().end().remove();\n" +
" } else if (fordelInput.length > 0) {\n" +
" exsitPathArr_bcAboutSubList_indeximgUrl.removeItem(delpath);\n" +
" if (exsitPathArr_bcAboutSubList_indeximgUrl.length > 0) {\n" +
" fordelInput.val(exsitPathArr_bcAboutSubList_indeximgUrl.join(','));\n" +
" } else {\n" +
" fordelInput.remove();\n" +
" }\n" +
" }\n" +
" }\n" +
" });\n" +
" });\n" +
" if (location.href.indexOf('load=detail') != -1) {\n" +
" $('#bcAboutSubList_indeximgUrluploader').find('.btns').addClass('virtual-hidden').css('visibility', 'hidden');\n" +
" $list.find('span.del').css('display', 'none');\n" +
" $('#tempdiv_bcAboutSubList_indeximgUrl').find('.titledel').css('display', 'none');\n" +
" }\n" +
"});\n" +
"function mygetFileName(filepath) {\n" +
" var fileNameEndindex = filepath.lastIndexOf('_');\n" +
" var filenameSuffix = filepath.lastIndexOf('.');\n" +
" if (fileNameEndindex < 0) {\n" +
" fileNameEndindex = filepath.length;\n" +
" }\n" +
" if (filepath.lastIndexOf('\\\\') > 0) {\n" +
" return filepath.substring(filepath.lastIndexOf('\\\\') + 1, fileNameEndindex) + filepath.substring(filenameSuffix);\n" +
" } else if (filepath.lastIndexOf('/') > 0) {\n" +
" return filepath.substring(filepath.lastIndexOf('/') + 1, fileNameEndindex) + filepath.substring(filenameSuffix);\n" +
" } else {\n" +
" return filepath;\n" +
" }\n" +
"}\n" +
"function randomFor(n) {\n" +
" var rnd = '';\n" +
" for (var i = 0; i < n; i++) {\n" +
" rnd += Math.floor(Math.random() * 10);\n" +
" }\n" +
" return rnd;\n" +
"}";

var imgId=0;//用於標記子記錄上傳組件id
  2,修改添加和減小子表記錄的方法
//添加子表記錄
$('#addBcAboutSubBtn').bind('click', function(){
var tr = $("#add_bcAboutSub_table_template tr").clone();
$("#add_bcAboutSub_table").append(tr);
//修正name的值
resetTrNum('add_bcAboutSub_table');//jeecg自帶
//處理上傳組件WebUploader,id加上「_」前綴,用於區別
resetTrIdAdd('add_bcAboutSub_table',"_"+(++imgId));//替換模板元素
var temp = code.replace(/_index/g, "_"+imgId );//替換js
//添加相應的上傳js代碼
loadScriptString(temp,"s"+(imgId));
return false;
});

//刪除子表記錄
$('#delBcAboutSubBtn').bind('click', function(){
var selectNum=$("#add_bcAboutSub_table").find("input[name$='ck']:checked");
if(selectNum.length>0){//僅當有選中才操做
selectNum.parent().parent().remove();//刪除對應行

if(imgId>0){//有添加過
for(i=0;i<imgId;i++){
deleteScriptString("s"+(imgId-i));//刪除對應js(文檔中),刪除非新增的行時,也會刪除新增行對應的js代碼,但該代碼已經加載至內存中,因此刪除動做並沒有影響。(加載到內存中的js代碼沒法刪除,因此有內存泄漏的風險,單個頁面不建議大量使用,或者自行添加限制代碼)
}
}

//因tr順序改變,修正name屬性值
resetTrNum('add_bcAboutSub_table');//jeecg自帶
}


return false;
});
  
  3.增長和刪除用到的自定義方法
//增長記錄時修改模板中id的值
function resetTrIdAdd(tableId,imageId) {

$("#"+tableId + " tr").each(function(i) {

$(this).find("div").each(function() {//處理div的id

var $this = $(this), id = $this.attr('id'), val = $this
.val();
if (id != null && id.indexOf("_index") >= 0) {
$this.attr("id", id.replace('_index', imageId));
}
});
$(this).find("input").each(function() {//處理input的id
var $this = $(this), id = $this.attr('id'), val = $this
.val();
if (id != null && id.indexOf("_index") >= 0) {
$this.attr("id", id.replace('_index', imageId));
}
});
});

}
function loadScriptString(code,idName) {//動態添加js代碼,指定id
var script = document.createElement("script");
script.type = "text/javascript";
try {
script.appendChild(document.createTextNode(code));
} catch (ex) {//兼容ie
script.text = code;
}
script.id=idName;//指定id
document.body.appendChild(script);
}

function deleteScriptString(idName) {//根據id動態刪除js代碼
var s=document.getElementById(idName);
if($(s).length>0){//存在才刪除,不然報錯
document.body.removeChild(s);
}
}
總算是能用了,改得超累,有更方便快捷方法的大神還請不吝賜教,寫錯的地方也但願你們幫忙指出,我會第一時間修改,多謝~
相關文章
相關標籤/搜索