Articles in this section
Category / Section

How to prevent text wrap in Dropdown popup when the text content exceeds the popup width?

2 mins read

Description

When DropDownList is set with a PopupWidth and if the text value is longer than the given width, then the text will be wrapped inside the popup element. To prevent DropDownList items text wrapping and get horizontal scroller in popup element, you can set the CSS style “white-space” value as “no-wrap” for the DropDownList items.

Solution

Define a DropDownList control and bind create event for it. In the create event handler, get the DropDownList object and set the CSS “white-space” as “no-wrap” for all list elements inside the DropDownList. This will prevent the text wrap of DropDownList items. To get the horiziontal Scroller for the popup, set the popup list wrapped width for the Scroller object and refresh() the Scroller. Refer the following code block,

HTML

<!-- Header section with required references for rendering JavaScript control-->    
    <div>Select a State</div>
    <input type="text" id="stateList" />

JavaScript

    $(function () {
        // declaration
        var states = [
        { index: "s1", countryName: "Alabama" }, { index: "s2", countryName: "Alaska" },
        { index: "s3", countryName: "Arizona" }, { index: "s4", countryName: "Arkansas" },
        { index: "s5", countryName: "California" }, { index: "s6", countryName: "Colorado" },
        { index: "s7", countryName: "Connecticut" },
        { index: "s8", countryName: "Delaware" },
        { index: "s9", countryName: "Florida" },
        { index: "s10", countryName: "Georgia" },
        { index: "s11", countryName: "Hawaii" },
        { index: "s12", countryName: "Idaho" }, { index: "s13", countryName: "Illinois" },
        { index: "s14", countryName: "Indiana" }, { index: "s15", countryName: "Iowa" },
        { index: "s16", countryName: "Kansas" }, { index: "s17", countryName: "Kentucky" },
        { index: "s18", countryName: "Louisiana" }, { index: "s19", countryName: "Maine" },
        { index: "s20", countryName: "Maryland" }, { index: "s21", countryName: "Massachusetts" },
        { index: "s22", countryName: "Michigan" }, { index: "s23", countryName: "Montana" },
        { index: "s24", countryName: "New Mexico" }, { index: "25", countryName: "New York" },
        { index: "26", countryName: "North Carolina" }, { index: "s27", countryName: "Nevada" },
        { index: "s28", countryName: "New Jersey" }, { index: "s29", countryName: "Pennsylvania" },
        { index: "s30", countryName: "Ohio" }, { index: "s31", countryName: "Oklahoma" },
        { index: "s32", countryName: "Oregon" },
        { index: "s33", countryName: "Rhode Island" },
        { index: "s34", countryName: "South Carolina" }, { index: "s35", countryName: "South Dakota" },
        { index: "s36", countryName: "Tennessee" }, { index: "s37", countryName: "Texas" },
        { index: "s38", countryName: "Utah" },
        { index: "s39", countryName: "Vermont" }, { index: "s40", countryName: "Virginia" },
        { index: "s41", countryName: "Washington" }, { index: "s42", countryName: "West Virginia" },
        { index: "s43", countryName: "Wisconsin" }, { index: "s44", countryName: "Wyoming" }
        ];
 
        $('#stateList').ejDropDownList({
            width: 100,
            popupWidth:100,
            dataSource: states,
            fields: { id: "index", value: "index", text: "countryName" },
            create: function (args) {
                var dropDownObj = $("#stateList").data("ejDropDownList");
                dropDownObj.ultag.children('li').css({ "white-space": "nowrap" });
                //To display the horizontal scroller
                dropDownObj.scrollerObj.model.width = dropDownObj.popupListWrapper.outerWidth();
                dropDownObj.scrollerObj.refresh();
            }
        });
    });

Output DropDownList items without text wrap,

DropDownList

Figure 1: Popup element with a horizontal Scroller


Conclusion

I hope you enjoyed learning about how to prevent text wrap in Dropdown popup when the text content exceeds the popup width.

You can refer to our JavaScript DropDown List feature tour page to know about its other groundbreaking feature representations and documentation, and how to quickly get started for configuration specifications.  You can also explore our JavaScript DropDown List example to understand how to create and manipulate data.

For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our other controls.

If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forumsDirect-Trac, or feedback portal. We are always happy to assist you!

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please sign in to leave a comment
Access denied
Access denied