Kadir GÜNEY Kadir GÜNEY - 2 months ago 8
Javascript Question

MVC javascript display selected data

First of all, I list the e-mail from coming ActionResult in the first cycle.
I want to see the details by clicking on the listed data. I open with the help of jQuery details. The problem arises in this section. in this case ,the opening of the details of the first mail in the detail of each row.

There are details of the message in the second loop.To connect to the two loops in a guid font was coming. (MessageId).

id=messageId (guid type)

mailing list

<div class="message-list-container">
<div class="message-list" id="message-list">

@foreach (var item in Model)
{
<div id="@item.MessageId" class="message-item">
<span class="sender" title="@item.From">
@item.From
</span>
<span class="time">@mvcHelper.saatAyarla(item.Date)</span>
@if(item.Attachments.Any())
{
<span class="attachment">
<i class="ace-icon fa fa-paperclip"></i>
</span>
}
<span class="summary">
<span class="text">
@item.Subject
</span>
</span>
</div>
}
</div>
</div>


mailing details

<!--Messsage details-->
@foreach (var item in Model)
{
<!-- <div class="hide message-content" id="id-message-content">-->
<div class="hide message-content" id="@item.MessageId">
<div class="message-header clearfix">
<div class="pull-left">
<span class="blue bigger-125"> @item.Subject </span>
<div class="space-4"></div>
<i class="ace-icon fa fa-star orange2"></i>
&nbsp;
<img class="middle" alt="John's Avatar" src="/Areas/admin/Content/images/avatars/avatar.png" width="32" />
&nbsp;
<a href="#" class="sender">@item.From</a>

&nbsp;
<i class="ace-icon fa fa-clock-o bigger-110 orange middle"></i>
<span class="time grey">@mvcHelper.saatGoster(item.Date)</span>
</div>
</div>
<div class="hr hr-double"></div>
<div class="message-body">
<p>
@item.TextBody
</p>
</div>
<div class="hr hr-double"></div>
<!--Eklenti paneli-->
<div class="message-attachment clearfix">
@if (item.Attachments.Any())
{

<div class="attachment-title">
<span class="blue bolder bigger-110">Eklentiler</span>
&nbsp;
<span class="grey">(@item.Attachments.Count() Dosya)</span>
</div>


<ul class="attachment-list pull-left list-unstyled">
@foreach (var attachment in item.Attachments)
{
<li>
<a href="#" class="attached-file">
<i class="ace-icon fa fa-file-o bigger-110"></i>
<span class="attached-name">@mvcHelper.getAttachmentName(attachment.ToString())</span>
</a>

<span class="action-buttons">
<a href="#">
<i class="ace-icon fa fa-download bigger-125 blue"></i>
</a>

<a href="#">
<i class="ace-icon fa fa-trash-o bigger-125 red"></i>
</a>
</span>
</li>
}
</ul>
}
</div>
</div><!-- /.message-content -->
}
<!--Eklenti paneli Son-->
<!--message details end-->


loop connecting two points.

first foreach = <div id="@item.MessageId" class="message-item">

//Places where the problem is. They need to be connected.

second foreach = <!-- <div class="hide message-content" id="id-message-content">-->
<div class="hide message-content" id="@item.MessageId">

var content = message.find('.message-content:last').html($('#id-message-content').html());


jQuery code

$('.message-list .message-item .text').on('click', function () {
var message = $(this).closest('.message-item');

//if message is open, then close it
if (message.hasClass('message-inline-open')) {
message.removeClass('message-inline-open').find('.message-content').remove();
return;
}

$('.message-container').append('<div class="message-loading-overlay"><i class="fa-spin ace-icon fa fa-spinner orange2 bigger-160"></i></div>');
setTimeout(function () {
$('.message-container').find('.message-loading-overlay').remove();
message
.addClass('message-inline-open')
.append('<div class="message-content" />');
var content = message.find('.message-content:last').html($('#id-message-content').html());

//remove scrollbar elements
content.find('.scroll-track').remove();
content.find('.scroll-content').children().unwrap();

content.find('.message-body').ace_scroll({
size: 150,
mouseWheelLock: true,
styleClass: 'scroll-visible'
});

}, 500 + parseInt(Math.random() * 500));

});

Answer

Your first problem is that you are creating multiple elements with identical id properties. This makes your HTML invalid.

Here is the problem code:

@foreach (var item in Model)
{
    <div id="@item.MessageId" class="message-item">
...

@foreach (var item in Model)
{
   <div class="hide message-content" id="@item.MessageId">
...

For each message in your model, this will create 2 <div> elements whose id has the value of the @item.MessageID variable. The second of these is and illegal element because it has the same ID as an earlier element. You will need to make these <div>s have unique IDs.

The second problem is:

When you run

var content = message.find('.message-content:last').html($('#id-message-content').html());

this part: $('#id-message-content').html()

cannot find anything because there is no element whose id is "id-message-content". Also every time you open the message, you are appending another "message-content" div into the message-item. This is not necessary.

To fix these issues, you can change the code like this:

First loop:

@foreach (var item in Model)
{
    <div data-messageid="@item.MessageId" class="message-item">
...
           <span class="summary">
                <span class="text">
                    @item.Subject
                </span>
           </span>
           <div class="message-content" hidden></div>
...

Second loop:

@foreach (var item in Model)
{
<div class="hide message-content" id="message-content-@item.MessageId">
...

jQuery:

$('.message-list .message-item .text').on('click', function () {
var message = $(this).parents('.message-item');

//if message is open, then close it
if (message.hasClass('message-inline-open')) {
    message.removeClass('message-inline-open').find('.message-content').hide();
    return;
}

$('.message-container').append('<div class="message-loading-overlay"><i class="fa-spin ace-icon fa fa-spinner orange2 bigger-160"></i></div>');
setTimeout(function () {
    $('.message-container').find('.message-loading-overlay').remove();
    message.addClass('message-inline-open');
    var content = message.find(".message-content");
    content.show();
    content.html($('#message-content-' + message.data("messageid")).html());

    //remove scrollbar elements
    content.find('.scroll-track').remove();
    content.find('.scroll-content').children().unwrap();

    content.find('.message-body').ace_scroll({
        size: 150,
        mouseWheelLock: true,
        styleClass: 'scroll-visible'
    });

}, 500 + parseInt(Math.random() * 500));
});
Comments