在dcat的数据表格中开启了多项选择,在弹出操作表单前,如何获取复选框的选中项

 //数据表格批量操作
        $grid->batchActions(function ($batch) {
            $batch->add(new OpDialog('批量操作按钮'));
        });

        //弹层触发异步表单
        class OpDialog extends BatchAction
        {
            public function render() {
                $modal = Modal::make()->lg()
                    ->title('操作弹层')
                    ->button('操作弹层')
                    ->body(OpForm::make()->payload(['id'=>复选框选中项的值])) //如何向表单传递选中项的值?
                    ->onShow($this->getModalScript());
                return $modal;
            }
        }

        //异步表单
        class OpForm extends Form implements LazyRenderable
        {
            use LazyWidget;
            //表单
            public function form()
            {
                //TODO   表单生成前,需要得到选中项的值进行判断
            }
            //默认数据
            public function default()
            {

            }
        }

《L05 电商实战》
从零开发一个电商项目,功能包括电商后台、商品 & SKU 管理、购物车、订单管理、支付宝支付、微信支付、订单退款流程、优惠券等
《L03 构架 API 服务器》
你将学到如 RESTFul 设计风格、PostMan 的使用、OAuth 流程,JWT 概念及使用 和 API 开发相关的进阶知识。
最佳答案

挺麻烦的这个,因为需要操作 js 来获取,我用自己写的一个包来做例子。

用 Action 生成一个异步表单的 Modal 大概会预渲染下面的 js 代码:

(function () {
    var target = $("#modal-suam4t3gk2"),
        body = target.find(".modal-body");
    target.on("modal:load", function () {
        Dcat.helpers.asyncRender(
            "http://blog.test/admin/dcat-api/render?_current_=http%3A%2F%2Fblog.test%2Fadmin%2Fmedia%3F&disk=public&path=%2F&renderable=Jatdung_MediaManager_Forms_UploadFile&_trans_=media-manager",
            function (html) {
                body.html(html);

                target.trigger("modal:loaded");
            }
        );
    });
    target.on("show.bs.modal", function (event) {
        body.html('<div style="min-height:150px"></div>').loading();

        setTimeout(function () {
            target.trigger("modal:load");
        }, 10);
    });
})();

看 Dcat.helpers.asyncRender 里的 url,可以看到 query 参数有 disk=public&path=%2F,这是我传的 payload 参数,也就是没办法单纯通过 php 代码实现。

找到生成这段代码的地方是一个 trait 的方法(Modal 类引用) Dcat\Admin\Traits\InteractsWithRenderApi::getRenderableScript()

    protected function getRenderableScript()
    {
        if (! $this->getRenderable()) {
            return;
        }

        $url = $this->renderable->getUrl();

        return <<<JS
target.on('{$this->target}:load', function () {
    Dcat.helpers.asyncRender('{$url}', function (html) {
        body.html(html);

        {$this->loadScript}

        target.trigger('{$this->target}:loaded');
    });
});
JS;
    }

所以需要自己继承一个 Modal 类,重写这个方法,新增一点增加参数的代码:

protected function getRenderableScript()
    {
        if (! $this->getRenderable()) {
            return;
        }

        $url = $this->renderable->getUrl();

        return <<<JS
target.on('{$this->target}:load', function () {
   // 新增
    var url = '{$url}';
    var ids = Dcat.grid.selected().toString();  // 格式是 id1,id2
    url += '&ids=' + ids;
    Dcat.helpers.asyncRender(url , function (html) {
        body.html(html);

        {$this->loadScript}

        target.trigger('{$this->target}:loaded');
    });
});
JS;
    }

后面就是在异步表单的 form 方法里获取 payload 里面的 ids 参数,然后用 explode 解成数组,这部分我就不写了。

上面代码是盲写的,可能有错误,不过思路是这样的,因为只是例子所以 url 写了个简单拼接,建议用 js URL 对象

1年前 评论
diyle (楼主) 1年前
讨论数量: 3
Mutoulee

获取复选框选中的 ID 数组:

工具栏《Dcat Admin 中文文档》

1年前 评论

挺麻烦的这个,因为需要操作 js 来获取,我用自己写的一个包来做例子。

用 Action 生成一个异步表单的 Modal 大概会预渲染下面的 js 代码:

(function () {
    var target = $("#modal-suam4t3gk2"),
        body = target.find(".modal-body");
    target.on("modal:load", function () {
        Dcat.helpers.asyncRender(
            "http://blog.test/admin/dcat-api/render?_current_=http%3A%2F%2Fblog.test%2Fadmin%2Fmedia%3F&disk=public&path=%2F&renderable=Jatdung_MediaManager_Forms_UploadFile&_trans_=media-manager",
            function (html) {
                body.html(html);

                target.trigger("modal:loaded");
            }
        );
    });
    target.on("show.bs.modal", function (event) {
        body.html('<div style="min-height:150px"></div>').loading();

        setTimeout(function () {
            target.trigger("modal:load");
        }, 10);
    });
})();

看 Dcat.helpers.asyncRender 里的 url,可以看到 query 参数有 disk=public&path=%2F,这是我传的 payload 参数,也就是没办法单纯通过 php 代码实现。

找到生成这段代码的地方是一个 trait 的方法(Modal 类引用) Dcat\Admin\Traits\InteractsWithRenderApi::getRenderableScript()

    protected function getRenderableScript()
    {
        if (! $this->getRenderable()) {
            return;
        }

        $url = $this->renderable->getUrl();

        return <<<JS
target.on('{$this->target}:load', function () {
    Dcat.helpers.asyncRender('{$url}', function (html) {
        body.html(html);

        {$this->loadScript}

        target.trigger('{$this->target}:loaded');
    });
});
JS;
    }

所以需要自己继承一个 Modal 类,重写这个方法,新增一点增加参数的代码:

protected function getRenderableScript()
    {
        if (! $this->getRenderable()) {
            return;
        }

        $url = $this->renderable->getUrl();

        return <<<JS
target.on('{$this->target}:load', function () {
   // 新增
    var url = '{$url}';
    var ids = Dcat.grid.selected().toString();  // 格式是 id1,id2
    url += '&ids=' + ids;
    Dcat.helpers.asyncRender(url , function (html) {
        body.html(html);

        {$this->loadScript}

        target.trigger('{$this->target}:loaded');
    });
});
JS;
    }

后面就是在异步表单的 form 方法里获取 payload 里面的 ids 参数,然后用 explode 解成数组,这部分我就不写了。

上面代码是盲写的,可能有错误,不过思路是这样的,因为只是例子所以 url 写了个简单拼接,建议用 js URL 对象

1年前 评论
diyle (楼主) 1年前

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!