PHP code example of hacheraw / sortable

1. Go to this page and download the library: Download hacheraw/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/ */

    

hacheraw / sortable example snippets


/**
 * @mixin \Sortable\Model\Behavior\SortableBehavior
 */
class ElementsTable extends Table
{
    /**
     * @param array $config
     * @return void
     */
    public function initialize(array $config): void
    {
        parent::initialize($config);

        // Add the behavior to your table
        $this->addBehavior('Sortable.Sortable', [
            'field' => 'name_of_the_field', // default name is `position`
            'group' => [], // If there are associations you must set the foreign keys. eg: ['post_id']
            'start' => 1, // First item of the list (where to start counting from)
            'steps' => 1 // How much to add or substract
            ]
        );
    }

// Controller::action()

    // Move the row to a new position
    $this->Elements->move($row_id, $new_place);

    // Move the row to the top
    $this->Elements->toTop($row_id);

    // Move the row to the bottom
    $this->Elements->toBottom($row_id);

    // Controller::action()

    /**
     * If there are fields that must be column named
     * post_id, then you must add it as a condition.
     */
    $conditions = ['post_id' => 7];

    // Passing variables to the View
    $this->set('min', $this->Proceedings->ProceedingColumns->getStart());
    $this->set('max', $this->Proceedings->ProceedingColumns->getLast($conditions)); // When editing order of existing row
    $this->set('max', $this->Proceedings->ProceedingColumns->getNew($conditions)); // When adding a new row
    $this->set('step', $this->Proceedings->ProceedingColumns->getStep());

    // View

    echo $this->Form->control('position', ['min' => $min, 'max' => $max, 'step' => $step, ]); // When editing order of existing row
    echo $this->Form->control('position', ['min' => $min, 'max' => $max, 'step' => $step, 'value' => $max]); // When adding a new row