JQuery Append does not work

1

I'm doing a load on demand I use a button that the user clicks on it loads the data from 2 to 2, this data comes from the database, but in that code I'll post it down below does not work when I put the. >

$(function(){

carregar(0, 2, "CarregarUsuario.php");

$("button.carregar-mais").click(function(evento){

    //desabilitando carregamento
    evento.preventDefault();

    var inicio = $("tbody tr").length;

    carregar(inicio, 2, "CarregarUsuario.php");
});

function carregar(inicio, maximo, url) {

    var dados = {inicio: inicio, maximo: maximo};

    $.post(url, dados, function (data) {

        for(i = 0; i < data.dados.length; i++) {

            $("tbody").append(
                "<tr>" +
                "<td>" + data.dados[i].CodigoUsuario + "</td>" + 
                "<td>" + data.dados[i].NomeUsuario + "</td>" + 
                "<td>" + data.dados[i].Email_Interno + "</td>" +
                "<td>" + data.dados[i].Senha + "</td>" +
                "</tr>"
            );
        }

        var conta = $("tbody tr").length;

        if(conta == data.resultadoQuantidade) {

            $("button.carregar-mais").hide();
        }
    },"json");
}

});

In this part here if I take the tr it works more I do not know why:

                "<td>" + data.dados[i].CodigoUsuario + "</td>" + 
                "<td>" + data.dados[i].NomeUsuario + "</td>" + 
                "<td>" + data.dados[i].Email_Interno + "</td>" +
                "<td>" + data.dados[i].Senha + "</td>"

Can anyone help me?

Personal just below is the part that connects to the database, I think it will be clearer how the code works:

<?php

include_once("ConexaoBancoDados.php");

$inicio = $_POST['inicio'];
$maximo = $_POST['maximo'];
// var_dump($_POST);
// $inicio = 1;
// $maximo = 10;

$resultUsuarioCont = mysqli_query($conn, "SELECT * FROM usuarios");

$resultado["resultadoQuantidade"] = mysqli_num_rows($resultUsuarioCont);

$resultUsuario = mysqli_query($conn, "SELECT * FROM usuarios LIMIT $inicio, $maximo");

if($resultado["resultadoQuantidade"] > 0) {

    while($linhaUsuario = mysqli_fetch_assoc($resultUsuario)) {
        $resultadoDados[] = $linhaUsuario;
    }

    $resultado["dados"] = $resultadoDados;
}else {
    $resultado["dados"] = null;
    $resultado["resultadoQuantidade"];
}
// var_dump($resultado["dados"]);
header('Content-type: application/json');
echo json_encode($resultado);
    
asked by anonymous 30.12.2016 / 14:08

2 answers

0

I was able to find the problem I think I'm going to open a new question, the problem is in php, json_encode is returning null, I do not know why, I printed the query until before passing json_encode and everything came true, but when I play data within that function returns null.

    
06.01.2017 / 15:02
1

Try to give an id to your table and use the code below to see if it works.

$("#idDaTabela tr:last").after(...);

Update:

There also seems to be a problem with how you are selecting the button. Put the "button-load-plus" id (see that I removed the id point) on the button and call it:

$("#button-carregar-mais").on("click", function(evento){...});

See the example:

$(function() {
  $("#button-carregar-mais").on("click", function(evento) {
    $("#tabela tr:last").after("<tr><td>Nova Linha</td></tr>");
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><buttonid="button-carregar-mais">
  Botão
</button>
<table id="tabela">
  <tbody>
    <tr>
      <td>Linha Existente</td>
    </tr>
  </tbody>
</table>
    
30.12.2016 / 17:41