views:

69

answers:

2

Hello,

I'm having an issue where in my controller, I'm setting values in a collection and storing them in ViewData. eg:

ViewData["ex1"] = new SelectList(ex1); // a simple collection
ViewData["ex2"] = new SelectList(group.Members, "Id", "Gender");

I'm passing these to my View and looping through like this. eg:

<div id="divListBox" style="overflow:auto; border:1px solid #CCCCCC; padding-left:5px; background-color:white; height: 130px"> 

          <%
            var list = this.ViewData["e2x"] as SelectList;
            var pList = this.ViewData["ex1"] as SelectList;

            foreach (var p in list)
            {
                foreach (var pil in pList)
                  {
                    if(pil.Text.Contains(p.Value)) // ex1 does contains ex2
                    {
                       %>
                        <input id="cbPerson" type="checkbox" value="<%= p.Value %>" />
                        <label for="cbPerson"><%= p.Text %></label>
                        <input id="cbPersonInfo" type="hidden" value="<%= pil.Text %>" /><br />  
                       <%
                    }
                  }
            }
          %>
        </div> ...

and here is my jQuery. eg:

$('#divListBox > input').each(function() {
        var personInfo = $('#cbPersonInfo').val();
        $(this).append($('personInfo'));
            $('*').qtip('hide');
            $('#divListBox label').each(function() {
                $(this).qtip({
                    content: personInfo,
                    show: 'mouseover',
                    hide: 'mouseout',
                    style: {
                        classes: 'ui-tooltip-custom',
                        tip: true
                    },
                    position: {
                        my: 'left bottom',
                        at: 'top right'
                    }
                });
            });
        });

if I set my input type of "hidden" to "text", I see the correct information for each one. Hoever, when i hover over them, the first information shows as tooltip for all of them. I think it may be my jquery but I'm not too sure. I've been dealing with this issue for hours now and still nothing. Can anyone please help?

A: 

For one your outer c# loop 'foreach (var pil in pList){' will recreate the same '" />
' element possibly several times.

The javascript var personInfo = $('#cbPersonInfo').val(); selects (and re-selects the same element repeatedly, thus the same tooltip for all the hovers, in this case pil.Text

If more than one element has been assigned the same ID, queries that use that ID will only select the first matched element in the DOM. (http://is.gd/dM4EG)

Ensure that that all html elements in your page have unique IDs.

Edit: Quick link for solution (http://craigsworks.com/projects/qtip_new/forum/take-tooltip-content-from-the-element-attributes-t-8.html) . Will update possible solution later today.

<% foreach (var qt in Model)
{%>
    <input id="cbPerson" type="checkbox" value="<%= qt.Value %>" />
    <label for="cbPerson" qTip="<%=qt.Text  %>"><%= qt.Text %></label>
    <input id="cbPersonInfo" type="hidden" value="<%= qt.Text %>" /><br />
<%} %>

<script type="text/javascript">
    $(document).ready(function () {
        $('[qTip]').each(function () { // Find all elements that have the qTip attribute (labels)
            var personInfo = $(this).attr('qTip');
            $(this).qtip({
                content: personInfo ,
                show: 'mouseover',
                hide: 'mouseout',
                position: { target: 'mouse' }
            }); 
        });
    });
</script>
Ahmad
so how would i fix my code to get it working?
hersh
@hersh - can you clarify on what elements you want qtip to work? Or what you expect the end result to be?
Ahmad
I believe it's just my jquery that's giving me the issue. I've tried switching my loop and again, same result, meaning correct information shown for each checkbox + label. What I want qtip to do is show the description for each label. here's an example of what ff renders for me in fb. "Listbox input -- checkbox" ... "label -- when i hover over this, should show description" ... "input -- hidden type which have the description". again, the first description associated with the first checkbox/label shows for all of them.
hersh
+2  A: 

foreach:

id="cbPerson" type="checkbox" value="<%= p.Value %>" />
<label for="cbPerson"><%= p.Text %></label>
id="cbPersonInfo" type="hidden" value="<%= pil.Text %>" /><br />

means you have several times the same ID (ID is ALWAYS ! unique), so when you do a jquery selec on ID, Jquery selects the first one it finds

if you wrap your person in a container it is even easier.

I made a quick adjustment for you which I tested and worked

  %>
    <div class="person">
     <input id="cbPerson" type="checkbox" value="<%= p.Value %>" />
     <label for="cbPerson"><%= p.Text %></label>
     <input id="cbPersonInfo" type="hidden" value="<%= pil.Text %>" /><br />  
    </div>
    <%

<script type="text/javascript">
// for each container with class person
    $('.person').each(function () {
    //find the input with class cbPersonInfo which is !!!! IN !!!! $(this): $(this) is now the container 
        var personInfo = $(this).find(".cbPersonInfo").val();
        $(this).qtip({
            content: personInfo,
            show: 'mouseover',
            hide: 'mouseout',
            style: {
                classes: 'ui-tooltip-custom',
                tip: true
            },
            position: {
                my: 'left bottom',
                at: 'top right'
            }
        });
    });


</script>

This code means: for each div with class person, find the div inside with class cbPeronInfo and use it's value for the qtip. (and offcourse hook the qtip to that class)


@Edit

actually, for semantic reasons it is better to use a UL instead of a (more logic) but I assume you can figure out how to change that yourself? if you wanted to otherwise give me a sign :)

Nealv
Thank Nealv, but now the tooltip is way far. Is there a way to just hover over the label and tip is just above it. Right now, I think since everything is put inside the div, the tooltip shows at where the end of the div is. I dont want to resize the width of the div to a fixed width either because some people's occupation is longer than others.
hersh
Can you put the page online ? I will fix the css
Nealv
Nealv, I've figured it out. I've replaced what you had ... "$(this).qtip({" ... with "$(this).find('label').qtip({" ... thanks again for the help and thanks for saving me a lot of time.
hersh
@hersh you are very welcome :) If you post a new question send me a msg :) I love jquery
Nealv