Dcat-admin 可設定Grid的行操作按鈕 在第二欄或預設最後一欄


原因:
自製巢狀表格(訂單/訂單細項),但預設行操作按鈕一定要放在最後一欄(appand)
所以做一些修改,可以設定 行操作按鈕改用prepare方式放在前欄(還是會在rowselect之後一欄)

原始設定:跑框了

修改原始代碼:
1.增加Grid的設定值 options['actions_prepend']
檔案位置 \vendor\dcat\laravel-admin\src\Grid.php

 /**
     * Options for grid.
     *
     * @var array
     */
    protected $options = [
        'pagination'          => true,
        'filter'              => true,
        'actions'             => true,
        'quick_edit_button'   => false,
        'edit_button'         => true,
        'view_button'         => true,
        'delete_button'       => true,
        'row_selector'        => true,
        'create_button'       => true,
        'bordered'            => false,
        'table_collapse'      => true,
        'toolbar'             => true,
        'create_mode'         => self::CREATE_MODE_DEFAULT,
        'dialog_form_area'    => ['700px', '670px'],
        'table_class'         => ['table', 'custom-data-table', 'data-table'],
        'scrollbar_x'         => false,
        'actions_class'       => null,
        'batch_actions_class' => null,
        'paginator_class'     => null,
        'actions_prepend'     => false,
    ];

2.增加可設定 行操作按鈕的function PrependActions()
檔案位置 \vendor\dcat\laravel-admin\src\Grid\Concerns\HasActions.php

    /**
     * setting prepend actions.
     *
     * @return $this
     */
    public function PrependActions(bool $prepend = true)
    {
        return $this->option('actions_prepend', $prepend);
    }

3.修改一下預設的function appendActionsColumn()
檔案位置 \vendor\dcat\laravel-admin\src\Grid\Concerns\HasActions.php

    /**
     * Add `actions` column for grid.
     *
     * @return void
     */
    protected function appendActionsColumn()
    {
        if (! $this->options['actions']) {
            return;
        }

        $attributes = ['class' => 'grid__actions__'];

        if($this->options['actions_prepend']) {
            $this->prependColumn(Grid\Column::ACTION_COLUMN_NAME, trans('admin.action'));
        } else {
            $this->addColumn(Grid\Column::ACTION_COLUMN_NAME, trans('admin.action'));
        }

        $this->setHeaderAttributes($attributes)
            ->setAttributes($attributes)
            ->displayUsing($this->getActionClass(), [$this->actionsCallback]);
    }

4.修改一下 gridactions 的class CSS(太寬),把width 20%的屬性刪除
檔案位置 \vendor\celaraze\dcat-extension-plus\src\Support.php

     /**
     * 行操作按钮最右.
     */
    public function gridRowActionsRight()
    {
        if (admin_setting('grid_row_actions_right')) {
            Admin::style(
                <<<CSS
.grid__actions__{
    /*width: 20%;*/
    text-align: right;
}
CSS
            );
        }
    }

5.和selectrow 換一下位置,確保selectrow在第一欄、行操作按鈕在第二欄
檔案位置 \vendor\dcat\laravel-admin\src\Grid.php

/**
     * Build the grid.
     *
     * @return void
     */
    public function build()
    {
        if (! $this->buildable()) {
            $this->callBuilder();
            $this->handleExportRequest();

            $this->appendActionsColumn();
            $this->prependRowSelectorColumn();

            $this->sortHeaders();

            return;
        }

        if ($this->built) {
            return;
        }

        $collection = clone $this->processFilter();

        $this->appendActionsColumn();
        $this->prependRowSelectorColumn();

        Column::setOriginalGridModels($collection);

        $this->columns->map(function (Column $column) use (&$collection) {
            $column->fill($collection);

            $this->columnNames[] = $column->getName();
        });

        $this->buildRows($collection);

        $this->sortHeaders();
    }

這樣行操作按鈕 就完美的呈現在列前了喲,棒的呢!

#Dcat-Admin #grid






你可能感興趣的文章

自學經驗分享

自學經驗分享

APIFlask 初始化專案

APIFlask 初始化專案

MTR04_0802

MTR04_0802






留言討論