Update: For what looks to be a better implementation see: http://www.yiiframework.com/extension/sortable-model/
Just a quick component I built for dealing with manual sorting of database records. For example, say you have the following table.
User
id name position
1 me 1
2 you 3
3 someone 2
Now, when you decide to display these results in your view, you might do something like:
$criteria = new CDbCriteria();
$criteria->order = 'position ASC';
$users = User::model()->findAll($criteria);
foreach($users as $user)
{
echo $user->name;
}
This would display like:
me
someone
you
Simple enough.
What if you want to build an interface to re-arrange the users? You could have text fields to edit the positions, or better yet you could have up/down arrows (or even drag/drop functionality).
For the up/down arrows, you would need to write code that determines what position to assign a user when you click an up arrow or a down arrow to re-position a user. This may seem simple, but it can be complicated or at least cumbersome. Also you need code for adding new users and deleting users. This is where this extension comes in.
This component attempts to do all that for you but still gives you lots of control.
Install ¶
Download from: https://github.com/nsanden/Position-Sorting
Move the file to your components folder.
Add to your config/main.php
'components'=>array(
'positionSorting' => array(
'class' => 'PositionSorting',
//'field = 'position', //default - override this in the controller if need be
//'primary_key' = 'id' //default - override this in the controller if need be
),
)
Usage ¶
In your view, create a link for 'up' and a link for 'down' next to each record. This can be done in a cgridview just as easily as the simple foreach example above.
[html]
<a href="?r=controller/changePosition&id=1&move=up" data-move="up" class="position">Up</a>
<a href="?r=controller/changePosition&id=1&move=down" data-move="down" class="position">Down</a>
You'll probably want them to be ajax links that trigger the following action in your controller.
public function actionChangePosition($id, $move) {
$sorting = Yii::app()->positionSorting;
/* This tells the component what model we're working with - required */
$sorting->model = 'User';
/* Optionally, you can tell it to only deal with models
that match your additional condition */
//$sorting->condition = 'account_id = 2';
/* Optionally, you can specify the position field. */
//$sorting->field = 'position', //default
/* Optionally, you can specify the primary key field. */
//$sorting->primary_key' = 'id' //default - override this in the controller if need be
/* Tell it to re-position this record up or down from its current position */
$sorting->move($id, $move);
}
You may also want to write some jquery to make the client side table row move when you click the up or down link.
[javascript]
$(".position").click(function() {
...
var row = $(this).parents("tr:first");
if ($(this).attr("data-move") == 'up') {
row.insertBefore(row.prev());
} else {
row.insertAfter(row.next());
}
});
If you have any questions, please ask in the forum instead.
Signup or Login in order to comment.