How to get a div based dropdown to get a scrollbar instead of going through the end of the screen using JavaScript and CSS? Struts are also involved.

I have a div-based popup menu that lasts until the end of the screen when the client puts a lot of records on it, hiding the bottom records. It is located in one column in the set of data entry lines (td-elements), so the available space varies depending on which row is entered.

Unfortunately, this application that I have inherited does not have any help and actually does not have a set of skills to maintain, so I would rate a little β€œexplain as if I were five”. Also, my apologies for the wall of text.

From extensive Googling and studying other code in the application, I believe that optionsCollection will solve this particular problem, but I need to disable the function to set other data when the selected value changes and not find a way to do that with optionsCollection.

Unfortunately, this thing is simply too large to publish, and I would most likely distort it if I tried to compose the problem offline, so this is what I think is appropriate code.

div

<div class="empform"> <html:form action="/processBlank"> <div id="divJobClass" style="visibility: hidden; position: absolute; height="50px" border-color: #fff; border-style:solid; border-width: 1px; background: white; opacity: 1"> <table id="tableJobClass" cellspacing="0" style="border-color: #9090ff; border-style:solid; border-width:1px;" cellpadding="0"> <% ctr = 0; for (JobClassVO jc : jcList) { // href="setJC(<%=jc.getGuid()% >, '< %=jc.getJcIdDesc()% >')" %> <tr><td><input style="border: none; background: white" type="text" id="jc<%=ctr%>" size="50" value="<%=jc.getJcIdDesc()%>" readonly="readonly" onclick="setJC(<%=jc.getGuid()%>, '<%=jc.getJcIdDesc()%>', <%=ctr%>)" onkeydown='jcListCheck(event);'></td></tr><% ctr++; } %> 

If the popup menu is launched by clicking the field

 <td> <html:hidden name="erfEmployee" property="jcGUIDString" indexed="true"/> <html:text name="erfEmployee" property="jcId" indexed="true" size="8" maxlength="25" onblur='<%= "isLastRow('JobClass', " + count + ");" %>' onclick='<%= "showJcList(" + count + ");" %>' onkeydown='<%= "jcCheck(event," + count + ");" %>' onchange='<%= "verifyHoursClass(" + count + ");" %>' readonly="true" /> </td> 

isLastRow, jcCheck and onChange are not related to appearance. The functions used to display the div,

 function showJcList(index) { var fld = elem("erfEmployee[" + index + "].erfEeSsnFormatted"); if (fld.value == "") return; var div = elem_("divJobClass"); jcGuidTarget = elem("erfEmployee["+index+"].jcGUIDString"); jcIdTarget = elem("erfEmployee["+index+"].jcId"); showList(jcIdTarget, div, jcGuidTarget); focusSelected("jc", null, <%=jcList.size()-1%>); } function showList(idTarget, div, guidTarget) { ddDiv = div; if (ddDiv.style.visibility == "visible") { ddDiv.style.visibility = "hidden"; return; } var iTop = 0, oNode = idTarget, iLeft = 0; while(oNode.tagName != "BODY" && oNode.tagName != "HTML") { iTop += oNode.offsetTop; oNode = oNode.offsetParent; } oNode = idTarget; while(oNode.tagName != "BODY" && oNode.tagName != "HTML") { iLeft += oNode.offsetLeft; oNode = oNode.offsetParent; } ddDiv.style.left = "" + iLeft + "px"; ddDiv.style.top = "" + (iTop + idTarget.offsetHeight) + "px"; ddDiv.style.visibility = "visible"; ddIdTarget = idTarget; ddGuidTarget = guidTarget; } 

A few sites I was looking for suggested overflowing: auto and sizing, but that didn't seem to change anything. Maybe I messed up the size setting? But it seems to me that a fixed size will not help, since the available space depends on which line is entered. I found that overflow: scrolling gave me horizontal and vertical scroll bars, but the bars just expand with the div right after the screen ends. I thought about trying to figure out how much space was left and make the div tall enough to fit, but I think that would be ugly. I will go there if necessary, though.

Any advice, help, pointers to ideas would be greatly appreciated. Thanks.

+6
source share
1 answer

This is a common problem when writing widget extensions.

You just need

  • Set the desired max-height element;
  • set overflow-y: auto to indicate that it adds scrollbars only if necessary;
  • with javascript, calculate if the top position of the element plus the height element is higher than the height the viewport (visible area of ​​the window)
  • if it is crowded then do something. Typically, the usual behavior is to invert the direction of the drop-down list, forcing it to start from the bottom of the parent element, increasing to the top.

Take a look at the jsFiddle example made using jQuery (for convenience). Resize the window and see what happens to the overflow element.

Or just run the following code snippet:

 $(".dropdown li").on('mouseenter mouseleave', function(e) { var submenu = $('ul:first', this); var submenuTop = submenu.offset().top; var submenuHeight = submenu.height(); var viewportHeight = $(window).height(); var isOverflowing = (submenuTop + submenuHeight > viewportHeight); if (isOverflowing) { submenu.css("top", $(this).height() - submenuHeight); } else { submenu.css("top", 0); } }); 
 * { box-sizing: border-box; } body { background: dodgerBlue; } .dropdown, .dropdown li, .dropdown ul { list-style: none; margin: 0; padding: 0; } .dropdown { width: 100%; } .dropdown ul { position: absolute; top: 100%; left: 100%; visibility: hidden; display: none; z-index: 1; width: 200px; max-height: 150px; overflow-y: auto; overflow-x: hidden; } .dropdown li { position: relative; float: left; clear: both; } .dropdown li:hover { z-index: 910; } .dropdown ul:hover, .dropdown li:hover>ul, .dropdown a:hover+ul, .dropdown a:focus+ul { visibility: visible; display: block; } .dropdown a { display: block; background: #000; color: #fff; padding: 5px 20px; text-decoration: none; } .dropdown ul li { width: 100%; } .dropdown li:hover a { background: #333; } .dropdown li a:focus, .dropdown li a:hover { background: #666; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"> </script> <ul class="dropdown"> <li><a href="#">Link 1</a> <ul> <li><a href="#">Link 1</a> </li> <li><a href="#">Link 2</a> </li> <li><a href="#">Link 3</a> </li> <li><a href="#">Link 4</a> </li> <li><a href="#">Link 5</a> </li> </ul> </li> <li><a href="#">Link 2</a> <ul> <li><a href="#">Link 1</a> </li> <li><a href="#">Link 2</a> </li> <li><a href="#">Link 3</a> </li> </ul> </li> <li><a href="#">Link 3</a> <ul> <li><a href="#">Link 1</a> </li> <li><a href="#">Link 2</a> </li> </ul> </li> <li><a href="#">Link 4</a> <ul> <li><a href="#">Link 1</a> </li> <li><a href="#">Link 2</a> </li> <li><a href="#">Link 3</a> </li> <li><a href="#">Link 4</a> </li> <li><a href="#">Link 5</a> </li> <li><a href="#">Link 6</a> </li> <li><a href="#">Link 7</a> </li> <li><a href="#">Link 8</a> </li> </ul> </li> <li><a href="#">Link 5</a> <ul> <li><a href="#">Link 1</a> </li> <li><a href="#">Link 2</a> </li> <li><a href="#">Link 3</a> </li> <li><a href="#">Link 4</a> </li> <li><a href="#">Link 5</a> </li> </ul> </li> <li><a href="#">Link 6</a> <ul> <li><a href="#">Link 1</a> </li> <li><a href="#">Link 2</a> </li> <li><a href="#">Link 3</a> </li> <li><a href="#">Link 4</a> </li> <li><a href="#">Link 5</a> </li> </ul> </li> <li><a href="#">Link 7</a> <ul> <li><a href="#">Link 1</a> </li> <li><a href="#">Link 2</a> </li> <li><a href="#">Link 3</a> </li> <li><a href="#">Link 4</a> </li> <li><a href="#">Link 5</a> </li> <li><a href="#">Link 6</a> </li> <li><a href="#">Link 7</a> </li> <li><a href="#">Link 8</a> </li> </ul> </li> </ul> 
+4
source

Source: https://habr.com/ru/post/986612/


All Articles