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’:

_categories.php


<?php 

$items = array(); 

foreach($categories as $category) {

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

}

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

    'id'=>'items',

    'items'=>$items,

    // additional javascript options for the accordion plugin

    'options'=>array(

                'delay'=>100,

		'update'=>"js:function(){

			$.ajax({

				type: 'POST',

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

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

			});

		}",

	),				

));

?>

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

StoreController.php


	public function actionSortCategories()

	{

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

    	    Store::model()->updateCategoryOrder($_POST['id']);

	}

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

Store.php


	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?

help?

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:


            $.ajax({

                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:


	'update'=>"js:function(){

		$.ajax({

			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;

		    $item->save();

		    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

	}