Copy data from one table to another JS / HTML

Asked

Viewed 1,066 times

1

I have 2 tables (tab1 and tab2) tab1 has 'inputs' to register information. tab2 returns products already registered in the database.

I would like to create a function that when selecting the line(checkbox) the value of the "td" "product" is sent to the first line of the tab1 in the "valprod" field. I’m using an example of another question(From one table to another html table) but in this example tab1 takes the whole value of "tr".

var adicionar = document.querySelectorAll("input[type='checkbox']");

var tab1 = document.querySelector("#tab1 tbody");
var tab2 = document.querySelector("#tab2 tbody");
var adicionarOnClick = function () {    
    var escopo = this.checked ? tab1 : tab2;   
    escopo.appendChild(this.parentNode.parentNode);
};

for (var indice in adicionar) {
    adicionar[indice].onclick = adicionarOnClick;
}

//---------------------------
    (function ($) {
        RemoveTablePRow = function (handler) {
            var tr = $(handler).closest('tr');

            tr.fadeOut(200, function () {
                tr.remove();
            });

            return false;
        };
    })(jQuery);

    (function ($) {
        AddTablePRow = function () {
            var newRow = $("<tr>");
            var cols = "";
            cols += '<td><a href="#">+</a><input type="text" name="valprod" value="0"></td>';
            cols += '<td><input type="text" name="prcvenda" value=""></td>';
            cols += '<td <a href="#" class="removerCampo" onclick="RemoveTablePRow(this)">Remover Linha</a></td>';
            newRow.append(cols);
            $("#tab1").append(newRow);

            return false;
        };
    })(jQuery);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<h1>
    Tabela 1
</h1>
<a onclick="AddTablePRow()" href="#">Adicionar</a>
<form method="post">
    <table id="tab1" nome="tab1">
        <thead>
        <tr>
            <th>Valor do Produto</th>
            <th>Preço de Venda</th>
        </tr>
        </thead>
        <tbody>
        <tr>
            <td><a href="#">+</a>
                <input type="text" name="valprod" value="50">
            </td>
            <td>
                <input type="text" name="prcvenda" value="">
            </td>
            <td>
                <a href="#" onclick="RemoveTablePRow(this)">Remover Linha</a>
            </td>
        </tr>

        </tbody>
    </table>
    <hr>
    <h1>
        Tabela 2
    </h1>
    <table id="tab2" nome="tab2">
        <thead>
        <tr>
            <th>Selecionar</th>
            <th>Codigo</th>
            <th>Descrição</th>
            <th>Valor</th>            
        </tr>
        </thead>
        <tbody>
        <tr>
            <td>
                <input type="checkbox">
            </td>
            <td><a href="#">001</a></td>
            <td>Caneta</td>
            <td>23,00</td>
        </tr>       
        </tbody>
    </table>
    <button class="btn_enviar">Enviar</button>
</form>

  • Hello, make sure that this is useful > https://jsfiddle.net/4Lpu7vao/ . If so, let me know that I publish it as a response.

  • Hello @Lucaoa, based on your code I am adding the values of the fields into variables(var valprod = $(this).find('[id="valprod"]').val();), and then add in the fields of Tabela1. This would be the right way to go?

  • do not recommend you to work with id, because id has to be unique, javascript will always catch the first one you find. I recommend changing to classes. Another thing, it would be better if you updated jsfiddle so I can help you better.

  • Well... the way you were doing it didn’t work out so well. I ended up testing another way to copy the data, but it’s not exactly the way I need it. example: https://jsfiddle.net/LGian/4Lpu7vao/3/

  • @Gain, this post will help you, http://stackoverflow.com/questions/1954426/javascript-equivalent-of-phps-list

  • @Lucaoa, by your tip worked here, but still not 100% the way I need. I would like the information of the marked items to be sent to the other table only after clicking the send button. If you have any tips I appreciate. Ex updated: https://jsfiddle.net/4Lpu7vao/15/

Show 1 more comment
No answers

Browser other questions tagged

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