Giter Club home page Giter Club logo

oc-sortablerelations-plugin's Introduction

Sortable relations plugin for October

Adds drag and drop sorting functionality to the view list of the relations controller in October.

Usage

The following example illustrates how to use this plugin. It shows a many-to-many relationship between a category and it's related products.

Add a relation_sort_order field to the pivot database table.

class CreateCategoryProductTable extends Migration
{
    public function up()
    {
        Schema::create('acme_plugin_category_product', function (Blueprint $table) {
            $table->engine = 'InnoDB';
            $table->integer('category_id')->unsigned();
            $table->integer('product_id')->unsigned();
            $table->integer('relation_sort_order')->unsigned();
            $table->primary(['category_id', 'product_id'], 'category_product_primary');
        });
    }

    ...
}

Use the DamianLewis\SortableRelations\Traits\SortableRelations trait within the parent model of the relation. Add the sortable relation to the $sortableRelations array and add the 'relation_sort_order' attribute to the pivot data array.

class Category extends Model
{
    use DamianLewis\SortableRelations\Traits\SortableRelations;

    public $table = 'acme_plugin_categories';

    public $belongsToMany = [
        'products' => [
            'Acme\Plugin\Models\Product',
            'table' => 'acme_plugin_category_product',
            'pivot' => ['relation_sort_order']
        ]
    ];

    public $sortableRelations = [
        'products'
    ];
}

Implement the DamianLewis\SortableRelations\Behaviors\SortableRelations behavior within the parent controller of the relation and define the $sortableRelationConfig configuration file. This file should define the model class and the name of the sortable relation.

class Categories extends Controller
{
    public $implement = [
        'Backend.Behaviors.FormController',
        'Backend.Behaviors.ListController',
        'Backend.Behaviors.RelationController',
        'DamianLewis.SortableRelations.Behaviors.SortableRelations'
    ];

    public $formConfig = 'config_form.yaml';
    public $listConfig = 'config_list.yaml';
    public $relationConfig = 'config_relation.yaml';
    public $sortableRelationConfig = 'config_sortable_relation.yaml';

    ...
}

Example sortable relations configuration file config_sortable_relation.yaml:

modelClass: Acme\Plugin\Models\Category
relationName: products

Make sure the pivot[sort_order] column has been included in the view.list configuration for the relation controller.

Example relations configuration file config_relation.yaml:

products:
    label: Product
    view:
        list:
            columns:
                title:
                    label: Title
                    type: partial
                pivot[relation_sort_order]:
                    label: Order
                    type: number
                    invisible: true
    manage:
        list: $/acme/plugin/models/product/columns.yaml
        form: $/acme/plugin/models/product/fields.yaml

Lastly, two hidden input fields need to be added to the table rows. One for the parent model ID and the other for the related model ID. This can be accomplished by using a column partial type. The input field for the parent model should include a Lists-relationViewList-parent-id id attribute with a value equal to the parent model ID. The input field for the related model should include a Lists-relationViewList-related-id id attribute with a value equal to the related model ID.

Example partial file for the title column:

<input type="hidden" id="<?= 'Lists-relationViewList-parent-id-'.$record->pivot->category_id ?>" value="<?= $record->pivot->category_id ?>">
<input type="hidden" id="<?= 'Lists-relationViewList-related-id-'.$record->pivot->product_id ?>" value="<?= $record->pivot->product_id ?>">
<?= $value ?>

Note

If extending the relation configuration for the parent controller, make sure to include a call to the 'SortableRelations' relationExtendConfig method as shown in the following example.

class Categories extends Controller
{
    ...
    
    public function relationExtendConfig($config, $field, $model)
    {
        $this->asExtension('SortableRelations')->relationExtendConfig($config, $field, $model);
    }
    
    ...
}

oc-sortablerelations-plugin's People

Contributors

damianlewis avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

oc-sortablerelations-plugin's Issues

Column 'id' in where clause is ambiguous

First of all thank you for such a great plugin.

I'm using the plugin with the setup in readme. Having such an error when drag and drop the columns. Seems that in the query id should have the parent table indicator. But I couldn't find the place to correct it.

SQL: select 'imt_gammaoperationengine_vehicles'.*, 'imt_gammaauctionengine_auction_vehicle'.'auction_id' as 'pivot_auction_id', 'imt_gammaauctionengine_auction_vehicle'.'vehicle_id' as 'pivot_vehicle_id', 'imt_gammaauctionengine_auction_vehicle'.'relation_sort_order' as 'pivot_relation_sort_order' from 'imt_gammaoperationengine_vehicles' inner join 'imt_gammaauctionengine_auction_vehicle' on 'imt_gammaoperationengine_vehicles'.'id' = 'imt_gammaauctionengine_auction_vehicle'.'vehicle_id' where 'imt_gammaauctionengine_auction_vehicle'.'auction_id' = 3 and is_expert_reported = 1 and is_consultant_approved = 1 and 'id' = 5 and 'imt_gammaoperationengine_vehicles'.'deleted_at' is null limit 1)" on line 664 of ...

getSimpleValue()

"Call to undefined method October\Rain\Database\QueryBuilder::getSimpleValue()" on line 2483 of /Applications/MAMP/htdocs/category_post/vendor/laravel/framework/src/Illuminate/Database/Query/Builder.php

help!!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.