Split string into parts with 2 parameters to find

0

Here is the code that takes the 2 column description of the table

var descricao = $(elemento).closest('tr').find('td').eq('1').html();  

This is the description

  

CALIFORNIA [2,90] [BRE25] [3x80] [BRD25] [ARTICLE: 1,000 - COLOR: 02] - WITH MORE PULLS

start break description

    var descricaoArray = descricao.split('[');
    var modelo = descricaoArray[0].trim();
    var medida = descricaoArray[1].trim();
    var bracoesq = descricaoArray[2].trim();
    var modulo = descricaoArray[3].trim();
    var bracodir = descricaoArray[4].trim();
    var revestimento = descricaoArray[5].split(']');

end of break description, and here I move to inputs

    $('#resultDescricao').val(descricao);
    $('#modelo').val(modelo); 
    $('#medida').val(medida.replace(']', ''));
    $('#bracoesq').val(bracoesq.replace(']', '').replace('BRE', ''));
    $('#modulo').val(modulo.replace(']', '')); 
    $('#bracodir').val(bracodir.replace(']', '').replace('BRD', '')); 
    $('#revestimento option:contains(' + revestimento[0] + ')').prop('selected', true);
    $('#obs_item').val($.trim(obs_item));
    $('#vlr_unitario').val(vlr_unitario.replace(' ', ''));
    $('#qtde').val($('input[name="quant[' + id + ']"]').val());
    $('#num_modulos').val(num_modulos);

When the table loaded from the database works correctly

        public function carregar_itens_pedido($id_pedido) {
    $retorno = array();
    $sql = $this->db->prepare("SELECT 
    'itempedido'.'nomedesc' AS 'nomedesc',
    'itempedido'.'num_sofa' AS 'num_sofa',
    'itempedido'.'vlr_unitario' AS 'vlr_unitario',
    'itempedido'.'qtde_solicitada' AS 'qt',
    'itempedido'.'num_modulos' AS 'num_modulos' ,
    'itempedido'.idpedido  as id_pedido,
    'itempedido'.'obs_item' AS 'obs_item' ,
    'itempedido'.id  as id_produto    
    FROM   itempedido
     where itempedido.idpedido=:id_pedido ");
    $sql->bindValue(":id_pedido", $id_pedido);
    $sql->execute();
    $retorno = $sql->fetchAll();


    foreach ($retorno as $ret):
        $subTotal = $ret['vlr_unitario'] * $ret['qt'];
        ?>
        <script>
            var id = "<?php echo $ret['id_produto']; ?>";
            var num_sofa = "<?php echo $ret['num_sofa']; ?>";
            var name = "<?php echo $ret['nomedesc']; ?>";
            var price = "<?php echo $ret['vlr_unitario']; ?>";
            var qtde = "<?php echo $ret['qt']; ?>";
            var num_modulos = "<?php echo $ret['num_modulos']; ?>";
            var obs_item = "<?php echo $ret['obs_item']; ?>";

            subtotal = price * parseInt(qtde);
            subtotal = ("R$ " + number_format(subtotal, 2, ',', '.'));
            price = ("R$ " + number_format(price, 2, ',', '.'));

            if (qtde === null) {
                qtde = '';
            }

            var tr =
                    '<tr class="classeDaLinha" >' +
                    '<td class="id" id="id_linha">' + id + '</td>' +
                    '<td class="name">' + name + '</td>' +
                    '<td class="qtde" >' +
                    '<input style="width:50px;" type="number" name="quant[' + id + ']" id="quant[' + id + ']" class="p_quant" value="' + qtde + '"  onkeyup="updateSubtotal(this)" onchange="updateSubtotal(this)" data-price="' + price + '" />' +
                    '</td>' +
                    '<td class="num_modulos"> ' + num_modulos + '</td>' +
                    '<td class="obs_item"> ' + obs_item + '</td>' +
                    '<td style="width:100px;" class="price"> ' + price + '</td>' +
                    '<td style="width:100px;" class="subtotal">' + subtotal + '</td>' +
                    '<td class="acoes"><img class="delete" src="' + BASE_URL + '/assets/images/delete.png" width="20" height="20" title="Delete" onclick="excluirProd(this)"/>\n\
                    <img class="edit" src="' + BASE_URL + '/assets/images/edit.png" width="20" height="20" title="Editar" onclick="pegar_valor_linha_tabela_editar(this)"/></td>' +
            '</tr>';

            $('#products_table tbody').append(tr);
            updateTotal();
            preencherJason();
        </script>
        <?php
  endforeach;
}

After I loaded, I inserted a new line

function addProd() {
if ($('#modelo').val() === '') {
    alert('Informe um Modelo !');
    return;
}
if ($('#revestimento').val() === '') {
    alert('Selecione um Revestimento !');
    return;
}
if ($('#num_modulos').val() === '') {
    alert('Selecione a Quantidade de Módulos !');
    return;
}
if ($('#bracoesq').val() === '') {
    alert('Informe a medida Braço Esquerdo !');
    return;
}  
 if ($('#bracodir').val() === '') {
    alert('Informe a medida Braço Direito !');      
    return;
}
var products_table = document.getElementById('products_table');
var id = products_table.rows.length;
var name = document.getElementById('resultDescricao').value;
var price = document.getElementById('vlr_unitario').value.replace(/[ R|$|.]/gi, '').replace(/[,]/gi, '.');
var qtde = document.getElementById('qtde').value;
var num_modulos = document.getElementById('num_modulos').value;
var obs_item = document.getElementById('obs_item').value;

subtotal = price * parseInt(qtde);
subtotal = ("R$ " + number_format(subtotal, 2, ',', '.'));
price = ("R$ " + number_format(price, 2, ',', '.'));


incluir_produto();
document.getElementById('resultDescricao').value = "";
document.getElementById('modelo').value = "";
document.getElementById('vlr_unitario').value = 'R$0,00';
document.getElementById('bracoesq').value = "";
document.getElementById('bracodir').value = "";
$("#revestimento").val($("#revestimento option:first").val());
document.getElementById('qtde').value = "1";
document.getElementById('medida').value = "";
document.getElementById('modulo').value = "";
document.getElementById('obs_item').value = "";

if ($('input[name="quant[' + id + ']"]').length !== 0) {
    id = parseInt(id) + 1;

}

if ($('#id').val() != '') {              
   var tr =
   '<tr class="classeDaLinha" id="r'+id+'">' +
   '<td class="id" id="id_linha"  >' + id + '</td>' +
   '<td class="name">' + name + '</td>' +
   '<td class="qtde" >' +
   '<input style="width:50px;" type="number" name="quant[' + id + ']" id="quant[' + id + ']" class="p_quant" value="' + qtde + '"  onkeyup="updateSubtotal(this)" onchange="updateSubtotal(this)" data-price="' + price + '" />' +
   '</td>' +
   '<td class="num_modulos"> ' + num_modulos + '</td>' +
   '<td class="obs_item"> ' + obs_item + '</td>' +
   '<td class="price"> ' + price + '</td>' +
   '<td class="subtotal">' + subtotal + '</td>' +
   '<td class="acoes"><img class="delete" src="' + BASE_URL + '/assets/images/delete.png" width="20" height="20" title="Delete" onclick="excluirProd(this)"/>\n\
   <img class="edit" src="' + BASE_URL + '/assets/images/edit.png" width="20" height="20" title="Editar" onclick="pegar_valor_linha_tabela_editar(this)"/></td>' +'</tr>';

   OBJ_global.prev().after(tr); //colocar o novo tr antes deste
   OBJ_global.remove();
   var contador = 1;
   $(".classeDaLinha").each(function () {
             $(this).find("#id_linha").html(contador);
           $(this).find(".p_quant").prop("name", "quant["+contador + ]");
         contador++;       });
   $(OBJ_global).closest('tr').show();
   $('#id').val('');           
   OBJ_global = '';
} else {
    var tr =
    '<tr class="classeDaLinha">' +
    '<td class="id" id="id_linha"  >' + id + '</td>' +
    '<td class="name">' + name + '</td>' +
    '<td class="qtde" >' +
    '<input style="width:50px;" type="number" name="quant[' + id + ']" id="quant[' + id + ']" class="p_quant" value="' + qtde + '"  onkeyup="updateSubtotal(this)" onchange="updateSubtotal(this)" data-price="' + price + '" />' +
    '</td>' +
    '<td class="num_modulos"> ' + num_modulos + '</td>' +
    '<td class="obs_item"> ' + obs_item + '</td>' +
    '<td class="price"> ' + price + '</td>' +
    '<td class="subtotal">' + subtotal + '</td>' +
    '<td class="acoes"><img class="delete" src="' + BASE_URL + '/assets/images/delete.png" width="20" height="20" title="Delete" onclick="excluirProd(this)"/>\n\
            <img class="edit" src="' + BASE_URL + '/assets/images/edit.png" width="20" height="20" title="Editar" onclick="pegar_valor_linha_tabela_editar(this)"/></td>' +
    '</tr>';
$('#products_table tbody').append(tr);
}
updateTotal();
preencherJason();
}

this function breaks description, passes the values all wrong, stays like this input description gets

  

2 [ASDF [324] [BRE345] [435] [BRD345] [ARTICLE: 1,000 - COLOR: 01]] [BRE] [5] [BRD] [R $ 0,34]

and the measured input receives

  

ASDF [324] [BRE345] [435] [BRD345] [ARTICLE: 1,000 - COLOR: 01]

    
asked by anonymous 03.11.2017 / 03:15

1 answer

0

And people who need this code here

function pegar_valor_linha_tabela_editar(elemento) {//elemento da linha
//debugger;
//pegar os valores da tabela
var id = $(elemento).closest('tr').find('td').eq('0').text().trim();  // recebe id  
var descricao = $(elemento).closest('tr').find('td').eq('1').html().trim();  // recebe descriçao completa
var qtde = $('input[name="quant[' + id + ']"]').val().trim(); // recebe quantidade
var num_modulos = $(elemento).closest('tr').find('td').eq('3').text().trim(); // recebe numero de modulos
var obs_item = $(elemento).closest('tr').find('td').eq('4').text().trim(); // recebe observacao
var vlr_unitario = $(elemento).closest('tr').find('td').eq('5').text().trim(); // recebe valor unitario
OBJ_global = $(elemento).closest("tr"); //recebe objeto da linha

if (id === 0) {
    return;
} else {
    $('#id').val(id);
    //inicio quebra descriçao
    //aqui e coluna 2 da tabela eu pego e divico essas descrição
    //descriçao  CALIFORNIA [2,90] [BRE25] [3x80] [BRD25] [ARTIGO: 1.000 - COR: 02] - COM MAIS PUXES
    descricaoArray = descricao.split(/\[(.*?)\]/);//aqui pega o california e os dados dentro do colchete e com mais puxes fora do colchete no final
    modelo = descricaoArray[0].trim();
    medida = descricaoArray[1].trim();
    bracoesq = descricaoArray[3].trim();
    modulo = descricaoArray[5].trim();
    bracodir = descricaoArray[7].trim();
    revestimento = descricaoArray[9].trim();

    //fim do quebra descrição

    $('#resultDescricao').val(descricao);
    $('#modelo').val(modelo);
    $('#medida').val(medida);
    $('#bracoesq').val(bracoesq.replace('BRE', ''));
    $('#modulo').val(modulo);
    $('#bracodir').val(bracodir.replace('BRD', ''));
    $('#revestimento option:contains(' + revestimento + ')').prop('selected', true);
    $('#obs_item').val($.trim(obs_item));
    $('#vlr_unitario').val(vlr_unitario.replace(' ', ''));
    $('#qtde').val($('input[name="quant[' + id + ']"]').val());
    $('#num_modulos').val(num_modulos);
    excluirProd(elemento);
    }
    }
    
03.11.2017 / 06:14