PHPackages                             oppara/cakephp-plugin-sortable - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. oppara/cakephp-plugin-sortable

ActiveCakephp-plugin

oppara/cakephp-plugin-sortable
==============================

CakePHP plugin that mainly for jQuery UI Sortable

0.4.0(3mo ago)1421↓100%1[1 PRs](https://github.com/oppara/cakephp-plugin-sortable/pulls)MITPHPPHP ^7.2

Since Dec 17Pushed 3mo ago2 watchersCompare

[ Source](https://github.com/oppara/cakephp-plugin-sortable)[ Packagist](https://packagist.org/packages/oppara/cakephp-plugin-sortable)[ Docs](https://github.com/oppara/cakephp-plugin-sortable)[ RSS](/packages/oppara-cakephp-plugin-sortable/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependencies (3)Versions (6)Used By (0)

CakePHP plugin that mainly for [jQuery UI Sortable](https://jqueryui.com/sortable/)
===================================================================================

[](#cakephp-plugin-that-mainly-for-jquery-ui-sortable)

[![Build Status](https://camo.githubusercontent.com/40819303e1333b3a6bd5d08dab8089bcd0af32c6773720698eb00ce9d82b0a91/68747470733a2f2f7472617669732d63692e6f72672f6f70706172612f63616b657068702d706c7567696e2d736f727461626c652e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/oppara/cakephp-plugin-sortable)[![codecov](https://camo.githubusercontent.com/c3d31048e8f02b668077006fef794bbc7df5f490ccb3668f9946114fcadad5a4/68747470733a2f2f636f6465636f762e696f2f67682f6f70706172612f63616b657068702d706c7567696e2d736f727461626c652f6272616e63682f6d61737465722f67726170682f62616467652e737667)](https://codecov.io/gh/oppara/cakephp-plugin-sortable)

Requirements
------------

[](#requirements)

- CakePHP 4.0 or higher
- PHP 7.2 or higher

Installation
------------

[](#installation)

```
composer require oppara/cakephp-plugin-sortable

```

Enable plugin
-------------

[](#enable-plugin)

Add the plugin to your application's bootstrap:

```
// src/Application.php
public function bootstrap(): void
{
    parent::bootstrap();
    $this->addPlugin('Sortable');
}
```

Examples
--------

[](#examples)

```
CREATE TABLE articles (
    id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
    user_id INT UNSIGNED NOT NULL,
    display_order SMALLINT UNSIGNED NOT NULL DEFAULT 0,
    title VARCHAR(255) NOT NULL,
    body TEXT,
    created DATETIME,
    modified DATETIME
);
```

```
// src/Model/Table/ArticlesTable.php
namespace App\Model\Table;

use Cake\ORM\Table;

class ArticlesTable extends Tabl
{
    public function initialize(array $config)
    {
        $this->addBehavior('Sortable.Sortable', [
            'condition_fields' => ['user_id']
        ]);
    }
```

```
//  src/Controller/ArticlesController.php
class ArticlesController extends AppController
{

    public function sort()
    {
        if (!$this->request->is('ajax')) {
            exit;
        }

        $data = json_encode($this->request->getData());
        $this->log(__METHOD__ . ' data:' . $data, LOG_DEBUG);

        $id = $this->request->getData('id');
        $new_order = $this->request->getData('display_order');
        $this->Sections->sort($id, $new_order);

        return $this->response->withType('application/json')
            ->withStringBody(json_encode(['status' => 'OK']));
    }
```

```
//  src/templates/Articles/index.php

echo $this->Html->css(['sort'], ['block' => true]);
echo $this->Html->script(['jquery-ui.min', 'sort'], ['block' => true]);
?>

                title
                Actions
