2

I have a table of records which is displayed nicely in a HTML Table. The last column of this table displays a icon when hovering the row. When the user hovers the icon a drop down menu appears for all actions related to that row.

Currently I have the td that Icon is in set to Position:absolute and the div of the drop down menu is also set to Position Absolute.

This means I can align the Dropdown Menu Div to align nicely against the row it relates.

Firstly im not totally sure why this is the case. I would have thought I would need the TD as Absolute and the Menu Div as Relative but this just isnt the case.

The actual issue I am currently having (as the way I have it works across all browser) is that when the row is expanded to be double the height from text in other columns. The TD with absolute positioning applied does not expand to be 100% height of the TR. Which causes issues when trying to hover the tr and you move over where the last TD which should have expanded to the row height and it registers it as a hover out on the TR.

Any ideas to get around this? The problem with putting the icon in a div which is relative is that I need the div to be 100% height/width of the td to be able to align the menu correct to the row!

EDIT: As requested Some Html... This is a mustache template. Shouldn't be to hard to follow even if you have never used it

<table>
            <thead>
                <tr>
                    <th style="width:15px"><input type="checkbox" class="GridSelectAll" onclick="GridSelectAll(this);" /></th>
                    <th class="tar" style="width:30px">
                        <a href="javascript:SortGrid('EmployeeId')" id="headerEmployeeId">
                            <%= GetTranslation("EmployeeId") %><span class="sortArrow ui-icon"></span>
                        </a>                        
                    </th>
                    <th style="width:120px">
                        <a href="javascript:SortGrid('FirstName')" id="headerFirstName">
                            <%= GetTranslation("FirstName") %><span class="sortArrow ui-icon"></span>
                        </a>
                    </th>
                    <th style="width:120px">
                        <a href="javascript:SortGrid('LastName')" id="headerLastName">
                            <%= GetTranslation("LastName") %><span class="sortArrow ui-icon"></span>
                        </a>
                    </th>
                    <th>
                        <a href="javascript:SortGrid('CompanyName')" id="headerCompanyName">
                            <%= GetTranslation("PrimayCompanyName") %><span class="sortArrow ui-icon" ></span>
                        </a>
                    </th>
                    <th style="width:55px">
                        <a href="javascript:SortGrid('EmployeeRowStatus')" id="headerEmployeeRowStatus">
                            <%= GetTranslation("EmployeeRowStatus") %><span class="sortArrow ui-icon"></span>
                        </a>
                    </th>
                    <th style="width:60px">
                        <a href="javascript:SortGrid('IsLocked')" id="headerIsLocked">
                            <%= GetTranslation("IsLocked") %><span class="sortArrow ui-icon"></span>
                        </a>
                    </th>
                    <th style="width:15px"></th>
                </tr>
            </thead>
            <tbody>
                {{#EmployeeSummaries}}
                <tr>
                    <td><input type="checkbox" class="chkRowId" value="{{EmployeeId}}"  /></td>
                    <td class="tar">{{EmployeeId}}</td>
                    <td>{{FirstName}}</td>
                    <td>{{LastName}}</td>
                    <td>{{PrimaryCompanyName}}</td>
                    <td>{{EmployeeRowStatus}}</td>
                    <td>{{IsLocked}}</td>
                    <td style="position:absolute;">
                        <a class="optlink"><span class="ui-icon ui-icon-triangle-1-s"></span></a>
                        <div class="optmenu">
                            <ul>
                                <li><a onclick="UpdateEmployee_LoadDialog({{EmployeeId}});"><%= GetTranslation("ManageEmployee")%></a></li>
                                <li><a onclick="showLoading();" href="../Core/AuditItem.aspx?{{ItemTypeItemIdQuerystring}}"><%= GetTranslation("ViewHistory")%></a></li>
                                <li><a onclick="showLoading();" href="UserGroupsEvo.aspx#/?PageIndex=0&Filter=EmployeeId~EqualTo~{{EmployeeId}}"><%= GetTranslation("ViewUserGroups")%></a></li>
                                <li><a onclick="showLoading();" href="UserGroup_Employees.aspx?{{EmployeeIdQuerystring}}"><%= GetTranslation("ManageUserGroups")%></a></li>
                                <li><a onclick="showLoading();" href="../Employee/EmployeePreferences.aspx?{{EmployeeIdQuerystring}}"><%= GetTranslation("ManagePreferences")%></a></li>
                                <li><a onclick="ResetPasswords_LoadDialog({{EmployeeId}});"><%= GetTranslation("ResetPassword")%></a></li>
                                <li><a onclick="SendWelcomeEmails_LoadDialog({{EmployeeId}});"><%= GetTranslation("SendWelcomeEmail")%></a></li>
                                <li><a onclick="AddEmployee_Company_LoadDialog({{EmployeeId}}, {{PrimaryCompanyId}});"><%= GetTranslation("AddToCompany")%></a></li>
                                <li><a onclick="LoginAsUser({{EmployeeId}});"><%= GetTranslation("LoginAsUser") %></a></li>
                                {{#HasOtherCompanies}}
                                <li class="subheader"><%= GetTranslation("ManageOtherCompanies") %></li>
                                {{/HasOtherCompanies}}
                                {{#Companies}}
                                <li>
                                    <span style="vertical-align:top;" class="hh ui-icon ui-icon-circle-close" onclick='RemoveEmployeeFromCompany_LoadDialog(this, {{EmployeeId}}, {{CompanyId}}, "{{CompanyName}}");return false;' title="<%= GetTranslation("Remove") %>"></span>
                                    <a onclick="ManageEmployee_Company_LoadDialog({{EmployeeId}}, {{CompanyId}});">
                                        {{CompanyName}}
                                    </a>                                    
                                </li>
                                {{/Companies}}
                            </ul>
                        </div>    
                    </td>
                </tr>
                {{/EmployeeSummaries}}
            </tbody>

        </table>
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Steve
  • 2,971
  • 7
  • 38
  • 63
  • Could you post some sample code? Kinda hard to follow with just descriptions. – Travesty3 Apr 10 '12 at 15:05
  • now provided... .optlink span is the icon and .optmenu is the dropdown div that should appear in place – Steve Apr 10 '12 at 15:14
  • Have you tried making the `td` position:relative? – Travesty3 Apr 10 '12 at 15:18
  • Firefox seems to the ignore it and so instead of position the menu top right corner of the TD it positions it top right corner of the body. Chrome is fine with it and IE isn't far from being fine. (pixel or so out!) – Steve Apr 10 '12 at 15:29
  • here's why that is the case, the effect of `position: relative` on table elements is undefined http://stackoverflow.com/questions/4564638/using-position-relative-absolute-within-a-td – Smegger Apr 10 '12 at 15:31
  • ok so any methods to get around this behaviour? or a way to ensure if a set a div inside the td that it will be 100% height and width without setting the size of the td's? – Steve Apr 10 '12 at 15:45

2 Answers2

2

I would get rid of the table all together. I would then use divs with table behavior. This will give you much more control over how each element works, each row, and each column. This link explains the concept quite well: http://snook.ca/archives/html_and_css/getting_your_di

Thomas Wright
  • 1,309
  • 1
  • 9
  • 15
  • 1
    But he's displaying *tabular* data? Perfect use of a table? – Mathew Thompson Apr 10 '12 at 15:11
  • Yes, this is the proper use of a table. However, there is a reason that table qualities can be integrated into other elements. For example, the CSS code from the link: #left, #right, #middle { display: table-cell; } This allows you to build a functioning "table" with divs (which is more-or-less how tables are created in the DOM anyway), and still have access to other properties and functionality. – Thomas Wright Apr 10 '12 at 15:30
  • im not sure if your method is the best solution for me. (we need some good cross browser support) but it has certainly opened my eyes up to possibility of this in the future. Never new you could effectively create a table using the display table-row etc properties. fascinating. – Steve Apr 10 '12 at 16:04
1

You might try this::

<!DOCTYPE html>
<html>
<head>
<style>
body{font-family:arial;}
table{font-size:80%;background:black}
a{color:black;text-decoration:none;font:bold}
a:hover{color:#606060}
td.menu{background:lightblue}
table.menu
{
font-size:100%;
position:absolute;
visibility:hidden;
}
</style>
<script>
function showmenu(elmnt)
{
document.getElementById(elmnt).style.visibility="visible";
}
function hidemenu(elmnt)
{
document.getElementById(elmnt).style.visibility="hidden";
}
</script>
</head>

<body>
<h3>Drop down menu</h3>
<table width="100%">
 <tr bgcolor="#FF8080">
  <td onmouseover="showmenu('tutorials')" onmouseout="hidemenu('tutorials')">
   <a href="/default.asp">Tutorials</a><br>
   <table class="menu" id="tutorials" width="120">
   <tr><td class="menu"><a href="/html/default.asp">HTML</a></td></tr>
   <tr><td class="menu"><a href="/css/default.asp">CSS</a></td></tr>
   <tr><td class="menu"><a href="/xml/default.asp">XML</a></td></tr>
   <tr><td class="menu"><a href="/xsl/default.asp">XSL</a></td></tr>
   </table>
  </td>
  <td onmouseover="showmenu('scripting')" onmouseout="hidemenu('scripting')">
   <a href="/default.asp">Scripting</a><br>
   <table class="menu" id="scripting" width="120">
   <tr><td class="menu"><a href="/js/default.asp">JavaScript</a></td></tr>
   <tr><td class="menu"><a href="/vbscript/default.asp">VBScript</a></td></tr>
   <tr><td class="menu"><a href="default.asp">DHTML</a></td></tr>
   <tr><td class="menu"><a href="/asp/default.asp">ASP</a></td></tr>
   <tr><td class="menu"><a href="/ado/default.asp">ADO</a></td></tr>
   </table>
  </td>
  <td onmouseover="showmenu('validation')" onmouseout="hidemenu('validation')">
   <a href="/site/site_validate.asp">Validation</a><br>
   <table class="menu" id="validation" width="120">
   <tr><td class="menu"><a href="/web/web_validate.asp">Validate HTML</a></td></tr>
   <tr><td class="menu"><a href="/web/web_validate.asp">Validate XHTML</a></td></tr>
   <tr><td class="menu"><a href="/web/web_validate.asp">Validate CSS</a></td></tr>
   <tr><td class="menu"><a href="/web/web_validate.asp">Validate XML</a></td></tr>
   <tr><td class="menu"><a href="/web/web_validate.asp">Validate WML</a></td></tr>
   </table>
  </td>
 </tr>
</table>
<p>Mouse over these options to see the drop down menus</p>
</body>

</html>
ErickBest
  • 4,586
  • 5
  • 31
  • 43