I need to create filter using textbox. Post are paginated and I need to call them via ajax. I have a template that will only reload once hitting Enter key.
this is the code inside my PageController.php
public function filter( HTTPRequest $request) {
$tags = $_REQUEST['TAG'];
if ($tags) {
# code...
$blogpost = BlogPost::get()
->leftJoin('BlogPost_Tags', '"BlogPost"."ID" = "BlogPost_Tags"."BlogPostID"')
->leftJoin('BlogTag', '"BlogPost_Tags"."BlogTagID" = "BlogTag"."ID"')
->where(' "BlogTag"."Title" LIKE \'%'.$tags.'%\' ');
}
$pages = new PaginatedList($blogpost, $this->getRequest());
$pages->setPageLength(5);
return $this->$pages->renderWith( ['PostSummary'] );
}
This is the code inside my JS doing the ajax thing
$("#tag_field").on('keyup',function(e){
if (e.keyCode == 13) {
var loc = window.location + '/filter/';
var div = $('.paginated-post').html();
// showSpinner('.listing-items');
//console.log( $('#FilterForm_FilterForm_category').val() + " " + $('#FilterForm_FilterForm_trail').val() );
$.ajax({
type: "POST",
url: loc,
data: {
TAG: $('#tag_field').val(),
//cost: $('#FilterForm_FilterForm_cost').val(),
// OtherID: $('#FilterForm_FilterForm_trail').val()
},
success: function(html){
$('.paginated-post').html(div)
}
});
}
});
Any help is greatly appreciated. Thank you so much