From a07cdc87986267b0017f5cb5e3d20f9ca83f9dc7 Mon Sep 17 00:00:00 2001 From: Kartik Visweswaran Date: Sat, 3 Nov 2018 16:36:25 +0530 Subject: [PATCH] Add ability to have different exportConversions for specific export type fix #843 fix #844 --- CHANGE.md | 5 ++++- src/assets/js/kv-grid-export.js | 15 ++++++++++----- src/assets/js/kv-grid-export.min.js | 4 ++-- 3 files changed, 16 insertions(+), 8 deletions(-) diff --git a/CHANGE.md b/CHANGE.md index 8c51cbc4..db9c8871 100755 --- a/CHANGE.md +++ b/CHANGE.md @@ -3,9 +3,12 @@ Change Log: `yii2-grid` ## Version 3.2.7 -**Date:** _under development_ +**Date:** 03-Nov-2018 - (enh #846): Enhance Grid Grouping to better parse multi level group footers. +- (enh #843, #844): Add ability to have different exportConversions for specific export type. + - For example `from_pdf` and `to_pdf` will be used instead of `from` and `to`. If + only `to_pdf` is set then `from` and `to_pdf` will be used. - (enh #840): Update Persian Translations. - (enh #839): Enhancements to export form functionality. diff --git a/src/assets/js/kv-grid-export.js b/src/assets/js/kv-grid-export.js index 63745bef..47b07b19 100644 --- a/src/assets/js/kv-grid-export.js +++ b/src/assets/js/kv-grid-export.js @@ -2,7 +2,7 @@ * @package yii2-grid * @author Kartik Visweswaran * @copyright Copyright © Kartik Visweswaran, Krajee.com, 2014 - 2018 - * @version 3.2.6 + * @version 3.2.7 * * Grid Export Validation Module for Yii's Gridview. Supports export of * grid data as CSV, HTML, or Excel. @@ -253,19 +253,24 @@ $el.html(rawText); }); var htmlContent = $table.html(); - htmlContent = self.preProcess(htmlContent); + htmlContent = self.preProcess(htmlContent, expType); $table.html(htmlContent); $.each(cssStyles, function (key, value) { $table.find(key).css(value); }); return $table; }, - preProcess: function (content) { - var self = this, conv = self.exportConversions, l = conv.length, processed = content, c; + preProcess: function (content, expType) { + var self = this, conv = self.exportConversions, l = conv.length, processed = content, c, from, to, + fmParam = 'from_' + expType, toParam = 'to_' + expType; if (l > 0) { for (var i = 0; i < l; i++) { c = conv[i]; - processed = $h.replaceAll(processed, c.from, c.to); + from = c[fmParam] !== undefined ? c[fmParam] : (c.from !== undefined ? c.from : ''); + to = c[toParam] !== undefined ? c[toParam] : (c.to !== undefined ? c.to : ''); + if (from.length && to.length) { + processed = $h.replaceAll(processed, from, to); + } } } return processed; diff --git a/src/assets/js/kv-grid-export.min.js b/src/assets/js/kv-grid-export.min.js index 818a6e42..94af19c2 100644 --- a/src/assets/js/kv-grid-export.min.js +++ b/src/assets/js/kv-grid-export.min.js @@ -2,7 +2,7 @@ * @package yii2-grid * @author Kartik Visweswaran * @copyright Copyright © Kartik Visweswaran, Krajee.com, 2014 - 2018 - * @version 3.2.6 + * @version 3.2.7 * * Grid Export Validation Module for Yii's Gridview. Supports export of * grid data as CSV, HTML, or Excel. @@ -12,4 +12,4 @@ * For more JQuery plugins visit http://plugins.krajee.com * For more Yii related demos visit http://demos.krajee.com */ -!function(e){"use strict";var t,o,n="urn:schemas-microsoft-com:office:",r="http://www.w3.org/TR/REC-html40";t={replaceAll:function(e,t,o){return e.split(t).join(o)},isEmpty:function(t,o){return null===t||void 0===t||0===t.length||o&&""===e.trim(t)},popupDialog:function(e,t,o,n){var r=screen.width/2-o/2,i=60,a=window.open("",t,"",!0);return a.close(),window.open(e,t,"toolbar=no, location=no, directories=no, status=yes, menubar=no, scrollbars=no, resizable=no, copyhistory=no, width="+o+", height="+n+", top="+i+", left="+r)},slug:function(e){return e.toLowerCase().replace(/[^\w ]+/g,"").replace(/ +/g,"-")},templates:{html:'{css}{data}',pdf:"{before}\n{data}\n{after}",excel:'{css}{data}',popup:'Grid Export - © Krajee{msg}'}},o=function(t,o){var n=this,r=o.gridOpts,i=o.genOpts;n.$element=e(t),n.gridId=r.gridId,n.$grid=e("#"+n.gridId),n.dialogLib=o.dialogLib,n.messages=r.messages,n.target=r.target,n.exportConversions=r.exportConversions,n.showConfirmAlert=r.showConfirmAlert,n.action=r.action,n.bom=r.bom,n.encoding=r.encoding,n.module=r.module,n.filename=i.filename,n.expHash=i.expHash,n.showHeader=i.showHeader,n.showFooter=i.showFooter,n.showPageSummary=i.showPageSummary,n.$table=n.$grid.find(".kv-grid-table:first"),n.columns=n.showHeader?"td,th":"td",n.alertMsg=o.alertMsg,n.config=o.config,n.popup="",n.listen()},o.prototype={constructor:o,getArray:function(o){var n=this,r=n.clean(o),i=[],a={};return void 0!==n.config.colHeads&&n.config.colHeads.length>0?i=n.config.colHeads:r.find("thead tr th").each(function(o){var r=e(this).text().trim(),a=t.slug(r);i[o]=!n.config.$h.slugColHeads||t.isEmpty(a)?"col_"+o:a}),r.find('tbody tr:has("td")').each(function(t){a[t]={},e(this).children("td").each(function(o){var n=i[o];a[t][n]=e(this).text().trim()})}),a},setPopupAlert:function(e){var o=this;if(void 0!==o.popup.document)if(arguments.length&&arguments[1]){var n=o.popup.document.getElementsByTagName("body");setTimeout(function(){n[0].innerHTML=e},1200)}else{var r=t.templates.popup.replace("{msg}",e);o.popup.document.write(r)}},processExport:function(e,o){var n=this;setTimeout(function(){t.isEmpty(o)?n[e]():n[e](o)},100)},listenClick:function(e){var o=this,n=arguments.length>1?arguments[1]:"",r=window[o.dialogLib];o.$element.off("click.gridexport").on("click.gridexport",function(i){if(i.stopPropagation(),i.preventDefault(),!o.showConfirmAlert)return void o.processExport(e,n);var a=o.messages,s=t.isEmpty(o.alertMsg)?"":o.alertMsg,l=t.isEmpty(a.allowPopups)?"":a.allowPopups,c=t.isEmpty(a.confirmDownload)?"":a.confirmDownload,p="";return p=s.length&&l.length?s+"\n\n"+l:!s.length&&l.length?l:s.length&&!l.length?s:"",c.length&&(p=p+"\n\n"+c),t.isEmpty(p)?void 0:(r.confirm(p,function(t){t&&o.processExport(e,n),i.preventDefault()}),!1)})},listen:function(){var e=this;e.$element.hasClass("export-csv")&&e.listenClick("exportTEXT","csv"),e.$element.hasClass("export-txt")&&e.listenClick("exportTEXT","txt"),e.$element.hasClass("export-html")&&e.listenClick("exportHTML"),e.$element.hasClass("export-xls")&&e.listenClick("exportEXCEL"),e.$element.hasClass("export-json")&&e.listenClick("exportJSON"),e.$element.hasClass("export-pdf")&&e.listenClick("exportPDF")},clean:function(t){var o,n=this,r=n.$table.clone(),i=n.$element.data("cssStyles")||{},a=n.$table.closest(".kv-grid-container"),s=function(e){r.find(e+"."+n.gridId).remove()};o=a.hasClass("kv-grid-wrapper")?a.closest(".floatThead-wrapper").find(".kv-thead-float thead"):a.find(".kv-thead-float thead"),o.length&&(o=o.clone(),r.find("thead").before(o).remove()),r.find("tr.filters").remove(),r.find("th").removeAttr("rowspan"),r.find("th").find("a").each(function(){e(this).contents().unwrap()}),r.find("form,input,textarea,select,script").remove(),r.find("[onclick]").removeAttr("onclick"),r.find('a[href*="javascript"]').attr("href","#"),n.showHeader||r.children("thead").remove(),n.showPageSummary||s(".kv-page-summary-container"),n.showFooter||s(".kv-footer-container"),n.showCaption||s(".kv-caption-container"),r.find(".skip-export").remove(),r.find(".skip-export-"+t).remove(),r.find(".strip-tags-export").each(function(){var t=e(this),o=t.text();t.html(o)});var l=r.html();return l=n.preProcess(l),r.html(l),e.each(i,function(e,t){r.find(e).css(t)}),r},preProcess:function(e){var o,n=this,r=n.exportConversions,i=r.length,a=e;if(i>0)for(var s=0;i>s;s++)o=r[s],a=t.replaceAll(a,o.from,o.to);return a},download:function(o,n){var r,i,a=this,s=a.$element,l=s.attr("data-mime")||"text/plain",c=window.yii,p=s.attr("data-hash")||"",d=t.isEmpty(a.config)?{}:a.config,f=a.target,h=function(t,o){return e("",{name:t,value:o,type:"hidden"})};"json"===o&&d.jsonReplacer&&delete d.jsonReplacer,r=c?h(c.getCsrfParam()||"_csrf",c.getCsrfToken()||null):null,i="_popup"===f,i&&(f="kvDownloadDialog",a.popup=t.popupDialog("",f,350,120),a.popup.focus(),a.setPopupAlert(a.messages.downloadProgress)),e("
",{action:a.action,target:f,method:"post",css:{display:"none"}}).append(h("export_filetype",o),h("export_filename",a.filename)).append(h("export_encoding",a.encoding),h("export_bom",a.bom?1:0)).append(h("export_content",n),h("module_id",a.module),r).append(h("export_mime",l),h("export_hash",p)).append(h("export_config",JSON.stringify(d))).appendTo("body").submit().remove(),i&&a.setPopupAlert(a.messages.downloadComplete,!0)},exportHTML:function(){var o,n=this,r=n.clean("html"),i=n.config,a=i.cssFile?i.cssFile:[],s="";o=t.templates.html.replace("{encoding}",n.encoding),e.each(a,function(e,t){s+='\n'}),o=o.replace("{css}",s).replace("{data}",e("
").html(r).html()),n.download("html",o)},exportPDF:function(){var o=this,n=o.clean("pdf"),r=t.isEmpty(o.config.contentBefore)?"":o.config.contentBefore,i=t.isEmpty(o.config.contentAfter)?"":o.config.contentAfter,a=o.config.css,s=t.templates.pdf.replace("{css}",a).replace("{before}",r).replace("{after}",i).replace("{data}",e("
").html(n).html());o.download("pdf",s)},exportTEXT:function(t){var o=this,n=o.clean(t),r=n.find("tr:has("+o.columns+")"),i=String.fromCharCode(11),a=String.fromCharCode(0),s='"'+o.config.colDelimiter+'"',l='"'+o.config.rowDelimiter+'"',c='"'+r.map(function(t,n){var r=e(n),a=r.find(o.columns);return a.map(function(t,o){var n=e(o),r=n.text().trim();return r.replace(/"/g,'""')}).get().join(i)}).get().join(a).split(a).join(l).split(i).join(s)+'"';o.download(t,c)},exportJSON:function(){var e=this,t=e.getArray("json");t=JSON.stringify(t,e.config.jsonReplacer,e.config.indentSpace),e.download("json",t)},exportEXCEL:function(){var o,n,r=this,i=r.clean("xls"),a=r.config,s=a.cssFile&&r.config.cssFile.length?'':"";i.find("td[data-raw-value]").each(function(){n=e(this),(n.css("mso-number-format")||0===n.css("mso-number-format")||"0"===n.css("mso-number-format"))&&n.html(n.attr("data-raw-value")).removeAttr("data-raw-value")}),o=t.templates.excel.replace("{encoding}",r.encoding).replace("{css}",s).replace("{worksheet}",r.config.worksheet).replace("{data}",e("
").html(i).html()).replace(/"/g,"'"),r.download("xls",o)}},e.fn.gridexport=function(t){var n=Array.apply(null,arguments);return n.shift(),this.each(function(){var r=e(this),i=r.data("gridexport"),a="object"==typeof t&&t;i||r.data("gridexport",i=new o(this,e.extend({},e.fn.gridexport.defaults,a,e(this).data()))),"string"==typeof t&&i[t].apply(i,n)})},e.fn.gridexport.defaults={dialogLib:"krajeeDialog"},e.fn.gridexport.Constructor=o}(window.jQuery); \ No newline at end of file +!function(e){"use strict";var t,o,n="urn:schemas-microsoft-com:office:",r="http://www.w3.org/TR/REC-html40";t={replaceAll:function(e,t,o){return e.split(t).join(o)},isEmpty:function(t,o){return null===t||void 0===t||0===t.length||o&&""===e.trim(t)},popupDialog:function(e,t,o,n){var r=screen.width/2-o/2,i=60,a=window.open("",t,"",!0);return a.close(),window.open(e,t,"toolbar=no, location=no, directories=no, status=yes, menubar=no, scrollbars=no, resizable=no, copyhistory=no, width="+o+", height="+n+", top="+i+", left="+r)},slug:function(e){return e.toLowerCase().replace(/[^\w ]+/g,"").replace(/ +/g,"-")},templates:{html:'{css}{data}',pdf:"{before}\n{data}\n{after}",excel:'{css}{data}',popup:'Grid Export - © Krajee{msg}'}},o=function(t,o){var n=this,r=o.gridOpts,i=o.genOpts;n.$element=e(t),n.gridId=r.gridId,n.$grid=e("#"+n.gridId),n.dialogLib=o.dialogLib,n.messages=r.messages,n.target=r.target,n.exportConversions=r.exportConversions,n.showConfirmAlert=r.showConfirmAlert,n.action=r.action,n.bom=r.bom,n.encoding=r.encoding,n.module=r.module,n.filename=i.filename,n.expHash=i.expHash,n.showHeader=i.showHeader,n.showFooter=i.showFooter,n.showPageSummary=i.showPageSummary,n.$table=n.$grid.find(".kv-grid-table:first"),n.columns=n.showHeader?"td,th":"td",n.alertMsg=o.alertMsg,n.config=o.config,n.popup="",n.listen()},o.prototype={constructor:o,getArray:function(o){var n=this,r=n.clean(o),i=[],a={};return void 0!==n.config.colHeads&&n.config.colHeads.length>0?i=n.config.colHeads:r.find("thead tr th").each(function(o){var r=e(this).text().trim(),a=t.slug(r);i[o]=!n.config.$h.slugColHeads||t.isEmpty(a)?"col_"+o:a}),r.find('tbody tr:has("td")').each(function(t){a[t]={},e(this).children("td").each(function(o){var n=i[o];a[t][n]=e(this).text().trim()})}),a},setPopupAlert:function(e){var o=this;if(void 0!==o.popup.document)if(arguments.length&&arguments[1]){var n=o.popup.document.getElementsByTagName("body");setTimeout(function(){n[0].innerHTML=e},1200)}else{var r=t.templates.popup.replace("{msg}",e);o.popup.document.write(r)}},processExport:function(e,o){var n=this;setTimeout(function(){t.isEmpty(o)?n[e]():n[e](o)},100)},listenClick:function(e){var o=this,n=arguments.length>1?arguments[1]:"",r=window[o.dialogLib];o.$element.off("click.gridexport").on("click.gridexport",function(i){if(i.stopPropagation(),i.preventDefault(),!o.showConfirmAlert)return void o.processExport(e,n);var a=o.messages,s=t.isEmpty(o.alertMsg)?"":o.alertMsg,l=t.isEmpty(a.allowPopups)?"":a.allowPopups,c=t.isEmpty(a.confirmDownload)?"":a.confirmDownload,p="";return p=s.length&&l.length?s+"\n\n"+l:!s.length&&l.length?l:s.length&&!l.length?s:"",c.length&&(p=p+"\n\n"+c),t.isEmpty(p)?void 0:(r.confirm(p,function(t){t&&o.processExport(e,n),i.preventDefault()}),!1)})},listen:function(){var e=this;e.$element.hasClass("export-csv")&&e.listenClick("exportTEXT","csv"),e.$element.hasClass("export-txt")&&e.listenClick("exportTEXT","txt"),e.$element.hasClass("export-html")&&e.listenClick("exportHTML"),e.$element.hasClass("export-xls")&&e.listenClick("exportEXCEL"),e.$element.hasClass("export-json")&&e.listenClick("exportJSON"),e.$element.hasClass("export-pdf")&&e.listenClick("exportPDF")},clean:function(t){var o,n=this,r=n.$table.clone(),i=n.$element.data("cssStyles")||{},a=n.$table.closest(".kv-grid-container"),s=function(e){r.find(e+"."+n.gridId).remove()};o=a.hasClass("kv-grid-wrapper")?a.closest(".floatThead-wrapper").find(".kv-thead-float thead"):a.find(".kv-thead-float thead"),o.length&&(o=o.clone(),r.find("thead").before(o).remove()),r.find("tr.filters").remove(),r.find("th").removeAttr("rowspan"),r.find("th").find("a").each(function(){e(this).contents().unwrap()}),r.find("form,input,textarea,select,script").remove(),r.find("[onclick]").removeAttr("onclick"),r.find('a[href*="javascript"]').attr("href","#"),n.showHeader||r.children("thead").remove(),n.showPageSummary||s(".kv-page-summary-container"),n.showFooter||s(".kv-footer-container"),n.showCaption||s(".kv-caption-container"),r.find(".skip-export").remove(),r.find(".skip-export-"+t).remove(),r.find(".strip-tags-export").each(function(){var t=e(this),o=t.text();t.html(o)});var l=r.html();return l=n.preProcess(l,t),r.html(l),e.each(i,function(e,t){r.find(e).css(t)}),r},preProcess:function(e,o){var n,r,i,a=this,s=a.exportConversions,l=s.length,c=e,p="from_"+o,d="to_"+o;if(l>0)for(var f=0;l>f;f++)n=s[f],r=void 0!==n[p]?n[p]:void 0!==n.from?n.from:"",i=void 0!==n[d]?n[d]:void 0!==n.to?n.to:"",r.length&&i.length&&(c=t.replaceAll(c,r,i));return c},download:function(o,n){var r,i,a=this,s=a.$element,l=s.attr("data-mime")||"text/plain",c=window.yii,p=s.attr("data-hash")||"",d=t.isEmpty(a.config)?{}:a.config,f=a.target,h=function(t,o){return e("",{name:t,value:o,type:"hidden"})};"json"===o&&d.jsonReplacer&&delete d.jsonReplacer,r=c?h(c.getCsrfParam()||"_csrf",c.getCsrfToken()||null):null,i="_popup"===f,i&&(f="kvDownloadDialog",a.popup=t.popupDialog("",f,350,120),a.popup.focus(),a.setPopupAlert(a.messages.downloadProgress)),e("",{action:a.action,target:f,method:"post",css:{display:"none"}}).append(h("export_filetype",o),h("export_filename",a.filename)).append(h("export_encoding",a.encoding),h("export_bom",a.bom?1:0)).append(h("export_content",n),h("module_id",a.module),r).append(h("export_mime",l),h("export_hash",p)).append(h("export_config",JSON.stringify(d))).appendTo("body").submit().remove(),i&&a.setPopupAlert(a.messages.downloadComplete,!0)},exportHTML:function(){var o,n=this,r=n.clean("html"),i=n.config,a=i.cssFile?i.cssFile:[],s="";o=t.templates.html.replace("{encoding}",n.encoding),e.each(a,function(e,t){s+='\n'}),o=o.replace("{css}",s).replace("{data}",e("
").html(r).html()),n.download("html",o)},exportPDF:function(){var o=this,n=o.clean("pdf"),r=t.isEmpty(o.config.contentBefore)?"":o.config.contentBefore,i=t.isEmpty(o.config.contentAfter)?"":o.config.contentAfter,a=o.config.css,s=t.templates.pdf.replace("{css}",a).replace("{before}",r).replace("{after}",i).replace("{data}",e("
").html(n).html());o.download("pdf",s)},exportTEXT:function(t){var o=this,n=o.clean(t),r=n.find("tr:has("+o.columns+")"),i=String.fromCharCode(11),a=String.fromCharCode(0),s='"'+o.config.colDelimiter+'"',l='"'+o.config.rowDelimiter+'"',c='"'+r.map(function(t,n){var r=e(n),a=r.find(o.columns);return a.map(function(t,o){var n=e(o),r=n.text().trim();return r.replace(/"/g,'""')}).get().join(i)}).get().join(a).split(a).join(l).split(i).join(s)+'"';o.download(t,c)},exportJSON:function(){var e=this,t=e.getArray("json");t=JSON.stringify(t,e.config.jsonReplacer,e.config.indentSpace),e.download("json",t)},exportEXCEL:function(){var o,n,r=this,i=r.clean("xls"),a=r.config,s=a.cssFile&&r.config.cssFile.length?'':"";i.find("td[data-raw-value]").each(function(){n=e(this),(n.css("mso-number-format")||0===n.css("mso-number-format")||"0"===n.css("mso-number-format"))&&n.html(n.attr("data-raw-value")).removeAttr("data-raw-value")}),o=t.templates.excel.replace("{encoding}",r.encoding).replace("{css}",s).replace("{worksheet}",r.config.worksheet).replace("{data}",e("
").html(i).html()).replace(/"/g,"'"),r.download("xls",o)}},e.fn.gridexport=function(t){var n=Array.apply(null,arguments);return n.shift(),this.each(function(){var r=e(this),i=r.data("gridexport"),a="object"==typeof t&&t;i||r.data("gridexport",i=new o(this,e.extend({},e.fn.gridexport.defaults,a,e(this).data()))),"string"==typeof t&&i[t].apply(i,n)})},e.fn.gridexport.defaults={dialogLib:"krajeeDialog"},e.fn.gridexport.Constructor=o}(window.jQuery); \ No newline at end of file