Changes
Title
unchanged
Ordering Models by Weight with CJuiSortable
Category
unchanged
Tutorials
Yii version
unchanged
Tags
unchanged
Content
changed
[...]
// 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()',
)
));[...]
{
// Handle the POST request data submission
if (Yii::app()->request->isPostRequest && isset($_POST['Order']))
{
// Since we converted the Javascript array to a string,[...]
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[...]