Select2 - multiple select - options order

Select2 - multiple select - options order

vladimirijusvladimirijus Posts: 16Questions: 4Answers: 1

How to record select2 options into db ordered by user choice, not by id?

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 61,665Questions: 1Answers: 10,096 Site admin

    How are you getting the values? Select2 is probably just showing them in the order they are added, so you need to get the data from the server in the required order. If you are using the PHP or .NET libraries the Field->options() method will use the label value automatically. If you want to use some other order you would use a custom method.

    Allan

  • vladimirijusvladimirijus Posts: 16Questions: 4Answers: 1
    edited February 2016

    Hi Allan,
    Thank you for your help. You can find example here: http://backend.front.rs/list.php

    I'm getting values using Mjoin:

     ->join(
            Mjoin::inst( 'regions' )
                ->link( 'top_banners.id', 'top_banners_regions.top_banners_id' )
                ->link( 'regions.id', 'top_banners_regions.regions_id' )
                ->fields(
                    Field::inst( 'id' )
                        ->options( 'regions', 'id', 'name' ),
                    Field::inst( 'name' )                
                )  
        )
    

    and also, I use this to stop Select2 making alphabetical reorder on option select:

        $( 'select', editor.node( 'regions[].id' )).on( 'select2:select', function (evt) {
            var element = evt.params.data.element;
            var $element = $(element);
    
            $element.detach();
            $(this).append($element);
            $(this).trigger("change");
        } );
    

    What I need is an option to record values one-to-many in database in order user entered (for example region 5, region 3, region 6...)

    Vladimir

  • allanallan Posts: 61,665Questions: 1Answers: 10,096 Site admin

    I'm not sure that there is a way to retain the original order with the Editor PHP API. The Mjoin doesn't offer an order by option at this time.

    If it were to, then to retain the original ordering what I think you would need to do is add an auto incrementing sequence to the database. So as each new item is added - and Editor will add it in the order submitted - it gets a sequential identifier. Then when you read them back from the database the order by clause would be set to match.

    I'll take a look at adding that for the next version of Editor as I can see that might be quite useful.

    Allan

  • vladimirijusvladimirijus Posts: 16Questions: 4Answers: 1

    Table top_banners_regions which holds particular options, already has auto incrementing sequence (id). Should I add something to the Mjoin?

    It would be great if you add this feature to Editor, because data ordering is quite important to users.

  • allanallan Posts: 61,665Questions: 1Answers: 10,096 Site admin
    Answer ✓

    Just to confirm - this will be in the next version of Editor. Mjoin will have an order() method which you can use to specify which property of the joined table should be used to order the data.

    Regards,
    Allan

  • allanallan Posts: 61,665Questions: 1Answers: 10,096 Site admin

    I've sent you an e-mail with the update :-)

  • vladimirijusvladimirijus Posts: 16Questions: 4Answers: 1

    Great, thank you!

  • chipaldancechipaldance Posts: 12Questions: 2Answers: 0

    Hi,

    I search this method too !
    Can you send me this update, Allan ?

    Thanks a lot !

    Gaétan

  • allanallan Posts: 61,665Questions: 1Answers: 10,096 Site admin

    Editor 1.5.6 will be out next week if you can hang on a little bit longer. There are a couple of other related changes that I'd like to make first.

    Allan

  • chipaldancechipaldance Posts: 12Questions: 2Answers: 0

    Yes, I can.

    thanks Allan !

This discussion has been closed.