2
I need to scroll through a table to know if an element already exists in the first column.
Table:
<table id="tabela-resultado" class="table table-bordered table-striped table-hover">
<thead>
<tr>
<th>Nome</th>
<th>Ativo</th>
<th class="text-center">Ação</th>
</tr>
</thead>
<tbody>
<?php if(count($records)) { ?>
<?php foreach($records as $record) { ?>
<tr data-id="<?php echo $record->id; ?>">
<td>
<?php echo $record->nome; ?>
</td>
<td>
<?php
if ($record->ativo == 1) {
echo "SIM";
} else if($record->ativo == 0){
echo "NÂO";
}
?>
</td>
<td>
<?php echo anchor("gurpoProduto/excluir", "<i class='glyphicon glyphicon-trash'></i> Exlcuir", ['class' => 'btn btn-danger btn-block', 'name' => 'delete']); ?>
</td>
</tr>
<?php
}
}
?>
</tbody>
</table>
What I tried to:
function verificar_existencia(nome) {
var table = $("#tabela-resultado table tbody");
var nomeTabela = '';
table.find('tr').each(function(nome) {
var $tds = $(this).find('td'),
nomeTabela = $tds.eq(0).text()
});
if (trim(nome.toUpperCase()) === trim(nomeTabela.toUpperCase())) {
toastr.success("Este grupo já existe!", "Aviso");
//return false;
}
}
When calling the function verifica_existencia(nome)
, don’t call me back toastr.success("Este grupo já existe!", "Aviso");
when I enter an existing element.
In
$("#tabela-resultado table tbody")
you are selecting the elementtbody
, within atable
, within the element#tabela-resultado
, however thisid
belongs to the table itself. Try removing thistable
and see if anything changes.– Woss
In fact, if PHP code is not essential to the problem, which is the case, try to avoid posting it. For example, run this PHP chunk on your server and only use the generated HTML in the question. So we will have how to test your problem without having to create a table from scratch. You can read more on how to create a [mcve].
– Woss
look I removed the table and it still didn’t work out
– HawkB
Can apply the second hint, on the [mcve]?
– Woss
there’s no way you can help me like this?
– HawkB