Comment puis-je afficher un HtmlTableRow précédemment caché via jQuery?

J’ai un HtmlTable avec quatre lignes. Les deux premières lignes (une ligne “en-tête de colonne” et une ligne de données normale) sont toujours affichées. Si l’utilisateur sélectionne un bouton “+”, vous devez en append un autre, puis un autre (quatre lignes maximum).

Les deux lignes “dormantes” sont créées en C # mais sont initialement “masquées” en définissant leur hauteur à 0 (c’est l’intention, de toute façon):

boxIndex2 = new TextBox() { CssClass = "finaff-webform-field-input", Width = TEXTBOX_WIDTH, Height = 0, ID = "boxIndex2foapalrow3" }; cellColIndex2.Controls.Add(boxIndex2); 

Remarque: il ne s’agit que de l’une des six “zones de texte” du fichier HtmlTableRow. tous ont leur hauteur réglée sur 0.

Cela fonctionne partiellement:

entrez la description de l'image ici

Comme vous pouvez le constater, les troisième et quasortingème rangées sont courtes, mais pas invisibles / cachées. En cliquant sur le bouton “+”, ils sont obligés de prendre toute leur hauteur:

entrez la description de l'image ici

… et en sélectionnant à nouveau le bouton “+” “gonfle” la dernière ligne raccourcie:

entrez la description de l'image ici

Le jQuery que j’utilise pour ceci est:

 $(document).on("click", '[id$=btnAddFoapalRow]', function (e) { var textboxHeight = 15; if ($('[id$=boxIndex2foapalrow3]').height() === 0) { $('[id$=boxIndex2foapalrow3').height(textboxHeight); $('[id$=boxFund2foapalrow3').height(textboxHeight); $('[id$=boxOrg2foapalrow3').height(textboxHeight); $('[id$=boxAccount2foapalrow3').height(textboxHeight); $('[id$=boxActivity2foapalrow3').height(textboxHeight); $('[id$=boxAmount2foapalrow3').height(textboxHeight); } else if ($('[id$=boxIndex3foapalrow4]').height() === 0) { $('[id$=boxIndex3foapalrow4').height(textboxHeight); $('[id$=boxFund3foapalrow4').height(textboxHeight); $('[id$=boxOrg3foapalrow4').height(textboxHeight); $('[id$=boxAccount3foapalrow4').height(textboxHeight); $('[id$=boxActivity3foapalrow4').height(textboxHeight); $('[id$=boxAmount3foapalrow4').height(textboxHeight); } }); 

Alors, comment puis-je supprimer ces lignes du tout avant de sélectionner le bouton “Ajouter une ligne” (“+”)?

J’ai essayé tout ce que je pouvais imaginer pour que les deux lignes “démarrent la vie” du composant WebPart invisible, puis les visualisent par programmation.

J’ai essayé:

Définissez leur propriété visible sur false (C # code-behind), puis utilisez divers codes côté client / jQuery pour les afficher. J’ai essayé les méthodes jQuery (JavaScript?) Suivantes:

 .toggle() .show() .slidedown() 

…ainsi:

 /* This is supposed to make the rows visible, but is not yet working... */ $(document).on("click", '[id$=btnAddFoapalRow]', function (e) { if ($('[id$=foapalrow3]').not(":visible")) { alert('reached the foapalrow3 not visible branch'); //$('[id$=foapalrow3]').slideDown(); //$('[id$=foapalrow3]').toggle(); $('[id$=foapalrow3]').show(); } else if ($('[id$=foapalrow4]').not(":visible")) { alert('reached the foapalrow4 not visible branch'); $('[id$=foapalrow4]').slideDown(); } }); 

… mais aucun de ces travaux. Je vois avec le code ci-dessus l’alerte “atteint la twig non visible de foapalrow3”.

Alors, dois-je emprunter un chemin complètement différent, par exemple en rendant ces tables séparées sur une ligne et en les rendant visibles? C’est-à-dire que cliquer sur le bouton “+” rend visible le tableau avec la deuxième ligne de données (troisième ligne globale), cliquer à nouveau sur le bouton pour rendre la troisième ligne de données (quasortingème ligne générale)? Ou comment?

METTRE À JOUR

À la demande / suggestion de jmoreno, voici le code HTML, tel qu’il est visualisé via “View Source”:

  
Index Fund Organization Account Activity Amount
en
Index Fund Organization Account Activity Amount
en
Index Fund Organization Account Activity Amount
en
Index Fund Organization Account Activity Amount
en
Index Fund Organization Account Activity Amount
en
Index Fund Organization Account Activity Amount
en
Index Fund Organization Account Activity Amount
en
Index Fund Organization Account Activity Amount
en
Index Fund Organization Account Activity Amount
en
Index Fund Organization Account Activity Amount
en
Index Fund Organization Account Activity Amount
en
Index Fund Organization Account Activity Amount
en
Index Fund Organization Account Activity Amount

… et voici comment tout cela (la table HtmlTable et ses lignes, cellules et son contenu) est créé dans le C # côté serveur:

 private void GenerateSection5() { HtmlTable tblSection5 = null; var headerLabel = new Label { CssClass = "dplatypus-webform-field-label", Text = "

FOAPAL / Payment Amount Information

" }; this.Controls.Add(headerLabel); AddVerticalSpace(); tblSection5 = GetSection5Table(); if (null != tblSection5) { this.Controls.Add(tblSection5); } AddVerticalSpace(); var totalLabel = new Label { CssClass = "dplatypus-webform-field-label", Text = "Total: " }; this.Controls.Add(totalLabel); boxSection5Total = new TextBox() { CssClass = "dplatypus-webform-field-input" }; this.Controls.Add(boxSection5Total); AddVerticalSpace(); } private HtmlTable GetSection5Table() { const int TEXTBOX_WIDTH = 88; const Ssortingng CELL_WIDTH = "88px"; foapalHTMLTable = new HtmlTable(); foapalHTMLTable.Border = 2; // Create Row 1 var row1 = new HtmlTableRow(); var cellColTitle1 = new HtmlTableCell(); cellColTitle1.Width = CELL_WIDTH; cellColTitle1.Style.Add("text-align", "center"); row1.Cells.Add(cellColTitle1); var cellColTitle2 = new HtmlTableCell(); cellColTitle2.Width = CELL_WIDTH; cellColTitle2.Style.Add("text-align", "center"); row1.Cells.Add(cellColTitle2); var cellColTitle3 = new HtmlTableCell(); cellColTitle3.Width = CELL_WIDTH; cellColTitle3.Style.Add("text-align", "center"); row1.Cells.Add(cellColTitle3); var cellColTitle4 = new HtmlTableCell(); cellColTitle4.Width = CELL_WIDTH; cellColTitle4.Style.Add("text-align", "center"); row1.Cells.Add(cellColTitle4); var cellColTitle5 = new HtmlTableCell(); cellColTitle5.Width = CELL_WIDTH; cellColTitle5.Style.Add("text-align", "center"); row1.Cells.Add(cellColTitle5); var cellColTitle6 = new HtmlTableCell(); cellColTitle6.Width = CELL_WIDTH; cellColTitle6.Style.Add("text-align", "center"); row1.Cells.Add(cellColTitle6); var indexStr = new Label { CssClass = "dplatypus-webform-field-label", Text = "Index" }; cellColTitle1.Controls.Add(indexStr); var fundStr = new Label { CssClass = "dplatypus-webform-field-label", Text = "Fund" }; fundStr.Style.Add("text-align", "center"); cellColTitle2.Controls.Add(fundStr); var orgStr = new Label { CssClass = "dplatypus-webform-field-label", Text = "Organization" }; orgStr.Style.Add("text-align", "center"); cellColTitle3.Controls.Add(orgStr); var accountStr = new Label { CssClass = "dplatypus-webform-field-label", Text = "Account" }; accountStr.Style.Add("text-align", "center"); cellColTitle4.Controls.Add(accountStr); var activityStr = new Label { CssClass = "dplatypus-webform-field-label", Text = "Activity" }; activityStr.Style.Add("text-align", "center"); cellColTitle5.Controls.Add(activityStr); var amountStr = new Label { CssClass = "dplatypus-webform-field-label", Text = "Amount" }; amountStr.Style.Add("text-align", "center"); cellColTitle6.Controls.Add(amountStr); foapalHTMLTable.Rows.Add(row1); btnAddFoapalRow = new HtmlButton(); btnAddFoapalRow.Atsortingbutes["type"] = "button"; btnAddFoapalRow.InnerHtml = "+"; btnAddFoapalRow.ID = "btnAddFoapalRow"; this.Controls.Add(btnAddFoapalRow); // Create row 2 foapalrow2 = new HtmlTableRow(); var cellColIndex1 = new HtmlTableCell(); cellColIndex1.Width = CELL_WIDTH; foapalrow2.Cells.Add(cellColIndex1); var cellColFund1 = new HtmlTableCell(); cellColFund1.Width = CELL_WIDTH; foapalrow2.Cells.Add(cellColFund1); var cellColOrg1 = new HtmlTableCell(); cellColOrg1.Width = CELL_WIDTH; foapalrow2.Cells.Add(cellColOrg1); var cellColAccount1 = new HtmlTableCell(); cellColAccount1.Width = CELL_WIDTH; foapalrow2.Cells.Add(cellColAccount1); var cellColActivity1 = new HtmlTableCell(); cellColActivity1.Width = CELL_WIDTH; foapalrow2.Cells.Add(cellColActivity1); var cellColAmount1 = new HtmlTableCell(); cellColAmount1.Width = CELL_WIDTH; foapalrow2.Cells.Add(cellColAmount1); boxIndex1 = new TextBox() { CssClass = "dplatypus-webform-field-input", Width = TEXTBOX_WIDTH }; cellColIndex1.Controls.Add(boxIndex1); boxFund1 = new TextBox() { CssClass = "dplatypus-webform-field-input", Width = TEXTBOX_WIDTH }; cellColFund1.Controls.Add(boxFund1); boxOrganization1 = new TextBox() { CssClass = "dplatypus-webform-field-input", Width = TEXTBOX_WIDTH }; cellColOrg1.Controls.Add(boxOrganization1); boxAccount1 = new TextBox() { CssClass = "dplatypus-webform-field-input", Width = TEXTBOX_WIDTH }; cellColAccount1.Controls.Add(boxAccount1); boxActivity1 = new TextBox() { CssClass = "dplatypus-webform-field-input", Width = TEXTBOX_WIDTH }; cellColActivity1.Controls.Add(boxActivity1); boxAmount1 = new TextBox() { CssClass = "dplatypus-webform-field-input", Width = TEXTBOX_WIDTH }; cellColAmount1.Controls.Add(boxAmount1); foapalHTMLTable.Rows.Add(foapalrow2); // Row 3 foapalrow3 = new HtmlTableRow(); foapalrow3.ID = "foapalrow3"; var cellColIndex2 = new HtmlTableCell(); cellColIndex2.Width = CELL_WIDTH; foapalrow3.Cells.Add(cellColIndex2); var cellColFund2 = new HtmlTableCell(); cellColFund2.Width = CELL_WIDTH; foapalrow3.Cells.Add(cellColFund2); var cellColOrg2 = new HtmlTableCell(); cellColOrg2.Width = CELL_WIDTH; foapalrow3.Cells.Add(cellColOrg2); var cellColAccount2 = new HtmlTableCell(); cellColAccount2.Width = CELL_WIDTH; foapalrow3.Cells.Add(cellColAccount2); var cellColActivity2 = new HtmlTableCell(); cellColActivity2.Width = CELL_WIDTH; foapalrow3.Cells.Add(cellColActivity2); var cellColAmount2 = new HtmlTableCell(); cellColAmount2.Width = CELL_WIDTH; foapalrow3.Cells.Add(cellColAmount2); boxIndex2 = new TextBox() { CssClass = "dplatypus-webform-field-input", Width = TEXTBOX_WIDTH, Height = 0, ID = "boxIndex2foapalrow3" }; cellColIndex2.Controls.Add(boxIndex2); boxFund2 = new TextBox() { CssClass = "dplatypus-webform-field-input", Width = TEXTBOX_WIDTH, Height = 0, ID = "boxFund2foapalrow3" }; cellColFund2.Controls.Add(boxFund2); boxOrganization2 = new TextBox() { CssClass = "dplatypus-webform-field-input", Width = TEXTBOX_WIDTH, Height = 0, ID = "boxOrg2foapalrow3" }; cellColOrg2.Controls.Add(boxOrganization2); boxAccount2 = new TextBox() { CssClass = "dplatypus-webform-field-input", Width = TEXTBOX_WIDTH, Height = 0, ID = "boxAccount2foapalrow3" }; cellColAccount2.Controls.Add(boxAccount2); boxActivity2 = new TextBox() { CssClass = "dplatypus-webform-field-input", Width = TEXTBOX_WIDTH, Height = 0, //Visible = false, <= this does work ID = "boxActivity2foapalrow3" }; cellColActivity2.Controls.Add(boxActivity2); boxAmount2 = new TextBox() { CssClass = "dplatypus-webform-field-input", Width = TEXTBOX_WIDTH, Height = 0, ID = "boxAmount2foapalrow3" }; cellColAmount2.Controls.Add(boxAmount2); foapalHTMLTable.Rows.Add(foapalrow3); // Row 4 foapalrow4 = new HtmlTableRow(); foapalrow4.ID = "foapalrow4"; var cellColIndex3 = new HtmlTableCell(); cellColIndex3.Width = CELL_WIDTH; foapalrow4.Cells.Add(cellColIndex3); var cellColFund3 = new HtmlTableCell(); cellColFund3.Width = CELL_WIDTH; foapalrow4.Cells.Add(cellColFund3); var cellColOrg3 = new HtmlTableCell(); cellColOrg3.Width = CELL_WIDTH; foapalrow4.Cells.Add(cellColOrg3); var cellColAccount3 = new HtmlTableCell(); cellColAccount3.Width = CELL_WIDTH; foapalrow4.Cells.Add(cellColAccount3); var cellColActivity3 = new HtmlTableCell(); cellColActivity3.Width = CELL_WIDTH; foapalrow4.Cells.Add(cellColActivity3); var cellColAmount3 = new HtmlTableCell(); cellColAmount3.Width = CELL_WIDTH; foapalrow4.Cells.Add(cellColAmount3); boxIndex3 = new TextBox() { CssClass = "dplatypus-webform-field-input", Height = 0, Width = TEXTBOX_WIDTH, ID = "boxIndex3foapalrow4" }; cellColIndex3.Controls.Add(boxIndex3); boxFund3 = new TextBox() { CssClass = "dplatypus-webform-field-input", Width = TEXTBOX_WIDTH, Height = 0, ID = "boxFund3foapalrow4" }; cellColFund3.Controls.Add(boxFund3); boxOrganization3 = new TextBox() { CssClass = "dplatypus-webform-field-input", Width = TEXTBOX_WIDTH, Height = 0, ID = "boxOrg3foapalrow4" }; cellColOrg3.Controls.Add(boxOrganization3); boxAccount3 = new TextBox() { CssClass = "dplatypus-webform-field-input", Width = TEXTBOX_WIDTH, Height = 0, ID = "boxAccount3foapalrow4" }; cellColAccount3.Controls.Add(boxAccount3); boxActivity3 = new TextBox() { CssClass = "dplatypus-webform-field-input", Width = TEXTBOX_WIDTH, Height = 0, ID = "boxActivity3foapalrow4" }; cellColActivity3.Controls.Add(boxActivity3); boxAmount3 = new TextBox() { CssClass = "dplatypus-webform-field-input", Width = TEXTBOX_WIDTH, Height = 0, ID = "boxAmount3foapalrow4" }; cellColAmount3.Controls.Add(boxAmount3); foapalHTMLTable.Rows.Add(foapalrow4); // Add more rows if necessary (up to 6, if that's the limit? Or create a method that will add another one, no matter how many are required) return foapalHTMLTable; }

MISE À JOUR 2

@ Jon P: votre code fonctionne définitivement et de manière démontrable, comme je le vois dans votre exemple exécutable. Cependant, ma mise en œuvre ne fonctionne toujours pas. J’ai ajouté ce code au fichier * .ascx du projet.

CSS

  /* Hide the FOAPAL rows */ .inputTable tr:nth-child(n + 3) {display:none;} /* Standardize input width */ .inputTable input[type="text"] {width: 88px;}  

jQuery

 $(".expander").click(function () { alert("expander click entered - remove this alert after seeing it"); $('.inputTable').find('tr:hidden:first').show(); }); 

Cependant, je ne vois pas l’alerte lorsque je clique sur le bouton, bien que j’aie ajouté cette classe (“expandeur”) au bouton “+” comme ceci:

C #

 btnAddFoapalRow = new HtmlButton(); btnAddFoapalRow.Atsortingbutes["type"] = "button"; btnAddFoapalRow.InnerHtml = "+"; btnAddFoapalRow.ID = "btnAddFoapalRow"; btnAddFoapalRow.Atsortingbutes["class"] = "expander"; // <= This is how to do it, right? 

Donc, j’ai ajouté ceci:

 $(document).on("click", '[id$=btnAddFoapalRow]', function (e) { alert("btnAddFoapalRow click entered - remove this alert after seeing it"); $('.inputTable').find('tr:hidden:first').show(); }); 

… et définissez les deux derniers (rangées 3 et 4) sur invisible comme suit:

 foapalrow3.Visible = false; . . . foapalrow4.Visible = false; 

… mais, bien que je ne commence avec une seule ligne visible maintenant, les lignes supplémentaires ne sont pas développées / ajoutées / visibles lors de la réduction du bouton “+”; dois-je, au lieu de définir visible sur false, définir l’atsortingbut “display” sur “none” ou … ???

NOTE: J’ai configuré la table pour utiliser la classe “inputTable” comme ceci:

 foapalHTMLTable = new HtmlTable(); foapalHTMLTable.Atsortingbutes["class"] = "inputTable"; 

Les deux classes apparaissent dans le code HTML rendu (“View Source”) comme ceci:

 

… donc je ne sais pas quel est le problème. Les zones de texte de la dernière ligne (seconde) supposées être visibles sont ajoutées comme suit:

 boxAmount1 = new TextBox() { CssClass = "dplatypus-webform-field-input", Width = TEXTBOX_WIDTH }; cellColAmount1.Controls.Add(boxAmount1); 

… alors que ceux qui sont censés être “invisibles” sont comme ça (régler la hauteur sur 0, ce qui ne fonctionne que partiellement):

 boxIndex2 = new TextBox() { CssClass = "dplatypus-webform-field-input", Width = TEXTBOX_WIDTH, Height = 0, ID = "boxIndex2foapalrow3" }; cellColIndex2.Controls.Add(boxIndex2); 

Le code HTML rendu pour la table est de ce type:

 

MISE À JOUR 3

Tenter de rectifier les choses de cette façon:

 //foapalrow3.Visible = false; foapalrow3.Atsortingbutes["display"] = "none"; . . . //foapalrow4.Visible = false; foapalrow4.Atsortingbutes["display"] = "none"; 

… ne fonctionne pas non plus (cela me ramène à l’aspect initial de la table – les deux premières lignes sont normales, les deux dernières sont visibles mais “en hauteur”).

MISE À JOUR 4

J’ai même essayé ceci:

(a) Ajoutez un identifiant à la table:

 foapalHTMLTable.ID = "foapalhtmltable"; 

(b) Localisez la table et agissez dessus lorsque le bouton “+” est en purée:

 $(document).on("click", '[id$=btnAddFoapalRow]', function (e) { alert("btnAddFoapalRow click entered - remove this alert after seeing it"); //$('.inputTable').find('tr:hidden:first').show(); $('[id$=foapalhtmltable]').find('tr:hidden:first').show(); }); 

…mais hélas! Même si je cache vraisemblablement les rangées comme ceci:

 foapalrow3.Atsortingbutes["display"] = "none"; foapalrow4.Atsortingbutes["display"] = "none"; 

… (comme noté précédemment), cela ne fonctionne toujours pas. Ai-je besoin de “border-collapse” (voir la réponse de okw )?

MISE À JOUR 5

Je remarque qu’il n’y a pas d ‘”display: none” dans “View Source” pour “foapalrow3” ou “foapalrow4” – pourquoi le Heckle et Jeckle ne le sont-ils pas alors que je le mets dans le code comme suit:

 foapalrow3.Atsortingbutes["display"] = "none"; foapalrow4.Atsortingbutes["display"] = "none"; 

?

Je suppose que toutes les lignes sont censées être identiques, vous pouvez essayer d’utiliser une ligne de modèle dans une table masquée. Pour append une nouvelle ligne, utilisez un sélecteur jQuery pour obtenir la ligne.

 var newRow = $("#my_template_row").clone(); 

Voir Référence de clone jQuery – https://api.jquery.com/clone/

Vous pouvez ensuite append la ligne clonée dans votre tableau visible.

Au lieu de changer la hauteur (ce qui pose des problèmes d’affichage, je le recommande), je vous recommande d’utiliser CSS pour ne pas définir l’affichage. Puis bloquer quand + est cliqué.

Voici un jsFiddle où le clic sur supprime une classe CSS qui cache la ligne.

Mais en gros, il s’agit de

le css étant .hide-row {display: none;} et jsQuery utilisant .hasClass et .removeClass pour supprimer la classe lorsque le bouton est cliqué.

Je tiens également à souligner que vous utilisez trop de styles et d’atsortingbuts en ligne, j’espère que c’est simplement parce que vous avez essayé de régler le problème. J’ai fait une version alternative dans laquelle j’ai utilisé nth-of-type et où les éléments td, span et input sont stylés en fonction d’une classe de la table.

Ok, c’est ce qui a finalement fonctionné pour moi, basé sur le pantalon JQuery / CSS de Jon P:

C #

 foapalHTMLTable = new HtmlTable(); foapalHTMLTable.Border = 2; foapalHTMLTable.ID = "foapalhtmltable"; . . . foapalrow3 = new HtmlTableRow(); foapalrow3.ID = "foapalrow3"; foapalrow3.Style["display"] = "none"; . . . foapalrow4 = new HtmlTableRow(); foapalrow4.ID = "foapalrow4"; foapalrow4.Style["display"] = "none"; 

jQuery

 $(document).on("click", '[id$=btnAddFoapalRow]', function (e) { $('[id$=foapalhtmltable]').find('tr:hidden:first').show(); }); 

Merci également à Saber et Wheatin dans la question sœur de cette question ici .


Index Fund