导出jqgrid表格,csv格式。

function jqgrid2csv(dom,fileName){
	var options={
			csvEnclosure:"\"",
			csvSeparator:",",
			type : "text/csv;charset=utf-8"			
	}
	function fake_click(obj) {  
	    var ev = document.createEvent("MouseEvents");  
	    ev.initMouseEvent("click", true, false, window);   
	    obj.dispatchEvent(ev);  
	}

	function save_local_file(name, data){
		var opts={
				type:options.type
		}
	    var url = window.URL || window.webkitURL || window;
	    var file ;
	    try {
			file = new File([data], name, opts);
		} catch (e) {
			file = new Blob([data], opts);
		}
		
	    var link = document.createElementNS("http://www.w3.org/1999/xhtml", "a");
	    link.href = url.createObjectURL(file);
	    link.download = name;
	    fake_click(link);
	}
	function escapeRegExp (string) {
	      return string.replace(/([.*+?^=!:${}()|\[\]\/\\])/g, "\\$1");
	}
	function replaceAll (s, find, replace) {
	      return s.replace(new RegExp(escapeRegExp(find), 'g'), replace);
	}
	function csvString(csvValue){
		var result = replaceAll(csvValue, options.csvEnclosure, options.csvEnclosure + options.csvEnclosure);
        //if ( result.indexOf(options.csvSeparator) >= 0 || /[\r\n ]/g.test(result) )
        result = options.csvEnclosure + result + options.csvEnclosure;
        return result;		
	}
	
	var lines = $(dom).find(".ui-jqgrid-htable tr,.ui-jqgrid-btable tr");
	var i;
	var data=[];
	for(i=0;i

 代码为完整js,可直接引用。

​此脚本仅依赖于jquery,无其他依赖。

用法:

1.导出不含表头: jqgrid2csv("#grid1_core","filename.csv");

2.导出包含表头: 要求第一个参数传入 表格所在容器的id或者name

    jqgrid2csv("#grid1","filename.csv");

    jqgrid2csv("[name=grid1]","filename.csv");

csv文件默认为utf-8编码,无BOM,可用记事本打开(推荐Notepad++)

你可能感兴趣的:(jquery,jqgrid,导出,csv)