Split string into parts with 2 parameters to find

Asked

Viewed 50 times

0

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

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

that and the description

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

start breakdown

    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 description break, and here I step for 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 is loaded from the bank works properly

        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, if I insert 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();
}

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

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

and the measure receives

ASDF [324] [BRE345] [435] [BRD345] [ARTICLE: 1000 - COLOUR: 01]

  • Can you give an example of what result you want? Type, comment the input, and what result you want to achieve

  • I already did, it would be like ASDF 324 BRE345 435 BRD345 ARTICLE: 1.000 - COLOR: 01 each in an input

1 answer

0

And people who need it

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);
    }
    }

Browser other questions tagged

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