dcat admin 添加自定义操作按钮

$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();

}

}

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容