CRUD: Error with beforeColumn and afterColumn methods.

(A) what you did

I wanted to add a column at the beginning of the table, so n my CustomCrudController I added a column like this:

$this->crud->addColumn(['label' => "label", 'type' => "text", 'name' => 'name'])->beforeColumn('firstColumn');

(B) what you expected to happen I expected the Column “Name” to be shown at the beginning of the tabel, just before the “firstColumn” column.

© what happened

ErrorException in Columns.php line 92:
array_splice() expects parameter 2 to be long, string given

(D) what have you already tried to fix it? I already submited a PR: https://github.com/Laravel-Backpack/CRUD/pull/472 Changed the beforeColumn and afterColumn methods on /CRUD/src/PanelTraits/Columns.php:

    /**
     * Moves the recently added column to 'before' the $target_col.
     *
     * @param $target_col
     */
    public function beforeColumn($target_col)
    {
        foreach ($this->columns as $column => $value) {
            if ($value['name'] == $target_col) {
                **$offset = array_search($column, array_keys($this->columns));**
                array_splice($this->columns, **$offset**, 0, [array_pop($this->columns)]);
                break;
            }
        }
    }

    /**
     * Moves the recently added column to 'after' the $target_col.
     *
     * @param $target
     */
    public function afterColumn($target_col)
    {
        foreach ($this->columns as $column => $value) {
            if ($value['name'] == $target_col) {
                **$offset = array_search($column, array_keys($this->columns));**
                array_splice($this->columns, **$offset + 1**, 0, [array_pop($this->columns)]);
                break;
            }
        }
    }

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Comments: 15 (7 by maintainers)

Commits related to this issue

Most upvoted comments

Hi @gmedeiros ,

Yup, done in a different commit and issue. All works now, take a look.

Cheers!