It is easy to create an AJAX sortable list with CakePHP 2.0, using jQuery that automatically updates the database, however it is hard to find documentation explaining how to do it.
Here is my explanation – it assumes a basic knowledge of CakePHP.
Step 1: Data
Lets use a simple categories table which includes a column for sort_order
. This will be used to define the order the records are displayed on the page.
CREATE TABLE `categories` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(60) DEFAULT NULL,
`sort_order` int(10) unsigned DEFAULT NULL,
PRIMARY KEY (`id`)
);
Step 2: CakePHP
Bake a model, controller and views for this table so that we have some files to work with.
You should be able to go onto your website at http://yoururl/categories and see the default index view.
Layout
In order to use the sortable feature, you will need to edit the default layout. If you don’t already have your own layout you can copy the CakePHP default one (which is used if your own one isn’t found) from /lib/Cake/View/Layouts/default.ctp
and place it in app/View/Layouts/default.ctp
.
Edit this so that the writeBuffer method is one of the final lines, just before the close body tag:
Js->writeBuffer();?>