position-sorting-component Handles the logic required to move records up or down in terms of their position to other records in a database table.

  1. Install
  2. Usage

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.

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:


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.


Download from: https://github.com/nsanden/Position-Sorting

Move the file to your components folder.

Add to your config/main.php

     '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


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.

<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.

        $(".position").click(function() {
            var row = $(this).parents("tr:first");
            if ($(this).attr("data-move") == 'up') {
            } else {
2 0
Yii Version: Unknown
License: BSD-2-Clause
Category: Database
Developed by: nsanden
Created on: Dec 10, 2013
Last updated: 6 years ago


show all