Increase table without selecting manual id.

Asked

Viewed 141 times

0

good morning, I want to do the following I have a table that hides the part I want but in java script I just hide what I want if I select the id of the part of the table but I want to do next when click on more or less it will decrease or increase without I need to select the id.

<html>

<body>
<script>
		//Manipulando as linhas
		function ocultaRow (rowIndex) {
			var table = document.getElementById('tabela1');
			table.rows[rowIndex].style.display = 'none';
		}

		function mostraRow (rowIndex) {
			var table = document.getElementById('tabela1');
			table.rows[rowIndex].style.display = '';
		}
		
		//Manipulando as colunas
		function ocultaColumn (colIndex) {
			var table = document.getElementById('tabela1');
			for (var r = 0; r < table.rows.length; r++)
				table.rows[r].cells[colIndex].style.display = 'none';
		}
		function mostraColumn (colIndex) {
			var table = document.getElementById('tabela1');
			for (var r = 0; r < table.rows.length; r++)
				table.rows[r].cells[colIndex].style.display = '';
		}
	</SCRIPT>

	<FORM>
		Manipular Linhas
		<SELECT NAME="rowIdx">
		<script>
			for (var i = 1; i <= 4; i++)
				document.write('<OPTION VALUE="' + i + '">' + i);
		</SCRIPT>
		</SELECT>
		<INPUT TYPE="button" VALUE="- " ONCLICK="ocultaRow(this.form.rowIdx.selectedIndex);">
		<INPUT TYPE="button" VALUE="+" ONCLICK="mostraRow(this.form.rowIdx.selectedIndex);">
	</FORM>
	
	<FORM>
		Manipular Colunas
		<SELECT NAME="colIdx">
		<script>
			for (var i = 1; i <= 4; i++)
				document.write('<OPTION VALUE="' + i + '">' + i);
		</SCRIPT>
		</SELECT>
		<INPUT TYPE="button" VALUE="-" ONCLICK="ocultaColumn(this.form.colIdx.selectedIndex);">
		<INPUT TYPE="button" VALUE="+" ONCLICK="mostraColumn(this.form.colIdx.selectedIndex);">
	</FORM>
	
	<table width="100%"  border="1" id="tabela1">
		<tr id="1">
		  <td>Linha1 coluna 1</td>
			<td>Linha2 coluna 2</td>
			<td>Linha3 coluna 3</td>
			<td>Linha4 coluna 4</td>
		</tr>
		<tr id="2">
		  <td>nada2</td>
			<td>LINHA 2</td>
			<td>TESTE 2</td>
			<td>TESTE 2</td>
		</tr>
		<tr id="3">
		  <td>nada3</td>
			<td>LINHA 3</td>
			<td>TESTE 3</td>
			<td>TESTE 3</td>
		</tr>
		<tr id="4">
		  <td>nada4</td>
			<td>LINHA 4</td>
			<td>TESTE 4</td>
			<td>ddd</td>
		</tr>
         <tr>
		  <td >	0</td>
    <td>0</td>
    <td>0</td>
    <td>dd</td>
	  </tr>
	</table>

</body>
</html>

  • What is the difference in functionality between the selects and the + and -? I don’t really understand how they should work, or what they should do differently.

  • i manipulate the column and table row the + to add row or column and the _ to decrease row or column the problem is that i n want to select manual wanted it to do that by clicking the more or less.

  • But what is the relationship between select and these buttons? When you click on + it adds a line (at the end, in the middle or at the place that select indicates?)

  • that even add anywhere in the table but n want it to be automatic 1 by one. but already tried and always error.

  • I just got home and I can take another look here. Would you care to explain once again the functionality of each component? For example: change select must happen X by clicking on + should happen Y. See if it’s clear so I can help/respond.

  • I managed to solve thanks friend. I’m terrible in terms rs

Show 1 more comment

2 answers

1

Let me see if I understand your question, you want to increase and decrease without having to select the table id because you will have more than one table and more than one set of buttons to increase and decrease and so you need a selector that does not search for the ID, is this or more or less this ? If it is, you can group the table and buttons within the same container, and in the function you run when the onclick event occurs you can take the table relative to the element that was clicked, in your example you can change the onclick to pass this parameter, and in its function pick up the table according to the parameter passed using the parentElement attribute and the function getElementByTagName.

function ocultaRow (_self) { 
    var table = self.parentElement.parentElement.getElementsByTagName("table"));
    table.rows[rowIndex].style.display = 'none';
}

...

<INPUT TYPE="button" VALUE="- " ONCLICK="ocultaRow(this);">

But honestly, I think you should write your code, if you don’t want to use jQuery you could at least use addeventlistener for your elements, this makes your code easier to read, maintain, edit as well as save you work.

  • I managed to solve friend was almost that rs refiz the code now got top.

0


var contador=0; 

function incrementarlinha() { 
if(contador==8) 
alert('Maximo permitido alcanzado: 9'); 
else { 
contador++; 
			var table = document.getElementById('tabela1');
			table.rows[contador].style.display = '';
} 
} 
function decrementarlinha() { 
if(contador==0) 
alert('Minimo Permitido: 0'); 
else { 
			var table = document.getElementById('tabela1');
			table.rows[contador --].style.display = 'none';
}
}
// manipular coluna off
 var contadorcoll= 0;
 function incrementarcoluna(){
	 if(contadorcoll==3) 
	 alert('Maximo permitido alcanzado: 3'); 
	 else
	 {
	contadorcoll++;	 
	
	var table = document.getElementById('tabela1');
			for (var r = 0; r < table.rows.length; r++)
				table.rows[r].cells[contadorcoll].style.display = '';
	 }
 }

 function decrementarcoluna(){
if(contadorcoll==0)	
alert('Minimo Permitido: 0'); 
else{
	var contadorcolls = contadorcoll--;
	var table = document.getElementById('tabela1');
			for (var r = 0; r < table.rows.length; r++)
				table.rows[r].cells[contadorcolls].style.display = 'none';
}
 } 
	
	<table width="100%"  border="1" id="tabela1">
		<tr id="1">
		  <td width="30%">Linha1 coluna 1</td>
			<td width="30%" style="display: none;">LINHA 1 coll 2</td>
			<td width="29%" style="display: none;">LINHA 1 coll 3			</td>
			<td width="11%" style="display: none;">LINHA 1 coll 4</td>
		</tr>
		<tr id="2" style="display: none;">
		  <td>nada2</td>
		  <td style="display: none;">LINHA 2 coll 2</td>
			<td style="display: none;">LINHA 2 coll 3</td>
			<td style="display: none;">LINHA 2 coll 4</td>
		</tr>
		<tr id="3" style="display: none;">
		  <td>nada3</td>
		  <td style="display: none;">LINHA 3 coll 2</td>
			<td style="display: none;">LINHA 3 coll 3</td>
			<td style="display: none;">LINHA 3 coll 4</td>
		</tr>
		<tr id="4" style="display: none;" >
		  <td>nada4</td>
		  <td style="display: none;">LINHA 4 coll 2</td>
		<td style="display: none;">LINHA 4 coll 3</td>
			<td style="display: none;">LINHA 4 coll 4</td>
		</tr>
        <tr id="5" style="display: none;" >
		  <td>nada5</td>
		  <td style="display: none;">LINHA 5 coll 2</td>
			<td style="display: none;">LINHA 5 coll 3</td>
			<td style="display: none;">LINHA 5 coll 4</td>
		</tr>
        <tr id="6" style="display: none;" >
		  <td>nada6</td>
		  <td style="display: none;">LINHA 6 coll 2</td>
			<td style="display: none;">LINHA 6 coll 3</td>
			<td style="display: none;">LINHA 6 coll 4</td>
		</tr>
        <tr id="7" style="display: none;" >
		  <td>nada7</td>
		  <td style="display: none;">LINHA 7 coll 2</td>
			<td style="display: none;">LINHA 7 coll 3</td>
			<td style="display: none;">LINHA 7 coll 4</td>
		</tr>
        <tr id="8" style="display: none;" >
		  <td>nada8</td>
		  <td style="display: none;">LINHA 8 coll 2</td>
			<td style="display: none;">LINHA 8 coll 3</td>
			<td style="display: none;">LINHA 8 coll 4</td>
		</tr>
        <tr id="9" style="display: none;" >
		  <td >nada9</td>
		  <td style="display: none;">LINHA 9 coll 2</td>
			<td style="display: none;">LINHA 9 coll 3</td>
			<td style="display: none;">LINHA 9 coll 4</td>
		</tr>
         <tr>
		  <td >	0</td>
   <td style="display: none;">LINHA 4</td>
   <td style="display: none;">nada4 linha nada 4</td>
			<td style="display: none;">nada4 linha nada 4</td>
	  </tr>
	</table>

<form name="caca"> 
<input type="button" onClick="decrementarlinha()" value="diminuirlinha"> 
<input type="button" onClick="incrementarlinha()" value="aumentarlinha"> 
<input type="button" onClick="decrementarcoluna()" value="diminuircoluna"> 
<input type="button" onClick="incrementarcoluna()" value="aumentarcoluna"> 

</form>  
   

Browser other questions tagged

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