Partial Ajax Pagination in cakePHP - ajax

I'm trying to add ajax pagination into a view of my application.
I followed this tutorial: http://book.cakephp.org/view/1600/Ajax-Pagination
So, in this tutorial you always reload the whole content of the view by clicking the pagination links.
But what I want is, that it only reloads the part of the site which includes the paginated data.

I found a solution for myself:
I made a view which only contains the ajax pagination and included it via ajax request:
<?php $this->Html->scriptStart(array('inline' => false));?>
$(document).ready(function() {
<?php
echo $this->Js->request(array(
'controller' => 'topics',
'action' => 'index',
$location['Location']['id']
), array(
'async' => true,
'update' => '#ajax_topics',
));
?>
});
<?php $this->Html->scriptEnd();?>

Related

cakephp passing selection to JS Helper

I want to get the selected schoolFilter form value and place in [SELECTED VALUE HERE].
$data = $this->Js->get('#SchoolFilter')->serializeForm(array('isForm' => true, 'inline' => true));
$this->Js->get('#SchoolSchoolId')->event(
'change', $this->Js->request(
array('action' => 'assign_school_ads/', [SELECTED VALUE HERE], array(
'update' => '#results',
'data' => $data,
'async' => true,
'dataExpression' => true,
'method' => 'POST'
)
)
);
// School Filter
$schoolFilter = $this->Form->create('School', array('id' => 'SchoolFilter');
$schoolFilter .= $this->Form->input('school_id', array('label'=>'Schools', 'empty'=>'- select -');
$schoolFilter .= $this->Form->end();
I have seen variations on this question but without any clear answer, except to just forget using JS Helper. Is it possible within the context of JS Helper? And if not, can I get the value using regular JQuery, then inject it into JS Helper.
Use following code :-
$this->Js->get('#SchoolFilter');
$this->Js->event('change', $this->Js->request(array('controller'=>'put-ur-controller-ame-here','action' => 'assign_school_ads'),array('async' => true,'update' => '#results','method' => 'post','dataExpression'=>true,'data'=> $this->Js->serializeForm(array('isForm' => true,'inline' => true)))));
the serialize() function sends the form data to the php action so we can see which option was selected and decide what to update in the ajax call.
the form data will be found in $this->data in the action (just like after a form has been submited).
Don't forget to add $this->Js->writeBuffer(); in your layout just before the body closing tag. Otherwise all the ajax code will not be added to your page.

CakePHP Observe field for validation check(already exists)

i have already user observe field in cakephp 1.3 Now i want to do the same thing in cakephp 2.0
I have one form.there is a one field named email. When user insert email. onblur there is a popup one message that displays "email already exists" or "right"
for that i have write below code in my projects. the below code is my add file code
<?php $options = array('url' => array( 'controller' => 'users', 'action' => 'is_exist'), 'update' => 'email_exist');
echo $this->ajax->observeField('UserEmailId',array('url' => array( 'controller' => 'users', 'action' => 'is_exist'), 'update' => 'email_exists')); ?>
In my controller i have created one function like
public function is_exist($id = null)
{
$result = "yes";
$this->set('existdata',$result);
}
i have also created is_exists.ctp fiel in view/uers.
i dont know why its not working.
i did the same thing in Cakephp 1.3 and its working file but not in cakephp 2.0
can anyone tell me how i implement this ?
thanks in advance
because ajax helper is not supported in cakephp 2.
you need to learn how to implement it using the js helper
check this link : js helper

validating form which is rendered using renderPartial in yii

I have more then one forms on one page and I am rendering one form using renderPartial, now if I want to validate it using ajax validation it don't work.
view code
<?php $form=$this->beginWidget('CActiveForm', array(
'id'=>'bill-shipp-form',
'action'=>CController::createUrl('cart/index'),
'enableAjaxValidation'=>true,
'focus'=>array($billingShippingInfo,'first_name_b'),
//'enableClientValidation'=>true,
'clientOptions' => array(
'validateOnSubmit' => true,
'validateOnChange'=>false,
'afterValidate'=>'js:postBillShipp'
),
)); ?>
and in cart/index I have
if(isset($_POST['ajax']) && $_POST['ajax']==='bill-shipp-form')
{
echo CActiveForm::validate($billingShippingInfo);
Yii::app()->end();
}
Thanks in advance
You should use 4th parameter for render partial:
$this->render('view',$data,false,TRUE);
The 4th paramater is processOutput and you should set it to true
API

cakephp elements and Ajax update

here is what i am trying to achieve...
i have a index view in which i have used the ajax helper not the cakephp one but an external one from http://www.cakephp.bee.pl/
i have succefully created an accordion using this helper.
now inside the accordion i have an addbutton which submits a form via ajax.
what i would like to do is when i submit the ajax form. i would also like to update the accordion.
now the accordion is also created by using the ajax helper.
Can this be done without a page refresh
ajax submit form code :
<div class="qnotesform">
<?php echo $this->Form->create('Qnote');?>
<ul class="qnotelist">
<li><?php echo $this->Form->input('Qnote.subject', array('label'=>'Title', 'rows' => '1')); ?></li>
<li> <?php echo $this->Form->input('Qnote.body', array('label'=>'Header', 'rows' => '1')); ?></li>
<?php echo $this->Form->hidden('Qnote.user_id', array('value'=>Authsome::get('id'))); ?>
<?php echo $this->Form->hidden('Step.0.user_id', array('value'=>Authsome::get('id'))); ?>
<li><?php echo $this->Form->input('Step.0.body', array('label'=>'Steps' ,'class' => 'step', 'rows' => '1')); ?></li>
</ul>
<?php echo $ajax->submit(
'Submit', array(
'url' => array(
'controller'=>'qnotes',
'action'=>'add')
));
how can i achieve this .
could someone please point me the right direction
another thing that i want to clear out is. If i want to be working one page . and all crud
action to be used without a page refresh.
does anybody have a working example for the above senarios .
or let me make it simple.
how to refresh a div without refreshing the page.
Solution:
code
<?php echo $ajax->submit('Submit', array(
'url'=> array(
'controller'=>'qnotes',
'action'=>'add'),
'update'=>'leftdiv',
'after'=>'$("#accordion").accordion({autoHeight: false, collapsible: true , header: "h3"});alert("after");',
));
however now I am facing another issue now,
I am updating the accordion, everything works fine but as soon as update the accordion.
the accordion loses its style class and stops working.
I only see links then. any idea how.
It seems that one of the options in the array of the submit method is 'complete' which should allow you to trigger a js function that will update your accordion:
<?php echo $ajax->submit(
'Submit', array(
'url' => array(
'controller'=>'qnotes',
'action'=>'add',
'complete' => 'updateAccordian(request.responseText)')
));
the reuqest.responseText will contain the reponse from your controller/action.
HTH

CakePHP ajax form return display continuously current page w/ missing controller

I am running an ajax submit in cakephp. It appears to be running, but the end result for the population of the assigned div is the page itself as opposed to the results set. And it also says the controller is missing which obviously is not true.
Model: Plan
Action: search()
search form element (being pulled into the search.ctp):
...
echo $ajax->form(array('type' => 'post',
'options' => array(
'update' => 'plansQueryResults',
'url' => array('controller' => 'plan', 'action' => 'search'),
'loading' => "Element.show('plsLoaderID')",
'loaded' => "Element.hide('plsLoaderID')"
)
));
...
echo $form->end();
This div is pulling in the default layout, but
<div id="plansQueryResults"></div>
Standard error (wrapped around default layout) displaying within the div:
Missing Controller
Error: PlanController could not be found.
Error: Create the class PlanController below in file: dental/1/app/controllers/plan_controller.php
<?php
class PlanController extends AppController {
var $name = 'Plan';
}
?>
What is the code you have on the search action?
What is the returned data from the action? Check with Firebug.
By the way... when following the standards to create a DB, it is better and handy use cake bake: Code Generation with Bake

Resources