Disable/enable button and checkbox on the Dashboard

Hello, I want to enable the checkbox clicking the Boton 1 and then enable the boton 2 when the checkbox is checked.

I was able to achieve that when the checkbox is checked the Boton 2 was enable, but when i did the same with the checkboxes (pressing Boton 1 Enable All the checkboxes) it did´t work. Only work for the first checkbox of the table.

[{"id":"df5a1c69e7c3c33e","type":"inject","z":"71fbc1b8432e4c83","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"[{\"Marca\":\"Marca1\",\"Vencimiento\":\"Vencimiento1\",\"Modelo\":\"Modelo1\",\"id\":1},{\"Marca\":\"Marca2\",\"Vencimiento\":\"Vencimiento2\",\"Modelo\":\"Modelo2\",\"id\":2},{\"Marca\":\"Marca3\",\"Vencimiento\":\"Vencimiento3\",\"Modelo\":\"Modelo3\",\"id\":3},{\"Marca\":\"Marca4\",\"Vencimiento\":\"Vencimiento4\",\"Modelo\":\"Modelo4\",\"id\":4},{\"Marca\":\"Marca5\",\"Vencimiento\":\"Vencimiento5\",\"Modelo\":\"Modelo5\",\"id\":5}]","payloadType":"json","x":550,"y":340,"wires":[["33c971a311314e52"]]},{"id":"33c971a311314e52","type":"ui_template","z":"71fbc1b8432e4c83","group":"95d436df7bf84561","name":"","order":1,"width":14,"height":15,"format":"<div class=\"main\">\n    \n<div class=\"borde\">\n<input type=\"button\" class=\"boton\"  value=\"boton 1\"  onclick=\"check3()\" id=\"boton1\"/>\n\n</md-button>\n<div/>\n\n    <table border=\"1\" cellpadding=\"0\" cellspacing=\"0\" bordercolor=\"#D0D0D0\" id=\"Table1\" style=\"width:100%\">\n        <tr class=\"fondotitulo\">\n            <th>Marca</th>\n            <th>Vencimiento</th>\n            <th>Modelo</th>\n            <th>Id</th>\n            <th>✔</th>\n\n        </tr>\n        <tr ng-repeat=\"x in msg.payload | limitTo:100\">\n\n            <td class=\"fondotabla\" style=\"text-align:center;\">{{x.Marca}}</td>\n            <td class=\"fondotabla\" style=\"text-align:center;\">{{x.Vencimiento}}</td>\n            <td class=\"fondotabla\" style=\"text-align:center;\">{{x.Modelo}}</td>\n            <td class=\"fondotabla\" style=\"text-align:center;\">{{x.id}}</td>\n            <td class=\"fondotabla\" style=\"text-align:center;\">\n                <input data-id=\"{{x.id}}\" data-marca=\"{{x.Marca}}\" data-vencimiento=\"{{x.Vencimiento}}\" data-modelo=\"{{x.Modelo}}\" type=\"checkbox\" onclick=\"enable()\" id=\"checkbox\"/>\n            </td>\n        </tr>\n    </table>\n\n</div>\n\n\n<div class=\"borde\">\n<input type=\"button\" class=\"boton\" value=\"Boton 2\"  onclick=\"check2()\" id=\"boton2\" />\n\n</md-button>\n</div>\n</div>\n</div>\n<script type=\"text/javascript\">\n\ndocument.getElementById(\"boton2\").disabled = true;\n\n//document.getElementById(\"checkbox\").disabled = true;\n\n//Declare once to make scope available in functions\nvar theScope = scope;\n\nfunction enable(){\n    document.getElementById(\"boton2\").disabled = false;\n}   \n   \n   \n   \n   \n    function check() {\n        //Reference the Table.\n        var grid = document.getElementById(\"Table1\");\n \n        //Reference the CheckBoxes in Table.\n        var checkBoxes = grid.getElementsByTagName(\"INPUT\");\n        //console.log(checkBoxes)\n \n\n        let selected = []\n        //Loop through the CheckBoxes.\n        for (var i = 0; i < checkBoxes.length; i++) {\n            if (checkBoxes[i].checked) {\n                var id = checkBoxes[i].dataset.id;\n                var marca = checkBoxes[i].dataset.marca;\n                var vencimiento = checkBoxes[i].dataset.vencimiento;\n                var modelo = checkBoxes[i].dataset.modelo;\n                \n                selected.push( {id, marca, vencimiento, modelo})\n            }\n        }\n\n //console.log(selected)\ntheScope.send({payload:selected}) // send back top NR\n\n   \n }\n   \n</script>","storeOutMessages":false,"fwdInMessages":false,"resendOnRefresh":true,"templateScope":"local","x":720,"y":340,"wires":[["1f90549fc7aa897a"]]},{"id":"1f90549fc7aa897a","type":"debug","z":"71fbc1b8432e4c83","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":960,"y":340,"wires":[]},{"id":"5f3c1cdb4b121977","type":"ui_template","z":"71fbc1b8432e4c83","group":"95d436df7bf84561","name":"Estilo Css","order":2,"width":0,"height":0,"format":"<style>\n\n.main\n{\n    height:500px;\n}\n.datos{\n  width: 100px;\n\n}\n.fondotabla{\n    background-color:#FFFDE7; \n\n    \n}\n\n\n.fondotitulo{\n    background-color:#FCF799; \n   \n    \n}\n.contenedor{\n    \n  \n    \n    \n    \n    \n}\n.boton{\nbackground-color:#FCF799; \nwidth:200px;\nheight:40px;\nborder:none;\nborder-radius: 8px;\noutline: none;\n\n\n}\n\n.boton:hover{\n    Background-color:#FFFDE7;\n    transition:all .4s ease;\n    box-shadow:0.5px 1px 11px 0px rgba(89,87,87,0.85);\n}\n\n.boton:active {\n background-color:#FCF799; \n transition:all .05s ease;\n}\n    \n.borde{\nborder:2px solid #d0d0d0;\nborder-radius: 8px;\nwidth:200px;\nheight:40px;\nmargin-left:40%;\nborder-radius: 8px;\nmargin-top:1%;\n}\n</style>","storeOutMessages":true,"fwdInMessages":true,"resendOnRefresh":true,"templateScope":"local","x":720,"y":260,"wires":[[]]},{"id":"95d436df7bf84561","type":"ui_group","name":"prueba","tab":"4a5d91cd891c3256","order":1,"disp":true,"width":14,"collapse":false},{"id":"4a5d91cd891c3256","type":"ui_tab","name":"prueba","icon":"dashboard","order":15,"disabled":false,"hidden":false}]

Thanks, Lucas

The naming of your functions linked to the click events on the buttons didnt match but i guess that was for experimenting.

Try this :

[{"id":"df5a1c69e7c3c33e","type":"inject","z":"4895ea10b4ee9ead","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"[{\"Marca\":\"Marca1\",\"Vencimiento\":\"Vencimiento1\",\"Modelo\":\"Modelo1\",\"id\":1},{\"Marca\":\"Marca2\",\"Vencimiento\":\"Vencimiento2\",\"Modelo\":\"Modelo2\",\"id\":2},{\"Marca\":\"Marca3\",\"Vencimiento\":\"Vencimiento3\",\"Modelo\":\"Modelo3\",\"id\":3},{\"Marca\":\"Marca4\",\"Vencimiento\":\"Vencimiento4\",\"Modelo\":\"Modelo4\",\"id\":4},{\"Marca\":\"Marca5\",\"Vencimiento\":\"Vencimiento5\",\"Modelo\":\"Modelo5\",\"id\":5}]","payloadType":"json","x":390,"y":860,"wires":[["33c971a311314e52"]]},{"id":"33c971a311314e52","type":"ui_template","z":"4895ea10b4ee9ead","group":"95d436df7bf84561","name":"","order":1,"width":14,"height":15,"format":"<div class=\"main\">\n\n    <div class=\"borde\">\n        <input type=\"button\" class=\"boton\"  value=\"boton 1\"  onclick=\"enableCheckboxes()\" id=\"boton1\"/>\n        <div />\n\n        <table border=\"1\" cellpadding=\"0\" cellspacing=\"0\" bordercolor=\"#D0D0D0\" id=\"Table1\" style=\"width:100%\">\n            <tr class=\"fondotitulo\">\n                <th>Marca</th>\n                <th>Vencimiento</th>\n                <th>Modelo</th>\n                <th>Id</th>\n                <th>✔</th>\n\n            </tr>\n            <tr ng-repeat=\"x in msg.payload | limitTo:100\">\n\n                <td class=\"fondotabla\" style=\"text-align:center;\">{{x.Marca}}</td>\n                <td class=\"fondotabla\" style=\"text-align:center;\">{{x.Vencimiento}}</td>\n                <td class=\"fondotabla\" style=\"text-align:center;\">{{x.Modelo}}</td>\n                <td class=\"fondotabla\" style=\"text-align:center;\">{{x.id}}</td>\n                <td class=\"fondotabla\" style=\"text-align:center;\">\n                    <input disabled data-json=\"{{x}}\"  type=\"checkbox\" onclick=\"enable()\" id=\"checkbox\"/>\n                </td>\n            </tr>\n        </table>\n\n    </div>\n\n\n    <div class=\"borde\">\n        <input type=\"button\" class=\"boton\" value=\"Boton 2\"  onclick=\"sendSelection()\" id=\"boton2\" disabled />\n\n        </md-button>\n    </div>\n</div>\n</div>\n<script type=\"text/javascript\">\n\n//Declare once to make scope available in functions\nvar theScope = scope;\n\nfunction enableCheckboxes() {\n    var grid = document.getElementById(\"Table1\");\n    var checkBoxes = Array.from(grid.getElementsByTagName(\"input\"));\n\n    // check if any checkBoxes are present and enable them\n    if (checkBoxes.length > 0) {\n    checkBoxes.forEach( el => el.disabled = false)  \n    }\n}\n\n// enable button 2 when a checkbox is checked\nfunction enable(){\n    document.getElementById(\"boton2\").disabled = false;\n}   \n   \n  \nfunction sendSelection() {\n        //Reference the Table.\n        var grid = document.getElementById(\"Table1\");\n \n        //Reference the CheckBoxes in Table.\n        var checkBoxes = Array.from(grid.getElementsByTagName(\"input\"));\n        //console.log(checkBoxes)\n \n        let selected = []\n        //Loop through the CheckBoxes.\n        checkBoxes.forEach( cb => {\n           if (cb.checked) {\n           var json = cb.dataset.json;\n           selected.push( JSON.parse(json) )\n          }\n        })\n\n     //console.log(selected)\n    theScope.send({payload:selected}) // send back to NR\n }\n\n\n   \n</script>","storeOutMessages":false,"fwdInMessages":false,"resendOnRefresh":true,"templateScope":"local","x":560,"y":860,"wires":[["1f90549fc7aa897a"]]},{"id":"1f90549fc7aa897a","type":"debug","z":"4895ea10b4ee9ead","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":800,"y":860,"wires":[]},{"id":"5f3c1cdb4b121977","type":"ui_template","z":"4895ea10b4ee9ead","group":"95d436df7bf84561","name":"Estilo Css","order":2,"width":0,"height":0,"format":"<style>\n    .main {\n        height: 500px;\n        width: 600px;\n        margin-left: 0px;\n    }\n\n    .datos {\n        width: 100px;\n\n    }\n\n    .fondotabla {\n        background-color: #FFFDE7;\n\n\n    }\n\n\n    .fondotitulo {\n        background-color: #FCF799;\n\n\n    }\n\n    .contenedor {}\n\n    .boton {\n        background-color: #FCF799;\n        width: 200px;\n        height: 40px;\n        border: none;\n        border-radius: 8px;\n        outline: none;\n\n\n    }\n\n    .boton:hover {\n        background-color: #FFFDE7;\n        transition: all .4s ease;\n        box-shadow: 0.5px 1px 11px 0px rgba(89, 87, 87, 0.85);\n    }\n\n    .boton:active {\n        background-color: #FCF799;\n        transition: all .05s ease;\n    }\n\n \n\n    .borde {\n        border: 2px solid #d0d0d0;\n        border-radius: 8px;\n        width: 200px;\n        height: 40px;\n        margin-left: 40%;\n        border-radius: 8px;\n        margin-top: 1%;\n    }\n</style>","storeOutMessages":true,"fwdInMessages":true,"resendOnRefresh":true,"templateScope":"global","x":560,"y":780,"wires":[[]]},{"id":"95d436df7bf84561","type":"ui_group","name":"prueba","tab":"4a5d91cd891c3256","order":1,"disp":true,"width":14,"collapse":false},{"id":"4a5d91cd891c3256","type":"ui_tab","name":"prueba","icon":"dashboard","order":15,"disabled":false,"hidden":false}]

When boton 1 is clicked enableCheckboxes function runs that loops through the checkboxes (if there are any) and enables them.

Then when any checkbox is checked .. then boton 2 is enabled

I hope this is what you were after

Perfect!! It Works.
thank you so much!

1 Like

I have one more doubt, how i can get only the id from the table. I just want to send the id on the payload.
in the code that i send you the function was

if (checkBoxes[i].checked) {
                var id = checkBoxes[i].dataset.id;
                var marca = checkBoxes[i].dataset.marca;
                var vencimiento = checkBoxes[i].dataset.vencimiento;
                var modelo = checkBoxes[i].dataset.modelo;
                
                selected.push( {id, marca, vencimiento, modelo})

Now you use json and i don´t understan how i can get only the id.

During the selected.push( {id, marca, vencimiento, modelo}) command
we push an object { } with id, marca etc to the selected array

If you dont want all the data .. simply push to selected array only the id

selected.push(id)

or with the modified code i sent you

let selected = []
        //Loop through the CheckBoxes.
        checkBoxes.forEach( cb => {
           if (cb.checked) {
           let json = cb.dataset.json;

           selected.push( JSON.parse(json).id )
          }
        })
1 Like

Great, thanks!

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.