PHP code example of oppara / cakephp-plugin-sortable

1. Go to this page and download the library: Download oppara/cakephp-plugin-sortable library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

oppara / cakephp-plugin-sortable example snippets


// config/bootstrap.php

Plugin::load('Sortable');

// config/bootstrap.php

Plugin::loadAll();

// 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/Template/Articles/index.ctp

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

<div class="row">
  <div class="col-md-12">
    <table cellpadding="0" cellspacing="0" class="table table-hover table-bordered sortable">
        <thead>
            <tr>
                <th class="col-md-8" scope="col">title</th>
                <th class="col-md-4" scope="col" class="actions">Actions</th>
            </tr>
        </thead>
        <tbody>
             foreach ($articles as $article):