Monday, February 6, 2012

Delete and AJAX pagination with Filter

Hello!

I have AJAX pagination working correctly (with form filters!) with one
small problem. When I delete an entry from my pages, the new URLs for
the pagination controls (i.e. numbers and previous/next) all have
delete as part of the named parameters. I'm not sure what I am doing
wrong.

Some relevant functions from the controller:

public function delete ($id)
{
if (!$id) {
$this->Session->setFlash (__('Invalid id for
observation.'), 'flash_error');
}
if ($this->Observation->delete ($id)) {
$this->Session->setFlash (__('Observation was deleted.'),
'flash_success');
} else {
$this->Session->setFlash (__('Observation was not
deleted.'), 'flash_error');
}
$this->olog ();
}

// This should probably be in the model, but not sure what to put in
there to make it work with paginate.

public function olog ()
{
if ($this->data == null) {

// Default everything unless the user passed in something in the form!

$conditions = array ();
$start_time = $this->Observation->find ('first', array
('fields' => array ('MIN(time)')));
$start_time = $start_time [0]['MIN(time)'];
$end_time = $this->Observation->find ('first', array
('fields' => array ('MAX(time)')));
$end_time = $end_time [0]['MAX(time)'];
$type = 'all';
$text = '';

if ($this->params ['named'] != null) {

// User is paging in this case! There may or may not be information
in the form.

$conditions = array ();

if (isset ($this->params ['named']['type']) && ($this-
>params ['named']['type'] != 'all')) {
$type = $this->params ['named']['type'];
$conditions [] = array ('type' => $this->params
['named']['type']);
}
if (isset ($this->params ['named']['start_time']) &&
($this->params ['named']['start_time'] != "")) {
$start_time = $this->params ['named']
['start_time'];
$conditions [] = array ('time >=' => $this->params
['named']['start_time']);
}
if (isset ($this->params ['named']['end_time']) &&
($this->params ['named']['end_time'] != "")) {
$end_time = $this->params ['named']['end_time'];
$conditions [] = array ('time <=' => $this->params
['named']['end_time']);
}
if ((isset ($this->params ['named']['text']) && $this-
>params ['named']['text'] != "")) {
$text = $this->params ['named']['text'];
$conditions [] = array ("OR" => array ('object
LIKE ' => '%'.$this->params ['named']['text'].'%',

'description LIKE ' => '%'.$this->params ['named']['text'].'%'));
}
}
} else {

// Data is passed in from the form!

$conditions = array ();
if ($this->data ['Observation']['type'] != 'all') {
$conditions [] = array ('type' => $this->data
['Observation']['type']);
}
if ($this->data ['Observation']['start_time'] != "") {
$conditions [] = array ('time >=' => $this->data
['Observation']['start_time']);
}
if ($this->data ['Observation']['end_time'] != "") {
$conditions [] = array ('time <=' => $this->data
['Observation']['end_time']);
}
if ($this->data ['Observation']['text'] != "") {
$conditions [] = array ("OR" => array ('object LIKE '
=> '%'.$this->data ['Observation']['text'].'%',
'description
LIKE ' => '%'.$this->data ['Observation']['text'].'%'));
}

$text = $this->data ['Observation']['text'];
$type = $this->data ['Observation']['type'];
$start_time = $this->data ['Observation']['start_time'];
$end_time = $this->data ['Observation']['end_time'];
}

$observations = $this->paginate ('Observation', $conditions);

$this->set (compact ('observations', 'start_time', 'end_time',
'text', 'type'));
$this->set ('subtab', 'log');
}

Some relevant stuff from my view:

<?php echo $this->Js->link (__('Delete'),
array ('action' => 'delete', $observation
['Observation']['id']), // '?' => $this->request->params ['named']),
array ('update' => '#observations_table',
'confirm' => sprintf (__('Are you sure
you want to delete the observation on %s?'),
$observation
['Observation']['time'])
));
?>

<div class="pagination">
<ul>
<?php

// We need either the stuff from the form or the stuff from the
parameters passed in if the user is paging
// If the user is doing nothing (first time hitting the page), an
empty array is fine!

if ($this->params ['data'] != null) {
$url = $this->params ['data']['Observation'];
} else if ($this->params ['named'] != null) {
$url = $this->params ['named'];
} else {
$url = array ();
}

?>

<?php echo $this->Paginator->prev ('<< ' . __('Prev'),
array (), null, array ('class'=>'disabled current'));?>
<?php echo $this->Paginator->numbers (array ('separator'
=> '', 'url' => $url));?>
<?php echo $this->Paginator->next (__('Next') . ' >>',
array (), null, array ('class' => 'disabled current'));?>
</ul>
</div>

I think that's enough code to show everything I'm doing with regard to
delete and pagination. Anyway, thanks for any help!

Joey

--
Our newest site for the community: CakePHP Video Tutorials http://tv.cakephp.org
Check out the new CakePHP Questions site http://ask.cakephp.org and help others with their CakePHP related questions.


To unsubscribe from this group, send email to
cake-php+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php

No comments: