Ordering Models by Weight with CJuiSortable

Comparing #3 with #4

Revision #4 was created by Phil Loaiza Phil Loaiza on Apr 24, 2013, 4:37:00 PM.

Element in DOM to be selected to retrieve sorted list updated from "ul#orderList" to "ul.ui-sortable". Changed the code that saves "weight" to updateByPK() rather than save().

Content

[...]
// Add a Submit button to send data to the controller
echo CHtml::ajaxButton('Submit Changes', '', array(
'type' => 'POST',
'data' => array(
// Turn the Javascript array into a PHP-friendly string
'Order' => 'js:$("ul
#orderList.ui-sortable").sortable("toArray").toString()',
)
));
[...]
if ($model = YourClass::model()->findbyPk($models[$i]))
{
$model->weight = $i;
 
 
                $model->save(
// Use updateByPK to avoid running model validate
 
                $model->updateByPk( $models[$i],array("weight"=>$i) 
);
}
}
}
// Handle the regular model order view
else
[...]