Skip to content
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

ajaxdatatables and whereclause issue #219

Closed
mpixelz opened this issue Nov 3, 2016 · 11 comments
Closed

ajaxdatatables and whereclause issue #219

mpixelz opened this issue Nov 3, 2016 · 11 comments

Comments

@mpixelz
Copy link

mpixelz commented Nov 3, 2016

hi.. very quick question.. in backpack crud controller.. if i enable the ajaxdatatable.. where do i define my where clause?
currently im doing this:

public function prospective()
    {
    $this->crud->setEntityNameStrings('New Student', 'Student List - Prospective');
    $this->crud->hasAccessOrFail('list');

    $this->data['crud'] = $this->crud;
    $this->data['title'] = ucfirst($this->crud->entity_name_plural);
    $userallocation = User::all()->filter(function (User $user) { return $user->hasPermissionTo('user_allocation'); });
    // // get all entries if AJAX is not enabled
    if (! $this->data['crud']->ajaxTable()) {
        $this->data['entries'] = $this->data['crud']->getEntries()->where('registrationstep', null);
    }
    return view('StudentLists', $this->data)
    ->with('userallocation', $userallocation)
    ;
    }

it works without the ajaxtable as its inside the if (! $this->data['crud']->ajaxTable()) { $this->data['entries'] = $this->data['crud']->getEntries()->where('registrationstep', null); }
but when i enable ajax.. it skips this if statement and shows all the entries without the filter where clause.

@Ghitu
Copy link
Contributor

Ghitu commented Nov 3, 2016

Hi @mpixelz
$this->data['crud']->ajaxTable() returns true if AjaxTable is enabled using $this->data['crud']->enableAjaxTable();

So if you enable AjaxTable then of course it will skip your code in the if statement since !$this->data['crud']->ajaxTable() === false.

Can you tell us more about what do you want to do? Maybe we can help you with that.

Regards

@mpixelz
Copy link
Author

mpixelz commented Nov 3, 2016

this is what im trying to do?
$this->data['entries'] = $this->data['crud']->getEntries()->where('registrationstep', null);
i need to be able to filter the results displayed using the where clause when ajaxDataTables in enabled..

@Ghitu
Copy link
Contributor

Ghitu commented Nov 3, 2016

@mpixelz then if (! $this->data['crud']->ajaxTable()) { should be if ($this->data['crud']->ajaxTable()) {

@mpixelz
Copy link
Author

mpixelz commented Nov 3, 2016

i know i tried that.. but then whats the use of enabling ajaxdatatables if we are still going to do the query manually..

im just trying to have the ->where('registrationstep', null); clause in my ajax enabled method.

@tabacitu
Copy link
Member

tabacitu commented Nov 3, 2016

Hi @mpixelz ,

You can use addClause() for filtering (pretty undocumented feature). You can find the syntax here, under ADVANCED QUERIES.

I think for your case this should work:

-$this->data['entries'] = $this->data['crud']->getEntries()->where('registrationstep', null);
+$this->crud->addClause('where','registrationstep', null);

or maybe

$this->crud->addClause('whereNull','registrationstep');

@mpixelz
Copy link
Author

mpixelz commented Nov 3, 2016

does not work..
it ignores the where clause and shows all the results instead

@Ghitu
Copy link
Contributor

Ghitu commented Nov 3, 2016

@mpixelz
So what you want to achieve is filtering the results making an ajax request to this method from your controller?
Did you changed anything in the JavaScript files for this?

@mpixelz
Copy link
Author

mpixelz commented Nov 7, 2016

yes something like that.. the ajax part is handled automatically by backpack... thats how i am using it on the above mentioned method.. but the enableAjaxDatatables method doesn't work on that and i dont know where i need to put the clause for it to pick it up.

@tabacitu your above mentioned method should have worked as they do make sense but they dont work for some reason.
any other suggestion? or should i just ignore the ajax datatables for now?
thanks.

@tabacitu
Copy link
Member

tabacitu commented Nov 7, 2016

That's odd. The CrudController::search() method, which returns all AJAX results, uses $this->crud->query to build the results. You could

  • do a var_dump($this->crud->query); before and after your $this->crud->addClause();
  • try different clauses and see what actually filters the results;

Cheers!

@mpixelz
Copy link
Author

mpixelz commented Nov 11, 2016

i think it works but i have custom buttons and using it on a overwritten view file.. may be that is causing all this hassle.. im going to implement it on a separate page as see if something changes.
thanks alot thou! really appreciate all the help :)
cheers

@tabacitu
Copy link
Member

Sorry I couldn't be of more help. Cheers!

This was referenced Apr 2, 2020
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

No branches or pull requests

3 participants