Generate HTML elements via Javascript

Asked

Viewed 909 times

2

I need to create a code that after passing some parameters it generates a graph similar to the image below, I thought of creating a table for each activity. Does anyone have any tips, or do you know an article that could help me? I think about using Json to pass the data (I know how to work with Json), my problem would be creating this Function in Javascript

inserir a descrição da imagem aqui

This table is defined by dependencies, as we see in the image the activity B is dependent on the activity To

My Json will have a structure similar to the one below:

<script>
$(document).ready(function () {

    $.ajax({
        type: "GET",
        url: "/Venda/GetDadosItensVenda",
        success: function (itensVenda) {

            if (itensVenda != null) {

                $('#tbody').children().remove();

                $(itensVenda).each(function (i) {

                    var tbody = $('#tbody');
                    var tr = "<tr>";
                    tr +=
                    tr += "<td>" + itensVenda[i].CodigoProduto;
                    tr += "<td>" + itensVenda[i].Quantidade;
                    tr += "<td>" + itensVenda[i].PrecoUnitario;
                    tr += "<td>" + "<button class='btn btn-info' onclick=Editar(" + itensVenda[i].Id + ")>" + "Editar";
                    tr += "<td>" + "<button class='btn btn-danger' onclick=Deletar(" + itensVenda[i].Id + ")>" + "Deletar";

                    tbody.append(tr);

                });
            }
        }
    });
});

`

  • 1

    When you say "a graph" do you refer to the entire image? And is it important that it be all an image? In my opinion, the simplest would be to create several tables (since it is easier to create and style elements in the DOM than to draw arbitrary tables on a canvas), positioning them absolutely in an area of the page. Arrows could be a single image, rotated with CSS. P.S. Although it is important to generate an image, there is a library that can transform the visual result of the page into an image.

  • 1

    By the way, do you already know or have you defined what this JSON would look like? Would it be just a declarative description of the data (and the program draws as you want) or would it also have specified the positioning? If not defined. I would suggest a list of lists, to be drawn from left to right, where each element is either "beginning" or is a structure with the table values (the tables always have the same format, right?). Just one question: are all the elements on the right "reachable" by all the elements on the left? Could for example have an E with arrow coming out of B but not C?

  • @mgibsonbr It would not be an image no, it would be several tables even. I edited my question with a Json similar to what I want to use.

  • 1

    Does it have to be in this layout? I wonder why we have several frameworks that can help to build a chart. But in this layout I find difficult. Especially because it doesn’t look like a graph rsrs

  • Not that it needs to be a graph, just be something visual to inform dependencies between activities. I’m open to suggestions.

2 answers

1

JC, in your example you are forgetting to close the tags, in any case I would use a template to build the HTML.

Here’s an example with Handlebars.

$(function () {

  var source = $("#tmpl-registros").html();
  var template = Handlebars.compile(source);
  var tabela = $('#tbody');
  
  $.ajax({
    type: "GET",
    url: "/Venda/GetDadosItensVenda",
    success: function (itensVenda) {
      if (itensVenda != null) {
        var html = template(itensVenda);
        tabela.html(html);
      }
    },
    error: function () {
      var itensVenda = [];
      for (var indice = 1; indice <= 20; indice++) {
        itensVenda.push({
          Id: indice, 
          CodigoProduto: indice, 
          Quantidade: indice, 
          PrecoUnitario: 1.23 * indice
        });
      }

      var html = template(itensVenda);
      tabela.html(html);
    }
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/handlebars.js/4.0.5/handlebars.min.js"></script>

<table>
  <thead>
    <tr>
      <th>CodigoProduto</th>
      <th>Quantidade</th>
      <th>PrecoUnitario</th>
      <th>Editar</th>
      <th>Deletar</th>
    </tr>
  </thead>
  <tbody id="tbody">
  </tbody>
</table>

<script id="tmpl-registros" type="text/x-handlebars-template">
  {{#each this}}
  <tr>
    <td>{{CodigoProduto}}</td>
    <td>{{Quantidade}}</td>
    <td>{{PrecoUnitario}}</td>
    <td><button class='btn btn-info' onclick=Editar({{Id}})>Editar</button></td>
    <td><button class='btn btn-danger' onclick=Deletar({{Id}})>Deletar</button></td>
  </tr>
  {{/each}}
</script>

-1

Try this:

<script>
function sua_funcao(parametroA) {
            if (window.XMLHttpRequest) {
                // code for IE7+, Firefox, Chrome, Opera, Safari
                xmlhttp = new XMLHttpRequest();
            } else {
                // code for IE6, IE5
                xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
            }
            xmlhttp.onreadystatechange = function() {
                if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
                    document.getElementById("tabela").innerHTML = xmlhttp.responseText;
                }
            }
            xmlhttp.open("GET","cria_tabela.php?parametro="+parametroA,true);
            xmlhttp.send();
}
</script>

This code goes in the head of your page, it takes an isolated page "cria_table.php" and feeds the item "table" in your code with the result of this page, so you can continue to program PHP with few lines of Javascript.

This is the item to be fed:

<div class="col-lg-12 col-md-12 col-xs-12 col-sm-12" id="tabela" >
</div>

And in the cria_tabela would have your tables created in PHP.

If you don’t already know, I recommend a search in bootstrap, for better appearance of the page.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.