I would like to know how to Codeigniter
change the state of the activate button so that it is green, when it is deactivated it is red, but when I activate it it is green.
Controller code:
if ($activos =='true'){
$actinomtest ='Activar';
$valoresacti ='1';
$colorbtn = 'class="btn btn success';
}else{
$actinomtest ='Desactivar';
$valoresacti ='0';
}
And in this line within the same Controller called those variables:
foreach ($data as $row){
$botones='<button class="btn btn-primary btn-sm btnedit text-center" onclick="botontest('.$row->idTest.')">Editar</button>
<a class="btn btn-warning" href="#">Editar tabla</a>
<button class="btn btn-danger btn-sm btnactest text-center" onclick="btnactivtest('.$row->idTest.', '. $valoresacti.', '.$colorbtn .')">'.$actinomtest.'</button>';
}
There are several ways to do it. The simplest, for me, would be to store in the variable
$colorbtn
only the class that gives the color to your button. In your case they would be the classbtn-success
for the color green andbtn-danger
for the red.Your code could look like this:
Now you concatenate the variable
$colorbtn
inside the button's class attribute like this:<button class="btn '.$colorbtn.' btn-sm btnactest text-center"'
...The complete code would look like this:
}
Here is a link to the bootstrap 4 documentation on buttons: Buttons in bootstrap