Control ordering of labels in multi value column

Control ordering of labels in multi value column

kthorngrenkthorngren Posts: 21,305Questions: 26Answers: 4,947

I have a multi value column that I would like to not have sorted within the cell. For example the server returns the data in the following order C, F, A, D. The display is A, C, D, F. Is there an option to turn off the data ordering? Or maybe I need to render the data differently.

Below is the field I don't want the values to be sorted.

Kevin

            { data: 'c.commands',
                render: '[, ].label',
                editField: 'main.fk_commands[]',
        },

Init code:

$(document).ready(function() {
    editor = new $.fn.dataTable.Editor( {
        ajax:  '/manage_ios_commands',
        table: '#edit-table',
        fields: [
            { type: 'hidden', label: 'pkid:', name: 'main.pkid' },
            { label: 'Name:', name: 'main.name' },
            { type: 'select', label: 'Type:',  name: 'main.fk_types', placeholder: 'Select a Type' },
            { type: 'select2', label: 'Commands:',  name: 'main.fk_commands[]', 
                    attr: { multiple: true },
                    opts: { allowClear: true,
                            placeholder: 'Add IOS commands here',
                            tags: true,
                            tokenSeparators: [',']  }
            },
        ]
    } );

    $('#edit-table').on( 'click', 'tbody td:not(:first-child)', function (e) {
        editor.inline( this, {
            onBlur: 'submit'
        } );
    } );

    var table = $('#edit-table').DataTable( {
        ajax: '/manage_ios_commands',
        columns: [
            { data: 'main.pkid' },
            {
                data: null,
                defaultContent: '',
                className: 'select-checkbox',
                orderable: false
            },
            { data: 'main.name' },
            { data: 'type.type_name', editField: 'main.fk_types' },
            { data: 'c.commands',
                render: '[, ].label',
                editField: 'main.fk_commands[]',
        },
        ],

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 63,471Questions: 1Answers: 10,467 Site admin
    Answer ✓

    DataTables on the client-side won't be ordering the data - the rendering function, when used like that, will just loop over the c.commands array and pluck out the label property sequentially.

    That makes me suspect that the server-side is doing the ordering. Checking the JSON data would confirm that.

    What is the code you are using on the server-side?

    Regards,
    Allan

  • kthorngrenkthorngren Posts: 21,305Questions: 26Answers: 4,947

    I keep forgetting that when doing a SQL query with where pkid in (1,2,3,4) MySql will return the data in any order. In this case the order is sorted by the value returned. Changed my code to build the array in the order of the list not the SQL return.

    Was hoping to post this before you looked at it :-)

    Kevin

  • kthorngrenkthorngren Posts: 21,305Questions: 26Answers: 4,947
    edited December 2016

    There is an interesting side affect. When selecting the cell the items are reordered based on the order of the options. Then they are put back into the original order when exiting that cell. Not a problem just noting it.

    EDIT: This is only noticable when using select2.
    EDIT2: With select2 the order sent to the server is changed to the order displayed in when editing the field. Will take this into consideration when updating the record.

    Kevin

  • allanallan Posts: 63,471Questions: 1Answers: 10,467 Site admin

    Thanks for the updates - good to hear you've got it sorted now :smile:.

    Allan

This discussion has been closed.