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?