在项目中,我们会将查询数据以table的形式显示在页面中,有时候需要导出页面数据或者勾选table的复选框来选择要导出的数据。element-ui的table组件本身提供了方法,当勾选每行前面的复选框时,获得他的id。

        当我们点击导出按钮时,触发方法,发送导出请求,将我们需要导出的数据的id传给后端接口,接口返回要导出的数据流。

我们发送请求的时候需要设置,responseType:'blob',

当我们拿到接口返回来的数据流时,我们需要转换成Blob格式地址,再绑定到window.location.href.

代码如下:

//请求时需要设置responseType
axios({
   method: 'post',
   url: 'xxx',
   params: {id:xxx},
   responseType: 'blob'
}).then(response => {
    ...
//这句是将接口返回的数据流转换成url地址
    window.location.href = window.URL.createObjectURL(new Blob([response.data], { type:     'application/vnd.ms-excel' }))
    ...
})

 

GitHub 加速计划 / eleme / element
54.06 K
14.63 K
下载
A Vue.js 2.0 UI Toolkit for Web
最近提交(Master分支:1 个月前 )
c345bb45 5 个月前
a07f3a59 * Update transition.md * Update table.md * Update transition.md * Update table.md * Update transition.md * Update table.md * Update table.md * Update transition.md * Update popover.md 5 个月前
Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐