Function is not removing the data!

-1

//INSERIR
function move_patr_seri(Origem, Destino)
{
  var w_Cont_Qtde = 0;
  var w_Qtde_Peri = 4;
  var v_patr = new Array();

	var w_valor = Origem.value;
	var w_tipo;
    w_tipo = "S";
      

	if(Origem.name == "tx_patr")
	    {
		w_tipo = "P";
	    }
	    
	if (w_Cont_Qtde <=  w_Qtde_Peri - 1)
       {
		if ((v_patr.indexOf(w_tipo+w_valor) == -1) && (w_valor != ""))
		{
			var opt = document.createElement("option"); 
			opt.text = w_valor ;
			opt.value = w_valor ;
			Destino.options.add(opt);
			//Cria o Vetor
			v_patr[w_Cont_Qtde] = w_tipo + w_valor;				
			w_Cont_Qtde = w_Cont_Qtde + 1;
			if (Origem.name == "tx_patr"){ document.forms['sai_frm_incl_patr_seri'].tx_patr.focus();}
			else { document.forms['sai_frm_incl_patr_seri'].tx_seri.focus();	}		
			return true;
		}
		else
		{
		    alert("Patrimônio OU Serial já existe OU não é válido!");
			return true;
		}
	  }

	else
	if(w_ver == 1){
		alert("Quantidade atingida!");
		if(confirm("Deseja inserir a mesma quantidade para ambos?") == true)
		{
			w_cont = w_Qtde_Peri;
			w_ver = 0;
			w_Qtde_Peri = w_Qtde_Peri + w_Qtde_Peri;
			return true;
		}
	}
	else
		alert("Quantidade informada ja Incluida !!!");
   return true; 
}
function tira(Destino)
{
	var w_letra;
	var w_tira;
	w_letra = "S";
	if (Destino.name == "cb_Patr"){
			w_letra = "P";		
		}
   
	var i;
	for(i = 0; i < Destino.options.length; i++)
	{ 
		if (Destino.options[i].selected && Destino.options[i].value != "")
		{
			w_tira = w_letra+Destino.options[i].value;
			
			v_patr.splice(v_patr.indexOf(w_tira), 1);		
			w_Cont_Qtde = w_Cont_Qtde - 1;						
			Destino.remove(Destino.selectedIndex);
		}
	}
}
<form name="sai_frm_incl_patr_seri" method="post" >
	<body>	
      <table>
	       <tr>
		<td>   
					<font face="arial" align="center" valign="middle" color="blue" size="-1">PATRIMÔNIO</font><br>
						<input type="text" name="tx_patr" id="id_patr" maxlength="12" size="12" style="font-size:11; color:Black;" onkeypress="return SomenteNumero(event);" onkeyup="Mascara(this,Patri);" value="">
						<input type="button" onClick="move_patr_seri(this.form.tx_patr,this.form.cb_Patr);limpa_patr();" value=">>">
					<br>
					<select multiple size="7" name="cb_Patr" style="width:300">
				   </select>					
					<br>
						<input type="button" align="center" valign="middle" onClick="tira(this.form.cb_Patr)" value="<<">
					<br>
				</td>					
			</tr>
		</table><br>


	</body>	
</form>			

I have a snippet of code that the user inserts data into a box and can remove if so desired. So that was the inclusion part. Now I'm in the part of "change" which basically is the same code, where I just populate the box with the data that the user typed and he sees if he wants to remove or insert more data. But, the function is not extracting the data it is showing. Code:

HTML

    <td>   
        <font face="arial" align="center" valign="middle" color="blue" size="-1">PATRIMÔNIO</font><br>
            <input type="text" name="tx_patr" id="id_patr" maxlength="12" size="12" style="font-size:11; color:Black;" onkeypress="return SomenteNumero(event);" onkeyup="Mascara(this,Patri);" value="">
            <input type="button" onClick="move_patr_seri(this.form.tx_patr,this.form.cb_Patr);limpa_patr();" value=">>">
        <br>
            <select multiple size="7" name="cb_Patr" style="width:300">
                <?
                $w_querybusca="$w_select;";    
                $w_queryresultado=f_class_conecta_bd($w_querybusca);    

                while($w_registro = pg_fetch_object($w_queryresultado))
                {
                    print('<option value="'.$w_registro->tx_num_patr.'">'.trim($w_registro->tx_num_patr).'</option>'."\n");
                }
                ?>
           </select>                    
        <br>
            <input type="button" align="center" valign="middle" onClick="tira(this.form.cb_Patr)" value="<<">
        <br>
    </td>

In this section I have the box and the buttons to include and remove in the box!

JS

//RETIRARfunctiontira(Destino){varw_letra;w_letra="S";
    if (Destino.name == "cb_Patr"){
            w_letra = "P";      
        }

    var i;
    for(i = 0; i < Destino.options.length; i++)
    { 
        if (Destino.options[i].selected && Destino.options[i].value != "")
        {
            w_tira = w_letra+Destino.options[i].value;
            v_patr.splice(v_patr.indexOf(w_tira), 1);       
            w_Cont_Qtde = w_Cont_Qtde - 1;                      
            Destino.remove(Destino.selectedIndex.value);
        }
    }
}

This is the função which removes the data from the box , where v_patr contains the data that it reported on the inclusion and the w_letra is appended in front of the value to remove it from v_patr as well (because it is added in the letters before).

The problem is in v_patr.splice(v_patr.indexOf(w_tira), 1); , where I am adding the data in it like this:

<?php
$w_select ="SELECT *    FROM public.sai_cad_patr_seri WHERE 
                                        sai_cad_patr_seri.fk_seq_cara_peri = '$arr_w_param[17]'";
$w_querybusc = "$w_select;"; 
$w_queryresult=f_class_conecta_bd($w_querybusc);
$index = 0;
$patr = array();
$seri = array();        
while($w_registro = pg_fetch_object($w_queryresult))
{
    $patr[$index] = "P".trim($w_registro->tx_num_patr);
    $seri[$index] = "S".trim($w_registro->tx_num_seri);
    $index++;
}
$string_patr = implode(",", $patr);
$string_seri = implode(",", $seri);
    print("<SCRIPT language=javascript> 

            v_patr = new Array (2); 

            v_patr = string_patr+','+ string_seri;

        </SCRIPT>");
?>
    
asked by anonymous 14.10.2014 / 14:25

1 answer

1

The problem is that you are declaring your variables within the function:

function move_patr_seri(Origem, Destino)
{
  var w_Cont_Qtde = 0;
  var w_Qtde_Peri = 4;
  var v_patr = new Array();

  var w_valor = Origem.value;
  var w_tipo;

So every time the function is called, it's like cleaning up all the items, and at the time of removing there were no more.

To resolve, instantiate your variables at the beginning of the script.

<script>
   var v_patr = [];
   var w_Cont_Qtde = 0;
   var w_Qtde_Peri = 4;

   function move_patr_seri(Origem, Destino)
   {
      var w_valor = Origem.value;
      var w_tipo;
      w_tipo = (Origem.name == "tx_patr" ? "P" : "S") ;

Results

var Patri = '';
var v_patr = [];
var w_Cont_Qtde = 0;
var w_Qtde_Peri = 4;

// FIX
function SomenteNumero(){ return true;}
function limpa_patr(){return true;}
function Mascara(el, patri){return true;}

//INSERIR
function move_patr_seri(Origem, Destino) {

   var w_valor = Origem.value;
   var w_tipo;
    w_tipo = "S";
      

   if(Origem.name == "tx_patr")
       {
      w_tipo = "P";
       }
       
   if (w_Cont_Qtde <=  w_Qtde_Peri - 1)
       {
      if ((v_patr.indexOf(w_tipo+w_valor) == -1) && (w_valor != ""))
      {
         var opt = document.createElement("option"); 
         opt.text = w_valor ;
         opt.value = w_valor ;
         Destino.options.add(opt);
         //Cria o Vetor
         v_patr[w_Cont_Qtde] = w_tipo + w_valor;            
         w_Cont_Qtde = w_Cont_Qtde + 1;
         if (Origem.name == "tx_patr"){ document.forms['sai_frm_incl_patr_seri'].tx_patr.focus();}
         else { document.forms['sai_frm_incl_patr_seri'].tx_seri.focus();  }     
         Origem.value = '';
         return true;
      }
      else
      {
          alert("Patrimônio OU Serial já existe OU não é válido!");
          Origem.value = '';
         return true;
      }
     }

   else
   if(w_ver == 1){
      alert("Quantidade atingida!");
      if(confirm("Deseja inserir a mesma quantidade para ambos?") == true)
      {
         w_cont = w_Qtde_Peri;
         w_ver = 0;
         w_Qtde_Peri = w_Qtde_Peri + w_Qtde_Peri;
         Origem.value = '';
         return true;
      }
   }
   else
      alert("Quantidade informada ja Incluida !!!");
   return true; 
}

function tira(Destino) {
   var w_letra;
   var w_tira;
   w_letra = "S";
   if (Destino.name == "cb_Patr"){
         w_letra = "P";    
      }
   
   var i;
   for(i = 0; i < Destino.options.length; i++)
   { 
      if (Destino.options[i].selected && Destino.options[i].value != "")
      {
         w_tira = w_letra+Destino.options[i].value;
         
         v_patr.splice(v_patr.indexOf(w_tira), 1);    
         w_Cont_Qtde = w_Cont_Qtde - 1;                  
         Destino.remove(Destino.selectedIndex);
      }
   }
}
<form name="sai_frm_incl_patr_seri" method="post">
   <table>
      <tr>
         <td>
            <font face="arial" align="center" valign="middle" color="blue" size="-1">PATRIMÔNIO</font>
            <br>
            <input type="text" name="tx_patr" id="id_patr" maxlength="12" size="12" style="font-size:11; color:Black;" onkeypress="return SomenteNumero(event);" onkeyup="Mascara(this,Patri);" value="">
            <input type="button" onClick="move_patr_seri(this.form.tx_patr,this.form.cb_Patr);limpa_patr();" value=">>">
            <br>
            <select multiple size="7" name="cb_Patr" style="width:300">
            </select>
            <br>
            <input type="button" align="center" valign="middle" onClick="tira(this.form.cb_Patr)" value="<<">
            <br>
         </td>
      </tr>
   </table>
</form>
    
14.10.2014 / 17:02