Add an extra div below each table row using JQuery

I have an MVC 4 application where one of my views looks something like this:

enter image description here

and the corresponding code for this view:

<tbody>
   @foreach (var item in Model)
      {
        <tr id="document-row-@item.DocumentId">
           <td>                        
               @Html.DisplayFor(modelItem => item.DocumentName)
                 <div id="document-detail-contentsREVW">

                 </div>
           </td>

           <td>
               @Html.DisplayFor(modelItem => item.CreatedDate)
                  <div class="pull-right"> 
                           @Ajax.ActionLink("Details", "DocumentDetail", "Documents", new { Area = "SmartDocs", id = item.DocumentId.ToString() , currentStatus = item.CurrentStatus.ToString()}, new AjaxOptions
                        {
                           InsertionMode = InsertionMode.Replace,
                           HttpMethod = "GET",
                           LoadingElementId = "ajax-loader",
                           UpdateTargetId = "document-detail-contentsREVW",
                        }, new { @class = "details-link" })

                 </div>
            </td>
        </tr>
     }

      

Now when I click on the Details link, one controller action method is called and the details for that record are only shown below the first row in a tabular format inside document-detail-contentsREVW as used in my opinion and can be thought of as:

enter image description here

which is the details for the first entry. Once again, if I click on the details for the third or any other entry, the position for showing the detail is the same as shown in the following image: enter image description here

Here I want the details to appear just below the specific post for which the link was clicked. The functionality works fine. Also on clicking the details I can toggle (hide / unhide) the details of the div.

I feel like the position for using my div inside the tr is wrong and hence every time the same place is updated when verbosely clicked.

I have searched this a lot and even found many scripts to achieve this. For example: http://jsfiddle.net/ME3kG/30/ (I want something like this)

But here the details are pre-recorded and used at the click of an icon. But in my case, I will get the details dynamically after clicking the details link for this entry. I think I understand my requirement. So can this be done with jquery or how can I achieve this? .. Thanks in advance.

Below is my script:

   $(function () {
        $('.details-link').on('click', function (e) {
            e.preventDefault();
            $(this).closest('tr').find('.document-details').toggle();
            $(this).text(function (i, v) {
                return v === 'Details' ? 'Hide' : 'Details';
            });
        });
    });

      

+3


source to share


1 answer


The problem is that you gave all of your elements the same ID. Besides invalid html, the method @Ajax.ActionLink()

updates the first element with id=document-detail-contentsREVW

(not necessarily on the appropriate line). You need to create a unique identifier in a loop. If your model IList

then you can use (for example)

@for (int i = 0; i < Model.Count; i++)
{
  var id = string.Format("document-detail-{0}", i);
  <tr id="document-row-@item.DocumentId">
    @Html.DisplayFor(m => m[i].DocumentName)
    <div id=@id></div> // id will be "document-detail-0", "document-detail-1" etc
    ....
    @Ajax.ActionLink("Details",.....
    {
      ....
      UpdateTargetId = @id,

      



If you are simulating IEnumerable

, declare the counter before the loop and increment the counter in the statementforeach

@{int i = 0;} // declare counter
@foreach (var item in Model)
{
  var id = string.Format("document-detail-{0}", i++);
  <tr id="document-row-@item.DocumentId">
    @Html.DisplayFor(m => item.DocumentName) // same as before
    <div id=@id></div>
    ....
    @Ajax.ActionLink("Details",.....
      ....
      UpdateTargetId = @id,

      

+4


source







All Articles