CJuiSortable - trouble with 'update' in options array

I’m trying to update my category’s ‘position’ in my database tables when a new sort takes place via ajax.

This post got me most of the way I think but I’m still having some trouble.

I have a partial that gets rendered when viewing a ‘Store’:



$items = array(); 

foreach($categories as $category) {

	$items['id_'.$category->id] = $category->title;


$this->widget('zii.widgets.jui.CJuiSortable', array(



    // additional javascript options for the accordion plugin





				type: 'POST',

               			url: '{$this->createUrl('store/sortCategories')}',

                		data: $(this).sortable('serialize'),






then I have this action in my controller for ‘Store’:


	public function actionSortCategories()


	    echo "<script>alert('yo');</script>";



finally I have this method in my model for ‘Store’:


	public function updateCategoryOrder()


		foreach ($_GET['id'] as $position => $item)


			$category = Category::model()->findByPk($item);

			$category->position = $position;

			return $category->save();



I think everything looks right, but I’m very new to Yii - I can sort my items but the ‘update’ ajax function never seems to get invoked and so the ‘sortCategories’ action never gets invoked in my controller (my ‘yo’ alert is never triggered).

Any ideas?


In my opinion… even if the ajax is made… the alert() would not be fired as you are just echoing it…

To be sure if the ajax works… you would need to check with firefox / firebug (or something similar - chromebug,etc)…

Ajax data has to be returned thats why your ( ‘yo’ alert is never triggered);

And ajax only recognize data, your js functions on other page will never be triggered by ajax;

great - thanks … all set now

Hmm… Strange problem… It should work, anyway I’m using this code and it’s fine:


                type: 'POST',

                url: '{$this->createUrl('category/saveorder')}',

                data: 'th=' + $(this).sortable('toArray').toString()


hi, how do you solved the problem? I’m getting this error 400 CHttpException

The way I sorted it out using console.log in firebug (recommended!) to figure what was going on:



			type: 'POST',

			url: '{$this->createUrl('sortitems')}',

			data: {'items[]':$(this).sortable('toArray')},



and then in the controller:

	public function actionSortItems()


	    if (isset($_POST['items']) && is_array($_POST['items'])) {

		foreach($_POST['items'] as $index=>$id){

		    $item = Item::model()->findByPk($id);

		    $item->order = $index;


		    echo "<p>$index : $id</p>";//this isn't needed, but useful for checking that the data was received ok



	    exit;//exit here so the response is kept short, you could always return a confirm message