Changes
Title
unchanged
Ordering Models by Weight with CJuiSortable
Category
unchanged
Tutorials
Yii version
unchanged
Tags
unchanged
Content
changed
In this tutorial we will be setting up a content type to be ordered by weight using the JUI Sortable plugin.
*Note: This is
probably not suitable for large data
sets, as each model is updated on submission.*
**Tutorial Requirements**[...]
// Set the number of models to be the heaviest weight
// and format into a dropDownList-friendly array
$models = BlogYourClass::model()->findAll();
for ($i = 0; $i < sizeof($models); $i++) { $weights[$i] = $i; }[...]
'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[...]