Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How can I sort by name? #36

Open
ricardocanelas opened this issue May 30, 2014 · 3 comments
Open

How can I sort by name? #36

ricardocanelas opened this issue May 30, 2014 · 3 comments

Comments

@ricardocanelas
Copy link

My category table
captura de tela 2014-05-30 as 16 29 42

The listing is as follows:
captura de tela 2014-05-30 as 16 29 49

But I wish to be like this:
Sort by name:

Root

  • Esporte
    • Baskball
    • Futebol
    • Natação
  • Viagem
    • Brasil
    • México
    • USA

How can I do?

@cebe
Copy link
Member

cebe commented May 30, 2014

you would have to recalculate rgt and lft for this. Load the tree from the DB, sort the records using PHP and save them back.

@ricardocanelas
Copy link
Author

how i have to recalculate rgt and lft for this? ..or I can create a new column 'parent_id'?

@ricardocanelas
Copy link
Author

Please check if I did right:

public function orderByName(){
    $parent=Category::model()->findByPk(1);
    $descendants=$parent->children()->findAll(array("order" => "name DESC"));
    self::childrenOrderByName($descendants,$parent);
}

private function childrenOrderByName($descendants, $parent){
        foreach ($descendants as $key => $category) {
            $category->moveAsFirst($parent);
            $_parent = Category::model()->findByPk($category->id);
            $_descendants=$_parent->children()->findAll(array("order" => "name DESC"));

            if(count($_descendants) > 0){
                self::childrenOrderByName($_descendants,$_parent);
            }
        }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants