Skip to content

Add sorting support for dot notation column names #6

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from

Conversation

shemgp
Copy link

@shemgp shemgp commented Sep 13, 2016

Sorting was not working. I think it's because sortBy only works with arrays. Here's a way to make sorting work even for dot notation columns. Eg. User->post->subject with column User.post.name.

@aginev
Copy link
Owner

aginev commented Sep 14, 2016

Hello @shemgp,

What is the purpose of this pull requests? If you are trying to do actual search take a look at: https://github.com/aginev/search-filters The purpose of this package is only to display the table structured data, not actually to filter the results...

If using the search filters, take a look at it's readme and you can filter results like so:

$users = $userModel->with(['roles'])->filter(\Request::input('f', []))->orderBy('id', 'desc')->paginate(12);
$grid = new \Datagrid($users, \Request::input('f'));

// Add some columns to datagrid
$grid
    ->setColumn('id', '#', [
        'sortable'    => true,
        'has_filters' => true,
    ])
    ->setColumn('name', trans('admin/users.name'), [
        'sortable'    => true,
        'has_filters' => true,
    ])
    ->setColumn('role_id', trans('admin/users.role'), [
        'sortable'    => false,
        'has_filters' => true,
        'filters'     => Role::all()->pluck('name', 'id'),
        'wrapper'     => function ($value, $row) {
            return $row->getData()->roles->first()->name;
        },
    ])
    ->setColumn('email', trans('admin/users.email'), [
        'sortable'    => true,
        'has_filters' => true,
        'wrapper'     => function ($value, $row) {
            return '<a href="mailto:' . $value . '" target="_blank">' . $value . '</a>';
        },
    ])
    ->setColumn('created_at', trans('admin/users.created_at'), [
        'sortable'    => true,
        'has_filters' => true,
    ])
    ->setColumn('updated_at', trans('admin/users.updated_at'), [
        'sortable'    => true,
        'has_filters' => true,
    ]);

$grid->setActionColumn([
    'wrapper' => function ($value, $row) {
        return '<a href="' . action('Admin\UsersController@edit', $row->id) . '" class="btn btn-xs btn-success" title="' . trans('admin/users.edit_btn') . '"><i class="fa fa-pencil"></i></a>';
    },
]);

@shemgp
Copy link
Author

shemgp commented Sep 15, 2016

I am using datagrid for the view part of the Infyom CRUD (InfyOmLabs/laravel-generator#295). I didn't know that there was aginev/search-filters so I thought the code had a bug as it wasn't searching nor filtering.

@shemgp shemgp closed this Sep 15, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants