$grid->actions(function (Grid\Displayers\Actions $actions) {
$actions->append( new DownloadAction());
});
DownloadAction.php文件的实现:
namespace App\Admin\Extensions;
use Dcat\Admin\Grid\RowAction;
class DownloadAction extends RowAction
{
/**
* 返回字段标题
*
* @return string
*/
public function title()
{
return '下载表格';
}
/**
* 添加JS
*
* @return string
*/
protected function script()
{
return <<
$('.download-btn').on('click', function () {
console.log();
let url = $(this).data('excel');
var x = new XMLHttpRequest();
x.open("GET", url, true);
x.responseType= 'blob';
x.onload=function(e) {
var url = window.URL.createObjectURL(x.response)
var a = document.createElement('a');
a.href = url;
a.download= "表格标题";
a.click()
};
x.send();
});
JS;
}
public function html()
{
$this->setHtmlAttribute(['data-excel' => $this->row->excel_url,'class'=>'download-btn fa fa-download']);
return parent::html();
}
}