首页 > 解决方案 > Laravel-admin 中网格的 RowAction 使用相同的类标识符。如何为每一行设置唯一的类标识符的生成?

问题描述

有一个自定义按钮类(从Encore\Admin\Actions\RowAction 扩展)。

该类有一个确认窗口,其中包含每行的唯一信息。

    / **
     * @return void
     * /
    public function dialog()
    {
        $model = Categories::where('id',$this->getKey())->with('users')->get();
        $this->question(trans('admin.delete_confirm'), 'Comment:'.$model[0]->comment, ['confirmButtonColor' => '#d33']);
    }

当脚本形成调用对话框时,它们具有相同的类标识符(例如,“. grid-row-action-5dca9548c28226038 ”),但文本不同。

...
$ ('.grid-row-action-5dca9548c28226038').off('click').on('click', function() {
...
"text": "Comment: text 1",
...
}

$ ('.grid-row-action-5dca9548c28226038').off('click').on('click', function() {
...
"text": "Comment: text 2",
...
}

$ ('.grid-row-action-5dca9548c28226038').off('click').on('click', function() {
...
"text": "Comment: text 3",
...
}
...

如何让 Laravel 管理员为每一行生成一个唯一标识符?

标签: phplaravel-admin

解决方案


解决方案是重写获取选择器的方法。

    /**
     * @var bool
     */
    protected $multiplePrefix = true;

...

    /**
     * @param string $prefix
     *
     * @return mixed|string
     */
    public function selector($prefix)
    {
        if (isset($this->multiplePrefix)){
            return $this->getOptionalPrefix($prefix);
        } elseif (is_null($this->selector)) {
            return static::makeSelector(get_called_class(), $prefix);
        }

        return $this->selector;
    }

    /**
     * @param $prefix
     * @return string
     */
    protected function getOptionalPrefix($prefix)
    {
        if (is_null($this->selector)) {
            $this->selector = uniqid($prefix) . mt_rand(1000, 9999);
        }
        return $this->selector;
    }

推荐阅读