I am filling a table by js
, this is my code:
$('#table').append(//recorremos la lista llenando la tabla
'<tr>' +
' <td>' + lista[i].nombre + '</td>' +
' <td>' + lista[i].estatus + '</td>' +
'</tr>'
);
The data I receive from estatus
is a false
or a true
. I need that instead of the phrase appearing ; when it is false put a green circle and when it is true, put a red one. I have this part of the code:
"class", "fa fa-circle text-danger"
Where would I have to put the condition to know the result, and how to integrate that in my td
for estatus
?
Starting from the following concern, I quote:
It could be achieved by creating a function:
That returns
text-success
(green) as a default value, while if itestatus
is ittrue
will betext-danger
(red) the resulting value.Once the above function has been defined, it could be implemented in the following snippet:
Namely:
Therefore, if it
this.estatus
istrue
the function will returntext-danger
:On the other hand:
Provoking the result observed in the previous Snippet .
native javascript
We could have the same results with Native JavaScript that will be explained later in a comparative table.
Native JavaScript Snippet:
Comparison table
The differences between the code written with jQuery and native JavaScript can be seen in the following comparison table:
$(lista).each(function() {});
lista.forEach((item) => {});
lista
, , or put another way: walk through it.$('#table').append('<p>Algún texto</p>')
table.insertAdjacentHTML('beforeend', '<p>Algún texto</p>'))
References
To learn more about the methods used, see the following references:
element.insertAdjacentHTML(position, text)
.append( content [, content ] )
jQuery.each( array, callback )
Array.prototype.forEach()
try this and see how it goes