Multiple checkboxes in a row

Multiple checkboxes in a row

klinglerklingler Posts: 90Questions: 42Answers: 2

Good morning (o;

Set up a simple table where I display a list of suppliers with different states, like being active, showing on frontpage and use it in statistics.

I followed this example to be able to update the state when just toggling the checkboxes:

https://editor.datatables.net/examples/api/checkbox.html

Only problem (so far ;o) is that I can't set the checked set on each checkbox individually as per example as it handles only one per row:

        rowCallback: function ( row, data ) {
            // Set the checked state of the checkbox in the table
            $('input.editor-active', row).prop( 'checked', data.Aktiv == 1 );
        }

With this callback all checkboxes have the same state as the selector applies to all checkboxes per row.

thanks in advance
richard

Answers

  • klinglerklingler Posts: 90Questions: 42Answers: 2

    Okay..seems I have to use a different class per checkbox...this seems to work:

            rowCallback: function ( row, data ) {
                console.log(row, data);
                // Set the checked state of the checkbox in the table
                $('input.editor-active1', row).prop( 'checked', data.Aktiv == 1 );
                $('input.editor-active2', row).prop( 'checked', data.Anzeigen == 1 );
                $('input.editor-active3', row).prop( 'checked', data.Statistik == 1 );
            }
    

    And also needs individual editor updates per checkbox:

        $('#lieferanten').on( 'change', 'input.editor-active1', function () {
            editor
                .edit( $(this).closest('tr'), false )
                .set( 'Aktiv', $(this).prop( 'checked' ) ? 1 : 0 )
                .submit();
        } );
        $('#lieferanten').on( 'change', 'input.editor-active2', function () {
            editor
                .edit( $(this).closest('tr'), false )
                .set( 'Anzeigen', $(this).prop( 'checked' ) ? 1 : 0 )
                .submit();
        } );
        $('#lieferanten').on( 'change', 'input.editor-active3', function () {
            editor
                .edit( $(this).closest('tr'), false )
                .set( 'Statistik', $(this).prop( 'checked' ) ? 1 : 0 )
                .submit();
        } );
    

    Maybe there is a shorter version?

    Also don't know why the POST sends an empty string for inactive checkboxes....only 1 for active ones.

  • allanallan Posts: 63,498Questions: 1Answers: 10,470 Site admin

    seems I have to use a different class per checkbox...this seems to work:

    Yup - exactly that, otherwise the selector that sets the states wouldn't be able to tell the difference between the checkboxes!

    You could possibly put your code into a function call like this:

    checkbox( editor, this, 'Statistik' );
    

    Where checkbox would basically replicate those lines, but using the passed in variables.

    Allan

Sign In or Register to comment.