Asim
Asim

Reputation: 85

Cakephp ajax pagination for filters search results

I am using cakephp 2.4.5 and using ajax for filteration.I have a refine search on which filters data and fetch results via ajax with pagination.Now what i have to do is when i click next it should load next set of results within the div.Using cakephp paginator helper it redirects to other page,like http://localhost/teacher/Joblistings/getdataviaajax/page:2

Controllrt code for getdataviaajax

public function getdataviaajax(){
        $this->Paginator->settings = $this->paginate;
        $this->layout=false;
        if($this->request->is('ajax')){
            $conditions = array();
        if(!empty($this->request->data['cityid'])){
            $conditions['Joblisting.location_id'] = $this->request->data['cityid'];
        }
        if(!empty($this->request->data['jobid'])){
            $conditions['Joblisting.jobcategory_id']= $this->request->data['jobid'];
        }
        if(!empty($this->request->data['jobid'])){
            $conditions['Joblisting.jobcategory_id'] = $this->request->data['jobid'];   
        }
        if(!empty($this->request->data['experienceid'])){
            $conditions['Joblisting.experiencefrom'] = $this->request->data['experienceid'];
        }
        if(!empty($this->request->data['salaryid'])){
            $conditions['Joblisting.salaryfrom'] = $this->request->data['salaryid'];
        }
        $searchresults = $this->paginate('Joblisting',array($conditions));
        $this->set(compact('searchresults'));
        }else{
            $searchresults=array();
            $this->set(compact('searchresults'));
        }

    }

and searchresults.ctp for fetching results

<div class="col-md-3">
            <div class="table-responsive" style="background-color: #F5F5F5;">
                            <table class="table table-hover course-list-table tablesorter">
                                <thead>
                                <tr>
                                    <th>Refine Searches</th>
                                </tr>
                                </thead>
                                <tbody class="tbodyth">
                                <tr>
                                <th class="course-title">
                                    <?php echo $this->Form->input('city_id',array('label'=>false,'empty'=>'--Select City--','class'=>'selcalss','type'=>'select','style'=>'text-transform:capitalize;','id'=>'cityid'));?>
                                </th>

                                </tr>
                                 <tr>
                                <th class="course-title">
                                    <?php echo $this->Form->input('jobcategory_id',array('label'=>false,'empty'=>'--Select Institute--','class'=>'selcalss','id'=>'jobcatid'));?>
                                </th>
                                </tr>

                                 <tr>
                                <th class="course-title">
                                    <?php echo $this->Form->input('skill_id',array('label'=>false,'empty'=>'--Select Skills--','class'=>'selcalss','id'=>'skillid'));?>
                                </th>
                                </tr>

                                <tr>
                                <th class="course-title">
                                    <?php echo $this->Form->input('experience',array('id'=>'experience','label'=>false,'empty'=>'--Select Experience--','class'=>'selcalss','type'=>'select','options'=>$this->Common->experiencelist()));?>
                                </th>
                                </tr>
                                <tr>
                                <th class="course-title">
                                    <?php echo $this->Form->input('salary',array('id'=>'salaries','label'=>false,'empty'=>'--Expected Salary in Lakhs--','class'=>'selcalss','type'=>'select','options'=>$this->Common->salaries()));?>
                                </th>
                                </tr>

                                <tr>
                                <th class="course-title">
                                    <?php echo $this->Form->input('Search',array('id'=>'button','type'=>'button','label'=>false,'class'=>'btn btn-small pull-right'));?>
                                </th>
                                </tr>


                                </tbody>
                            </table>
                        </div>
         </div>
         <script>
            $(document).ready(function(){

                $('#button').click(function(){
                    $('#fadeid').addClass('fadebackgroundcolor');
                    $('.fadeimgclass').css('display','block');
                    var get_city_id = $('#cityid').val();
                    var get_job_id = $('#jobcatid').val();
                    var get_experience_id = $('#experience').val();
                    var get_salaries_id = $('#salaries').val();

                $.post(
                      '<?php echo Router::url('/Joblistings/getdataviaajax')?>',
                       {cityid: get_city_id,jobid:get_job_id,experienceid:get_experience_id,salaryid:get_salaries_id},
                       function(data){
                            $('#searchresultdata').html(data);
                            $('#fadeid').removeClass('fadebackgroundcolor');
                            $('.fadeimgclass').css('display','none');
                       }
                );
                });

                $('#jobcatid').change(function(){
                var jobcatid = $('#jobcatid').val();
                var url ='<?php echo Router::url('/Skills/getlists/')?>'+$('#jobcatid').val(); 
                $.getJSON(
                    url,   
                    function(data){  
                            $.each(data, function(ind, val){
                                $('#skillid').append('<option value='+val.Skill.skillname +'>'+val.Skill.skillname +'</option>');
                        }); 
                    }
                )

            })
            })
         </script>

The data is reloded from the file as

<?php 
if(sizeof($searchresults)==0){
    echo 'No Job Found Matching';
}
foreach($searchresults as $searches){ ?>
                            <li class="author-block" style="box-shadow: 0px 0px 2px 3px #cfcfcf">
                                <article class="paragraph-wrapper">
                                    <div class="inner">
                                        <header><h5><?php echo $searches['Joblisting']['jobheadline']?></h5></header>
                                        <p>
                                        <b><?php echo $searches['Joblisting']['experiencefrom']?>-<?php echo $searches['Joblisting']['experienceto']?> yrs </b>
                                           <span style="text-transform: capitalize"><?php echo $searches['Location']['cityname']?></span>
                                        </p>
                                        <p>
                                        <b>Keyskills : </b>
                                           Learning, L&D, Training & Development, T&D, Corporate Training...
                                        </p>
                                        <p>
                                        <b>Job Description:</b>
                                        <?php echo $searches['Joblisting']['jobdescription']?>
                                           To head L&D Department To identify training needs To get training modules designed ...
                                        </p>
                                    </div>
                                    <div style="padding: 10px; background-color: rgb(249, 249, 249);">
                                                <p>
                                        <img style="width: 8px;" src="<?php echo Router::url('/img/rssymbol.png') ?>">
                                                    <?php echo $searches['Joblisting']['salaryfrom'];?> To 
                                        <img style="width: 8px;" src="<?php echo Router::url('/img/rssymbol.png') ?>">  

<?php echo $searches['Joblisting']['salaryto']; ?>
<a href="<?php echo Router::url('/Purchasedmasters/detailinpurchasemaster/').$searches['Joblisting']['id']?>" class="btn btn-small pull-right">Pay Now</a>
                                        </p>
                                    </div>
                                </article>
                            </li>
                        <?php } ?>  
                        <div class="paging">
    <?php echo $this->Paginator->prev('< ' . __('previous'), array(), null, array('class' => 'prev disabled'));?>
    <?php echo $this->Paginator->numbers(array('separator' => ''));?>
    <?php echo $this->Paginator->next(__('next') . ' >', array(), null, array('class' => 'next disabled'));?>
    </div>
        ![enter image description here][1]

enter image description here

Upvotes: 0

Views: 876

Answers (2)

Pranav Labhe
Pranav Labhe

Reputation: 1953

You can create your own pager rather than using plugin...

  1. Get All List
  2. Make new list by grouping it as length..

Ex. you got list like this as per page no.

  YourAction(PageNo)
  {
     OldList = {a, b, c, d, e, f, g, h, i, j, k} // It may be outcome of some search result
     PageLength = 2;
     newList = {{a,b},{c,d},{e,f},{g,h},{i,j},{k,''}};
     Return newList[PageNo];
   }

Upvotes: 1

Pranav Labhe
Pranav Labhe

Reputation: 1953

Have you check global anchor tag coding...

It may be appending with attr target=_blnk, if not then you have to bind layoput to view ..

Upvotes: 0

Related Questions