CustomBatchAction.php 2.2 KB
<?php
/**
+-----------------------------------------------------------------------------------------------------------------------
 * 管理端扩展 action : 自定义批量操作
+-----------------------------------------------------------------------------------------------------------------------
 *
 * PHP version 7
 *
 * @category  App\Admin\Extensions\Actions
 * @package   App\Admin\Extensions\Actions
 * @author    Richer <yangzi1028@163.com>
 * @date      2021年2月24日09:43:49
 * @copyright 2020-2022 Richer (http://www.Richer.com/)
 * @license   http://www.Richer.com/ License
 * @link      http://www.Richer.com/
 */
namespace App\Admin\Extensions\Actions;

use Encore\Admin\Actions\BatchAction;
use Illuminate\Http\Request;
use Illuminate\Database\Eloquent\Collection;
use App\Admin\Rewrite\Facades\Admin;

/**
 * Class CustomBatchAction
 *
 * @category  App\Admin\Extensions\Actions
 * @package   App\Admin\Extensions\Actions
 * @author    Richer <yangzi1028@163.com>
 * @date      2021年2月24日09:43:49
 * @copyright 2020-2022 Richer (http://www.Richer.com/)
 * @license   http://www.Richer.com/ License
 * @link      http://www.Richer.com/
 */
class CustomBatchAction extends BatchAction
{
    /**
     * {@inheritdoc}
     */
    public function actionScript()
    {
        $warning = $this->warning;//'请选择需要远程开锁的设备!';

        return <<<SCRIPT
        var key = $.admin.grid.selected();

        if (key.length === 0) {
            $.admin.toastr.error('{$warning}', '', {positionClass: 'toast-top-center'});
            return ;
        }

        Object.assign(data, {_key:key});
SCRIPT;
    }

    /**
     * @return string
     */
    public function render()
    {
        $this->addScript();

        $modalId = '';

        if ($this->interactor instanceof Interactor\Form) {
            $modalId = $this->interactor->getModalId();

            if ($content = $this->html()) {
                return $this->interactor->addElementAttr($content, $this->selector);
            }
        }

        return sprintf(
            "<a href='javascript:void(0);' class='%s btn btn-sm $this->style' %s>%s</a>",
            $this->getElementClass(),
            $modalId ? "modal='{$modalId}'" : '',
            $this->name()
        );
    }
}