2

I have a listing table displaying some information from the database in an asp.net mvc website.

What i am trying to do is to check if the item status is 1 and if comments exists to display a little icon where the user will click on it and see the specific comment for that item.

So in the listing table it seems to display fine that icon according to the above criteria but when i click on a specific listing it opens all dialogs with comments for all other listings with the same item status instead of the one i have chosen.

Can you please help me on what am i doing wrong ?

    <table id="listTable" style="width:100%;" >
    <tr>
        <th style="text-align:center">
            #
        </th>          
        <th style="padding-left:5px;">
           Item Name
        </th>

        <th>
            Start Date       
        </th>
          <th>
            End Date       
        </th>
         <th>
            Request Date       
        </th>
         <th>
            Request Status      
        </th>
    </tr>

@foreach (var item in Model) {
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.BookingId)
        </td>
        <td>
            <a href="@Url.Action("ItemDetails", new {id=item.ItemId })" title="@item.Item.ItemDescription">
            @Html.DisplayFor(modelItem => item.Item.ItemName)
                </a>
        </td>
          <td>
            @Html.DisplayFor(modelItem => item.StartDate)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.EndDate)
        </td>
         <td>
            @Html.DisplayFor(modelItem => item.RequestDate)
        </td>
        @if (item.StatusCodeId == 1)
        {
         <td style="color:#DD7500">

            @Html.DisplayFor(modelItem => item.StatusCode.StatusCodeName)

             @if (item.Comments != null)
             {                                 
              <img class="orangeclicker" style="margin-left:3px;display:inline;margin-bottom:-3px;cursor: pointer;" title="Tutor Comments" src="~/Images/chat_icon.gif" /> 

                      <div class="orangedialog" title="Tutor Comments"> 
                     <p> @item.Comments</p>
                     </div>                          
             }  

        </td>
        }
        else if (item.StatusCodeId == 2)
        {
            <td style="color:darkgreen">

            @Html.DisplayFor(modelItem => item.StatusCode.StatusCodeName)

                 @if (item.Comments != null)
             {
                  <img id="greenclicker" style="margin-left:3px;display:inline;margin-bottom:-3px;cursor: pointer;" title="Tutor Comments" src="~/Images/chat_icon.gif" /> 
                     <div id="greendialog" title="Tutor Comments"> 
                     <p>@item.Comments</p>
                     </div>
             }     
        </td> 
        }
        else if (item.StatusCodeId == 3)
        {
          <td style="color:red">

            @Html.DisplayFor(modelItem => item.StatusCode.StatusCodeName)

                   @if (item.Comments != null)
                   {
                 <img id="redclicker" style="margin-left:3px;display:inline;margin-bottom:-3px;cursor: pointer;" title="Tutor Comments" src="~/Images/chat_icon.gif" />
                       <div id="reddialog" title="Tutor Comments"> 
                     <p>@item.Comments</p>
                     </div>
                   }         
        </td>   
        }
        else if (item.StatusCodeId == 4)
        {
             <td style="color:black">

            @Html.DisplayFor(modelItem => item.StatusCode.StatusCodeName)

                      @if (item.Comments != null)
                      {
                 <img id="blackclicker" style="margin-left:3px;display:inline;margin-bottom:-3px;cursor: pointer;" title="Tutor Comments" src="~/Images/chat_icon.gif" />
                          <div id="blackdialog" title="Tutor Comments"> 
                     <p>@item.Comments</p>
                     </div>
                      }     
        </td>  
        }


    </tr>   
}

</table>



<script>
    $(function ()
  {

        $('.orangedialog, #greendialog, #reddialog, #blackdialog').dialog({
          autoOpen: false,
          show: { effect: "blind", duration: 1000 },
          hide: { effect: "explode", duration: 1000 },
          buttons: {
              Close: function () {
                  $(this).dialog("close");
              }
          }
      });
      $("#greenclicker").live("click", function () {
          $("#greendialog").dialog("open");
      });

      $('.orangeclicker').live("click", function () {
          $(".orangedialog").dialog("open");
      });

      $("#redclicker").live("click", function () {
          $("#reddialog").dialog("open");
      });
      $("#blackclicker").live("click", function () {
          $("#blackdialog").dialog("open");
        });

  });

</script>

html:

<table id="listTable" style="width:100%;" >
    <tr>
        <th style="text-align:center">
            #
        </th>          
        <th style="padding-left:5px;">
           Item Name
        </th>

        <th>
            Start Date       
        </th>
          <th>
            End Date       
        </th>
         <th>
            Request Date       
        </th>
         <th>
            Request Status      
        </th>
    </tr>

    <tr>
        <td>
            192
        </td>
        <td>
            <a href="/Home/ItemDetails/42" title="this is a lab computer">
            Lab Computer 1
                </a>
        </td>
          <td>
            08/04/2014 09:00:00
        </td>
        <td>
            09/04/2014 09:00:00
        </td>
         <td>
            26/03/2014
        </td>
         <td style="color:#DD7500">

            In Progress

              <img class="orangeclicker" style="margin-left:3px;display:inline;margin-bottom:-3px;cursor: pointer;" title="Tutor Comments" src="/Images/chat_icon.gif" /> 
                      <div class="orangedialog" title="Tutor Comments"> 
                     <p> testtttttttttttttt</p>
                     </div>                          

        </td>


    </tr>   
    <tr>
        <td>
            191
        </td>
        <td>
            <a href="/Home/ItemDetails/42" title="this is a lab computer">
            Lab Computer 1
                </a>
        </td>
          <td>
            01/04/2014 17:03:00
        </td>
        <td>
            02/04/2014 09:05:00
        </td>
         <td>
            26/03/2014
        </td>
         <td style="color:#DD7500">

            In Progress

              <img class="orangeclicker" style="margin-left:3px;display:inline;margin-bottom:-3px;cursor: pointer;" title="Tutor Comments" src="/Images/chat_icon.gif" /> 
                      <div class="orangedialog" title="Tutor Comments"> 
                     <p> You won&#39;t get such a fast computer !!!</p>
                     </div>                          

        </td>


    </tr>   
    <tr>
        <td>
            190
        </td>
        <td>
            <a href="/Home/ItemDetails/44" title="3 ghz / 6gm ram / 1tb drive">
            Ibm Laptop
                </a>
        </td>
          <td>
            10/04/2014 09:00:00
        </td>
        <td>
            11/04/2014 09:00:00
        </td>
         <td>
            19/03/2014
        </td>
3
  • could you post the html equivalent of the above code Commented Apr 8, 2014 at 14:26
  • hi , i have posted the html Commented Apr 8, 2014 at 14:30
  • I'm assuming this is just a loop so there are a bunch of cells, some of which have comments associated with them. The ones with the comments he wants to be able to click and open the comment associated with them. I'm guessing .orangedialog is probably display:none so it only appears when the dialog plugin is fired. Commented Apr 8, 2014 at 14:31

2 Answers 2

1

Try the code below. I don't think you can use siblings() because all of the .orangedialog classes will be siblings.

HTML

  <img class="orangeclicker" commentid="@item.bookingid" style="margin-left:3px;display:inline;margin-bottom:-3px;cursor: pointer;" title="Tutor Comments" src="~/Images/chat_icon.gif" /> 

                      <div class="orangedialog @item.bookingid" title="Tutor Comments"> 
                     <p> @item.Comments</p>
                     </div>   

Javascript

 $('.orangeclicker').live("click", function () {
     $('.'+$(this).attr('commentid')).dialog("open");           
 }); 

Update: The dialog plugin changes the dom around. We added a custom id as shown above.

Sign up to request clarification or add additional context in comments.

7 Comments

it doesnt fire at all now :(
what i get in the aler is : underfined
@Wizeman1986 Undefined? Hmm, can you post what your code looks like?
i have posted the full code , i have been talking about "orangeclicker" as i have double entries for this at the moment but the issue is general at the rest as well
@Wizeman1986 What's the js code that's giving an alert of undefined? If there are two elements next to each other, using $(this).next().children().dialog("open"); like this or alerting the html should select the correct element, which would be the siblings child....or nephew/niece? :)
|
0

try this way

  <script>  $(function ()
  {    
   $(" .orangedialog").dialog({
      autoOpen: false,
      show: { effect: "blind", duration: 1000 },
      hide: { effect: "explode", duration: 1000 },
      buttons: {
          Close: function () {
              $(this).dialog("close");
          }
      }
  });

   $('.orangeclicker').on("click", function () {
     $(this).parent().find(".orangedialog").dialog("open");    //this will open only its respective comments dialog     
   });         
});

Note:Always be updated with the latest jQuery lib, .live() is deprecated and as of jQuery 1.7, use .on

Happy Coding :)

8 Comments

It's not a child, though, it's a sibling. The code is hard to see but the div isn't a child of the img.
Thank you for your time but the on click is not firing :(
@Wizeman1986: it depends on what version of jquery lib your using. also using .on is ideal way to associate delegated events since the html elements are getting created dynamically
@Aarmora: well that was difficult to figure out in asp code, thats the reason i requested for html dom structure. anyway code is modified to suit the problem
i am using jquery 1.8.2
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.