Récupérer données d'un tableau
salut à tous,
je voudrais récupérer les données qu'il dans mon tableau dans le formulaire ou j'ai saisi les données, pour pourvoir consulter dans un premier temps et modifier.
voici mon code:
test.html
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
|
<table align ="center" name= "tableau" id="tableau" border="1">
<tr>
<td>Numéro de ligne</td>
<td>Nom</td>
<td>Prénom</td>
<td>Organisation</td>
<td>telephone</td>
<td>Supprimer la ligne</td>
</tr>
</table>
<table align="center">
<form name="formulaireAgent">
<tr>
<td>
Nom :
</td>
<td>
<input id="nom" type="text" name="nom" style="width:200px;"></input>
</td>
</tr>
<tr>
<td>
Prénom :
</td>
<td>
<input id="prenom" type="text" name="titre" style="width:200px;"></input>
</td>
</tr>
<tr>
<td>
Organisation :
</td>
<td>
<input id="organisation" type="text" name="organisation" style="width:200px;"></input>
</td>
</tr>
<tr>
<td>
Téléphone :
</td>
<td>
<input id="telephone" type="text" name="categorie" style="width:200px;"></input>
</td>
</tr>
</table>
</form>
<table align="center">
<input type="submit" value='Ajouter' style="padding: 7px 4px 5px 5px; margin: 30px 0 0 11px" onclick="ajouterAgent()"></input> </table>
<table>
</table>
</body>
</html> |
test.js
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
|
function ajouterOrganisation()
{
//num = 0;
var Cell;
var nom = document.forms["formulaire"].nom.value;
var horaire = document.forms["formulaire"].horaire.value;
var fonction = document.forms["formulaire"].fonction.value;
var telephone = document.forms["formulaire"].telephone.value;
var position = document.forms["formulaire"].position.value;
var tableau = document.getElementById('tableau');
var ligne = tableau.insertRow(-1);
Cell = ligne.insertCell(0);
//Cell.innerHTML = num + 1;
Cell.innerHTML = ligne.rowIndex;
Cell = ligne.insertCell(1);
Cell.innerHTML = nom;
Cell = ligne.insertCell(2);
Cell.innerHTML = horaire;
Cell = ligne.insertCell(3);
Cell.innerHTML = fonction;
Cell = ligne.insertCell(4);
Cell.innerHTML = telephone;
Cell = ligne.insertCell(5);
Cell.innerHTML = position;
Cell = ligne.insertCell(6);
//Cell.innerHTML = ("<input type=button name=supprimer value=Supprimer onclick=suppression()>");
var bouton = document.createElement("input");
bouton.type = "button";
bouton.value = "Supprimer";
bouton.onclick = function(){suppression(ligne)};
Cell.appendChild(bouton);
document.forms["formulaire"].nom.value = "";
document.forms["formulaire"].horaire.value = "";
document.forms["formulaire"].fonction.value = "";
document.forms["formulaire"].telephone.value = "";
document.forms["formulaire"].position.value = "";
}
function ajouterAgent()
{
//num = 0;
var Cell;
var nom = document.forms["formulaireAgent"].nom.value;
var prenom = document.forms["formulaireAgent"].prenom.value;
var organisation = document.forms["formulaireAgent"].organisation.value;
var telephone = document.forms["formulaireAgent"].telephone.value;
var tableau = document.getElementById('tableau');
var ligne = tableau.insertRow(-1);
Cell = ligne.insertCell(0);
//Cell.innerHTML = num + 1;
Cell.innerHTML = ligne.rowIndex;
Cell = ligne.insertCell(1);
Cell.innerHTML = nom;
Cell = ligne.insertCell(2);
Cell.innerHTML = prenom;
Cell = ligne.insertCell(3);
Cell.innerHTML = organisation;
Cell = ligne.insertCell(4);
Cell.innerHTML = telephone;
Cell = ligne.insertCell(5);
Cell.innerHTML = ("<input name='conf' type='hidden' value='0'>")
Cell.innerHTML = ("<input name='supprimer' type='submit' value ='Supprimer' onclick=suppression(ligne)")
//Cell.innerHTML = ("<input type=button name=supprimer value=Supprimer onclick=suppression()>");
var bouton = document.createElement("input");
bouton.type = "button";
bouton.value = "Supprimer";
bouton.onclick = function(){suppression(ligne)};
Cell.appendChild(bouton);
document.forms["formulaireAgent"].nom.value = "";
document.forms["formulaireAgent"].prenom.value = "";
document.forms["formulaireAgent"].organisation.value = "";
document.forms["formulaireAgent"].telephone.value = "";
}
function suppression(ligne)
{
//var nb = document.getElementById('tableau').rows.length;
//document.getElementById('tableau').deleteRow(-1);
if(confirm("Êtes-vous sûr de vouloir supprimer ?"))
document.getElementById('tableau').deleteRow(ligne.rowIndex);
else
return;
//Recomptage des lignes...
var tableau = document.getElementById('tableau');
var trs = tableau.rows;
var n = trs.length;
var i;
for (i=1; i<n; i++) //on commence à 1 et non à 0 ;)
{
trs[i].cells[0].innerHTML = trs[i].rowIndex;
}
} |
merci d'avance