Reference to buttons.name in config

Reference to buttons.name in config

BjornHaBjornHa Posts: 66Questions: 12Answers: 0

Is it possibe to use the buttons.name setting to activate a set of buttons in a layout definition? (like 'buttons:main' or similar)
I would like to define multiple sets of buttons in the default settings and be able to refer to them in the config locally, but maybe this needs to be done through API calls after config?
KR, Bjørn H

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 65,328Questions: 1Answers: 10,836 Site admin

    Is it possibe to use the buttons.name setting to activate a set of buttons in a layout definition?

    Do you mean using something to indicate which buttons should be displayed in the layout? If you want a different set of buttons depending on some external variable, you would need to include the logic for that in the initialisation.

    Allan

  • BjornHaBjornHa Posts: 66Questions: 12Answers: 0

    I meant to have something like

                buttons: {
                    name: 'standard',
                    buttons: [
                        { 
                            extend: 'csv', 
                            className: 'btn-secondary'
                        },
                        {
                            extend: 'excel',
                            className: 'btn-secondary'
                        },
                        {
                            extend: 'copy',
                            className: 'btn-secondary'
                        } */
                    ]
                },
                buttons: {
                    name: 'modify',
                    buttons: [
                        { 
                            extend: 'create', 
                            className: 'btn-success'
                        },
                        {
                            extend: 'edit',
                            className: 'btn-success'
                        },
                        {
                            extend: 'remove',
                            className: 'btn-danger'
                        }
                    ]
                },
    

    in $.fn.dataTable.defaults
    and then be able to refer them like

                layout: {
                    top: [
                        'buttons:standard',
                        'paging',
                        { search: { placeholder: 'Search' } }
                    ],
                    bottomStart: 'buttons:modify'
                },
    

    or

                layout: {
                    top: [
                        'buttons:standard', 'buttons:modify'
                        'paging',
                        { search: { placeholder: 'Search' } }
                    ]
                },
    

    or something similar..

    /bjørn

  • allanallan Posts: 65,328Questions: 1Answers: 10,836 Site admin
    Answer ✓
    buttons: {
      ...
    },
    buttons: {
      ...
    }
    

    isn't possible. It would be like let i = 1; i = 2; and expecting i to be 1 and 2 :). Typically the second declaration will be the one that the Javascript engine will use.

    You would need to do something like:

    const buttonsDefaultExport = { buttons: [ ... ] }
    const buttonsDefaultEdit = { buttons: [ ... ] }
    

    Then:

    layout: {
      top: editButtons ? buttonsDefaultEdit : buttonsDefaultExport
    }
    

    Better would be to have a function that would accept an array of options:

    layout {
      top: buttons( 'export', 'edit' )
    }
    

    which I think is more inline with what you are looking for. I'm afraid that isn't something that is available in Buttons at the moment, but I don't see any reason why such a thing couldn't be written.

    Allan

Sign In or Register to comment.