If you simply need to update the position of the items in a list using
ajax and dragging/dropping them this is a solution
(based on this thread
http://groups.google.com/group/cake-php/browse_thread/thread/e921d173b7c41519/becb28c858e4d683?lnk=gst&q=sortable&rnum=1#becb28c858e4d683)
--------------------------------------------------------------------------------------------------------------
Controller:
--------------------------------------------------------------------------------------------------------------
<?php
class TestsController extends AppController
{
var $helpers = array('html', 'Form', 'Javascript', 'Ajax',
'time');
var $components = array();
//setting $uses to empty to avoid to use the relative model
function index()
{
pr($this->params['pass']);
}
function sort()
{
$options = $this->Test->findAll(null, null, 'position ASC');
$this->set('options', $options);
}
function order()
{
$ids= $this->params['form']['sort'];
$i = 1;
foreach($ids as $id){
$this->Test->id = $id;
$this->Test->saveField('position', $i++);
}
$this->autoRender = false;
//$this->layout = 'ajax';
}
}
--------------------------------------------------------------------------------------------------------------
MODEL
--------------------------------------------------------------------------------------------------------------
<?php
class Test extends AppModel {
var $name = 'Test';
}
?>
--------------------------------------------------------------------------------------------------------------
VIEW: sort.ctp
--------------------------------------------------------------------------------------------------------------
<ul id="sort" style="list-style-type:none;">
<?php
foreach ($options as $option){?>
<li id="items_<?= $option['Test']['id']?>"><b><?=$option['Test']
['name'];?></b> - position: <?= $option['Test']['position']?></li>
<?php }
?>
</ul><?php echo $ajax->sortable('sort', array('onUpdate' => "
function(t){new
Ajax.Request( 'order', {method:'post',
postBody:Sortable.serialize('sort'), asynchronous:true})}"))?>
--------------------------------------------------------------------------------------------------------------
I then have an empty view for order.ctp
--------------------------------------------------------------------------------------------------------------
My DB table looks as follows:
--
-- Table structure for table `tests`
--
CREATE TABLE `tests` (
`id` int(11) NOT NULL auto_increment,
`name` varchar(50) NOT NULL,
`position` int(11) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1;
--------------------------------------------------------------------------------------------------------------
In the HEADER of your layout remember to include
<?php echo $javascript->link('prototype')?>
<?php echo $javascript->link('scriptaculous')?>
<?php echo $javascript->link('zebra_tables')?>
<?php echo $javascript->link('effects')?>
--------------------------------------------------------------------------------------------------------------
Hope this helps!
Dan
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Cake
PHP" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/cake-php?hl=en
-~----------~----~----~----~------~----~------~--~---