Checkbox datatables editor not working

Checkbox datatables editor not working

rvdsarrrvdsarr Posts: 2Questions: 2Answers: 0

Hey guys,

Here's my code for my editor's datatable with a checkbox. The problem is that when the checkbox is clicked, it is instantly un checked and the data is not saved to the database.

Can someone point me in the right direction as to why this is not working?

var editorproducttable;

$(function() {    

    editorproducttable = new $.fn.dataTable.Editor( {
        "ajax": "/functions/icecat_table.php?do=producttable",
        "table": "#productsall",
        "fields": [ {
                label: "Code",
                name:  "products.code"
            }, {
                label: "Name",
                name:  "products.name"
            }, {
                label: "Brand",
                name:  "brands.name"
            }, {
                label: "Modelno.",
                name:  "products.manufacturer"
            }, {
                label: "Price",
                name:  "products.sellprice"
            }, {
                label: "Stock",
                name:  "products.stock"
            }, {
                label: "Productgroup",
                name:  "categories.name"
            }, {
                label:     "Active:",
                name:      "activewebsite",
                type:      "checkbox",
                separator: "|",
                options:   [
                    { label: '', value: 1 }
                ]
            }
        ]
    } );

    oTable = $('#productsall').dataTable( {
        dom: "Bfrtip",
        ajax: {
            url: "/functions/icecat_table.php?do=producttable",
            type: "POST"
        },
        //serverSide: true,        
        // scrollY:        "600px",
        // scrollCollapse: true,
        // paging:         false,
        columns: [
            { data: "products.code" },
            { data: "products.name" },
            { data: "brands.name" },
            { data: "products.manufacturer" },
            { data: "products.sellprice" },
            { data: "products.stock" },
            { data: "categories.name" },
            { data: "products.activewebsite",
                render: function ( data, type, row ) {
                    if ( type === 'display' ) {
                        return '<input type="checkbox" class="editor-active">';
                    }
                    return data;
                },
                className: "dt-body-center"
            }
        ],
        rowCallback: function ( row, data ) {
            // Set the checked state of the checkbox in the table
            $('input.editor-active', row).prop( 'checked', data.products.activewebsite == 1 );
        },
        fnDrawCallback: function(){
            clickRowHandler();
        }
    });
 
    $('#productsall').on( 'change', 'input.editor-active', function () {
        editorproducttable
            .edit( $(this).closest('tr'), false )
            .set( 'activewebsite', $(this).prop( 'checked' ) ? 1 : 0 )
            .submit();
    }); 
include( "datatables/DataTables.php" );

use
    DataTables\Editor,
    DataTables\Editor\Field,
    DataTables\Editor\Format,
    DataTables\Editor\Join,
    DataTables\Editor\Upload,
    DataTables\Editor\Validate;

if (isset($_REQUEST)){
    if (isset($_REQUEST['do']) && $_REQUEST['do'] == 'producttable'){

        Editor::inst( $db, 'products' )
            ->fields(
                Field::inst( 'products.code' ),
                Field::inst( 'products.name' ),        
                Field::inst( 'brands.name' ),
                Field::inst( 'products.manufacturer' ),
                Field::inst( 'products.sellprice' ),
                Field::inst( 'products.stock' ),
                Field::inst( 'categories.name' ),
                Field::inst( 'products.activewebsite' )
                    ->setFormatter( function ( $val, $data, $opts ) {
                        return ! $val ? 0 : 1;
                    } )
            )
            ->leftJoin( 'brands', 'brands.id', '=', 'products.brand' )
            ->leftJoin( 'categories', 'categories.category_id', '=', 'products.productgroup' )
            ->process( $_POST )
            ->json();
    }

Answers

  • jobloggsjobloggs Posts: 8Questions: 3Answers: 0

    hi RV,

    did you work this out, I'm getting the same problem.

  • allanallan Posts: 63,761Questions: 1Answers: 10,510 Site admin

    Are you able to give me a link to the page you are working on so I can take a look and debug what the issue is please?

    Thanks,
    Allan

This discussion has been closed.