Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make dropdown menu go upwards if it is at the bottom of the browser window, automatic toggle between down and up, depending on position [duplicate]

I am getting problem in Dropdown that goes down but it should go up according to window size.i am getting this problem in Angular UI Grid. Please see a link to understand better.

<div ng-controller="MyCtrl">
<div class="container">
    <div class="row">
        <div class="span2">
            <table class="table">
                <tr ng-repeat="row in [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24]">
                    <td> <a>{{row}}</a>

                    </td>
                    <td>
                        <ul class="nav">
                            <!-- To create dropups instead of dropdowns i could add the class "dropup" to the li-element below. -->
                            <!-- But what I need is an inteligent mechanism that decides when to use dropdown and when to use dropup -->
                            <li class="dropdown"> <a class="btn-link dropdown-toggle">
                                <i class="icon-align-justify"></i>
                            </a>

                                <ul class="dropdown-menu">
                                    <li ng-repeat="menu in [1,2,3]"> <a class="btn-link">{{menu}}</a>

                                    </li>
                                </ul>
                            </li>
                        </ul>
                    </td>
                </tr>
            </table>
        </div>
    </div>
</div>
like image 258
Ankush Verma Avatar asked Jan 03 '17 14:01

Ankush Verma


People also ask

Which of the following classes can be used to make a dropdown menu go upward?

Notice the dropup class. This is what allows it to go up instead of down.

How do I make a drop down menu go over content?

Step 1: Create and style a div with a class name “dropdown.” First, create a div and add the class dropdown to it. In CSS, set this div's display to inline-block and position to relative. This will allow the dropdown content to appear right below the dropdown button.

How do you make a dropdown open up in CSS?

Example ExplainedUse any element to open the dropup menu, e.g. a <button>, <a> or <p> element. Use a container element (like <div>) to create the dropup menu and add the dropup links inside it. Wrap a <div> element around the button and the <div> to position the dropup menu correctly with CSS.

Why dropdown is showing upward?

This is happening because may be you don't have space on your screen below the dropdown when you select first time. When you select first item, it shows the pannel below it with makes more space available below your dropdown and that is why in second time your dropdown opens downward.


1 Answers

You are going to have to handle this yourself, it may be a little messy but I honestly I don't see how you can do it otherwise. I have modified your li tag to add a class (you can also use id) attribute to be able to reference it using jQuery (or plain javascript if you want to) and then added an ng-class to be able to dynamically assign either dropdown or dropup.

<li class="{{'row-' + row}}" ng-class="getClass(row)">
    <a class="btn-link dropdown-toggle"><i class="icon-align-justify"></i></a>
    <ul class="dropdown-menu">
        <li ng-repeat="menu in [1,2,3]"> <a class="btn-link">{{menu}}</a></li>
    </ul>
</li>

Now, inside your controller, you can create the getClass function to decide, based on the dropdown's position relative to the window, which class it should have.

var dropdownHeight = 140; // estimate/calculate the dropdown list's height manually
$scope.getClass = function (row) {
    if ($(window).height() - $('.row-' + row).position().top < dropdownHeight) {
        return 'dropup';
    }
    return 'dropdown';
};

You can play around with the values of the height until satisfied with the behavior, but keep in mind the you might want to add an event listener on window resize (and related events) to be able to recalculate the classes based on the new numbers.

like image 77
Ghassen Louhaichi Avatar answered Oct 12 '22 22:10

Ghassen Louhaichi