active button

active button

Andreas S.Andreas S. Posts: 208Questions: 74Answers: 4

I have a problem to activate a button if no row is selected and deactivate if a row is selected.
I tried with this code:

            dtButtons.push( {
                text: '<i class="fas fa-check-double fa-fw"></i>',
                titleAttr: button_txt[3],
                className: 'text-Peru',
                action: function( e, dt, node, config ) {
                    var data = mt_sum.row( { deselected: true } ).data();
                    $.post( {
                        url: 'lib/adm/meet-api.php?mmap=7'
                    } )
                    .done( function( data ) {
                        var notify = ( undefined == data.title ) ? JSON.parse( data ) : data;
                        if( 'done' != notify.type ) {
                            pNotify( notify.title, notify.text, notify.icon, notify.type );
                        }
                        mt_sum.ajax.reload();
                    });
                },
                enabled: true
            } );

        mt_sum.on(
            'select', function () {
                var selectedRows = mt_sum.rows( { selected: true } ).count();
                if( 6 == uLevel || 5 == uLevel ) {
                    mt_sum.button( 3 ).enable( selectedRows === 1 );
                    mt_sum.button( 4 ).enable( selectedRows === 1 );
                } else if ( 4 == uLevel ) {
                    mt_sum.button( 3 ).enable( selectedRows === 1 );
                    mt_sum.button( 4 ).enable( selectedRows === 1 );
                }
            } );

            mt_sum.on(
            'deselect', function() {
                var selectedRows = mt_sum.rows( { selected: true } ).count();
                if( 6 == uLevel || 5 == uLevel ) {
                    mt_sum.button( 3 ).disable( selectedRows === 0 );
                    mt_sum.button( 4 ).disable( selectedRows === 0 );
                } else if( 4 == uLevel ) {
                    mt_sum.button( 3 ).disable( selectedRows === 0 );
                    mt_sum.button( 4 ).disable( selectedRows === 0 );
                }
            } );

Have anyone a hint for me.

Andreas

This question has accepted answers - jump to:

Answers

  • kthorngrenkthorngren Posts: 21,184Questions: 26Answers: 4,925
    edited July 2018 Answer ✓

    What is uLevel? Maybe you can use console.log(uLevel); in your functions to make sure it is what you expect. Could be that none of the if statements are true causing the enable() and disable() statements to not be executed.

    Looks like if a row is selected you are enabling the button: enable( selectedRows === 1 ) and disabling the button if 0 rows are selected: disable( selectedRows === 0 ). This sounds opposite of your description above.

    Also you might need to change the statements to look like this:
    ``enable( selectedRows === 1 ? true : false );`

    To specifically return true or false.

    I do this with one of my buttons based on row selection:

        table.on( 'select deselect', function ( e, dt, type, indexes ) {
            var count = table.rows( { selected: true } ).count();
            
            if (count > 0) {
                table.button( 0 ).enable();
            } else {
                table.button( 0 ).disable();
            }
    
        } );
    

    If one or more rows are selected the button is enabled or disabled if no rows ar selected. Doing it this way seems like a simpler more readable code. You would just have to add your logic for the uLevel.

    HTH,
    Kevin

  • allanallan Posts: 63,237Questions: 1Answers: 10,418 Site admin
    Answer ✓

    I'd go a step further even than Kevin's excellent answer:

    table.on( 'select deselect', function ( e, dt, type, indexes ) {
        var count = table.rows( { selected: true } ).count();
        table.buttons( [3, 4] ).enable( count > 0 );
    } );
    

    That uses buttons().enable() to manipulate multiple buttons at a time.

    Allan

  • Andreas S.Andreas S. Posts: 208Questions: 74Answers: 4

    Thanks, for help to solve my Problem. Sometime I did not see the tree in the forest.

    Andreas

This discussion has been closed.