添加自定义导出按钮
<span class="btn btn-primary " style="float:left;margin-right:5px;" data-bind="click:exportExcelCustom">
<i class="fa fa-file-excel-o" style="marginright:3px;"></i><@spring.message "导出"/></span>
扩展 viewModel 对象并添加 exportExcelCustom() 方法
<script type="text/javascript">
var viewModel = Hap.createGridViewModel("#grid" , {
/*自定义导出*/
exportExcelCustom: function() {
var exportConfig = {};
var columns = [];
var index = 0;
var grid = $("#grid").data("kendoGrid"); //获取kendoGrid对象
console.log(grid)
for (var i = 0; i < grid.columns.length; i++) { //遍历所有字段
if (grid.columns[i].field != null && i!=7 && i!=8 && i!=9 && i!=13) {
var columnInfo = {};
columnInfo["name"] = grid.columns[i].field; //获取字段name
columnInfo["title"] = grid.columns[i].title; //获取字段实际显示标题
columnInfo["width"] = grid.columns[i].width*0.5; //获取字段显示宽度
var align = $('tbody').find('tr')[0].getElementsByTagName('td')
[i].style.textAlign; //获取对其⽅方式
if (align != null || align != '') {
columnInfo["align"] = align;
}
if (dataSource.options.schema.model.fields[grid.columns[i].field] != null) {//获取字段类型
columnInfo["type"] =
dataSource.options.schema.model.fields[grid.columns[i].field].type;
console.log("获取的数据类型:"+dataSource.options.schema.model.fields[grid.columns[i].field].type)
}
columns[index] = columnInfo; //将数据导⼊入columns中
console.log("插入的数据:"+columnInfo)
index++;
}
}
exportConfig["columnsInfo"] = columns; //将columns数据导⼊入exportConfig中
exportConfig["param"] = Hap.prepareQueryParameter(this.model.toJSON()); //获取⽹网⻚页限制条件
exportConfig["fileName"] = "订单"; //设置⽂文件名
var $inputImg = $('<input>').attr({name: "config", value:
kendo.stringify(exportConfig)}); //将⻚页⾯面信息传到$inputImg中,定义input,并设置name和value属性
var
$inputToken=$('<input>').attr({name:"${_csrf.parameterName}",value:"${_csrf.token}",
type:"hidden"}); //传⼊入token,以实现跨域访问,定义input
var $form = $("<form>"); //定义form
$form.attr({ //设置form的target,method,action属性
target: '_self',
method: 'post',
action: '${base.contextPath}/i/edi/iftmin/export'
}).append($inputImg); //在form中添加$inputImg,添加input域
$form.append($inputToken); //在form中添加$inputToken
$form.id='smbForm'; //设置form的id
$("#batchDiv").empty().append($form); //在batchDiv中添加该form
$($form).submit(); //提交form
$("#batchDiv").empty(); //置空batchDiv
},
}
);
</script>
在body最后面添加一个DIV
<div id="batchDiv"></div>
在控制器中添加处理逻辑
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;
@Autowired
private ObjectMapper objectMapper;
@Autowired private IExportService excelService;
@RequestMapping(value = "/i/edi/iftmin/export")
public void exportExcelCustom(HttpServletRequest request, @RequestParam String
config,
HttpServletResponse httpServletResponse) throws
IOException {
IRequest requestContext = createRequestContext(request);
JavaType type = objectMapper.getTypeFactory().constructParametrizedType(ExportConfig.class,
ExportConfig.class, EdiIftmin.class, ColumnInfo.class);
ExportConfig<EdiIftmin, ColumnInfo> exportConfig =
objectMapper.readValue(config, type);
excelService.exportAndDownloadExcel("com.hand.tms.orderManage.mapper.EdiIftminMapper.exportExcel", exportConfig, request, httpServletResponse,
requestContext);
}