add disabled class to a button 'on the fly'

add disabled class to a button 'on the fly'

crush123crush123 Posts: 417Questions: 126Answers: 18
edited March 2015 in DataTables 1.10

I have a stock list from a json source with an 'add to cart' button on each row,

each item is unique, so I want to disable the add to cart button if it is already in the cart.

I have created an array of item ids and am using this to test against the row id, but the button is not being disabled

{ data: "tblitem.ItemID",
        "render": function ( data, type, row ) {
                var buttonclass = '';
                var inmycart = [<?php echo $incart; ?>];
                if($.inArray(data, inmycart) !== -1) {
                    buttonclass = ' disabled';
                } else {
                    buttonclass = '';
                }
                return '<form name="schoolshopcart_1_ATC_'+data+'" method="POST" action="<?php echo $_SERVER["PHP_SELF"]; ?><?php echo (isset($_SERVER["QUERY_STRING"]) && $_SERVER["QUERY_STRING"] != "")?"?".htmlentities($_SERVER["QUERY_STRING"]):""; ?>"><input type="hidden" name="schoolshopcart_1_ID_Add" value="'+data+'" ><input type="hidden" name="schoolshopcart_1_Quantity_Add" value="1" size="4" ><input type="submit" class="btn btn-primary'+buttonclass+'" value="Add to Basket" name="schoolshopcart_1_ATC"></form>';
            }
        }

This question has an accepted answers - jump to answer

Answers

  • crush123crush123 Posts: 417Questions: 126Answers: 18
    edited March 2015 Answer ✓

    Fixed it.

    my 'data' was being treated as a string, so the inArray function was always returning -1, so I wrapped parseInt around it and it works fine

    { data: "tblitem.ItemID",
            "render": function ( data, type, row ) {
                    var buttonclass = '';
                    var inmycart = [<?php echo $incart; ?>];
                    if($.inArray(parseInt(data), inmycart) !== -1) {
                        buttonclass = ' disabled';
                    } else {
                        buttonclass = '';
                    }
                    return '<form name="schoolshopcart_1_ATC_'+data+'" method="POST" action="<?php echo $_SERVER["PHP_SELF"]; ?><?php echo (isset($_SERVER["QUERY_STRING"]) && $_SERVER["QUERY_STRING"] != "")?"?".htmlentities($_SERVER["QUERY_STRING"]):""; ?>"><input type="hidden" name="schoolshopcart_1_ID_Add" value="'+data+'" ><input type="hidden" name="schoolshopcart_1_Quantity_Add" value="1" size="4" ><input type="submit" class="btn btn-primary'+buttonclass+'" value="Add to Basket" name="schoolshopcart_1_ATC"></form>';
                }
            }
    
This discussion has been closed.