简体   繁体   English

在DropDownList中的某些项目上添加缩进-ASP.net MVC Razor视图

[英]Add indentation on certain items in DropDownList - ASP.net MVC Razor View

I've seen a few questions that seem similiar to my issue, but I haven't been able to nail my question yet - so I'm hoping someone has either had the same issue or knows how best to approach this. 我看到了一些看起来与我的问题类似的问题,但是我还无法确定我的问题-所以我希望有人遇到相同的问题,或者知道如何最好地解决这个问题。

I have a SelectedItemList of Locations from a database. 我有一个来自数据库的Locations SelectedItemList It returns a list of Locations , mostly cities but some countries are listed too. 它返回位置列表,大多数是城市,但也列出了一些国家。 This list would appear as such; 该列表将这样显示;

Australia
Melbourne
Perth
Sydney
Bermuda
Canada
Calgary
Toronto
Vancouver

All the items in the list have an Id and Text property, and any cities that belong to countries have a ParentLocation_Id property, which corresponds to the country Id (Australia is 8, so Sydney and Perth will have "ParentLocation_Id" of 8, etc.) So I know this is the conditional I will use to target those items within the list, in order to indent the correct ones. 列表中的所有项目均具有Id和Text属性,任何属于国家的城市均具有ParentLocation_Id属性,该属性与国家/地区ID相对应(澳大利亚为8,因此,悉尼和珀斯的“ ParentLocation_Id”为8,依此类推。 )因此,我知道这是我用来定位列表中那些项的条件,以便缩进正确的项。

I would like to use the @Html.DropDownListFor() method and be able to target the countries in order to indent them, so the list would appear within the Select list like so; 我想使用@ Html.DropDownListFor()方法,并能够针对国家/地区进行缩进,因此该列表将像这样出现在Select列表中;

在此处输入图片说明

but I wondered if there was a neater way to do this by using an HTML Extension. 但我想知道是否有使用HTML扩展的更整洁的方法。 Has anyone ever tried this before? 有人尝试过吗?

I have managed it so far by doing it this way, but you can see it looks hideous :( 到目前为止,我已经通过这种方式对其进行了管理,但是您可以看到它看起来很可怕:(

<select id="SelectedLocation" name="SelectedLocation">
                    @{
                        foreach (var location in Model.Locations)
                        {
                            if (location.Item.ParentLocation != null && location.Item.ParentLocation.Id != null)
                            {
                                <option value="@location.Item.Id" class="styled">&nbsp;&nbsp;&nbsp;&nbsp;@location.Item.Text</option>
                            }
                            else
                            {
                                <option value="@location.Item.Id" class="styled">@location.Item.Text</option>
                            }
                        }
                    }
                </select>

It may even be that this is the only way to achieve this UI, but I'd love to know if anyone thinks of anything better to handle this. 甚至可能这是实现此UI的唯一方法,但是我很想知道是否有人认为有什么更好的方法来解决此问题。

Thanks! 谢谢!

Thanks to all for helping with my issue. 感谢所有人为我的问题提供帮助。

I spoke with my boss re my problem, and he suggested I was over-complicating things slightly by wanting to do an Extension method. 我和老板讨论了我的问题,他建议我通过使用扩展方法来使事情稍微复杂一些。

I have managed to get the look and effect I want from a simple radio-button list, a little bit of JS and some styling. 我已经从一个简单的单选按钮列表,一些JS和一些样式中获得了想要的外观和效果。

It's taken me a little bit longer but I've learned something so I'm seeing it as a worthwhile task :) 我花了一点时间,但是我学到了一些东西,所以我认为这是一项值得的工作:)

Anyone whom is interested can check out the code below 任何有兴趣的人都可以查看下面的代码

  /* When the user clicks on the button, toggle between hiding and showing the dropdown content */ $('#selectLocation').click(function () { document.getElementById("locationDropdown").classList.toggle("show"); return false; }); // Close the dropdown menu if the user clicks outside of it window.onclick = function (event) { if (!event.target.matches('.dropbtn')) { var dropdowns = document.getElementsByClassName("dropdown-content"); var i; for (i = 0; i < dropdowns.length; i++) { var openDropdown = dropdowns[i]; if (openDropdown.classList.contains('show')) { openDropdown.classList.remove('show'); } } } }; $('input[name="SelectedLocation"]').on('change', function () { var idVal = $(this).attr("id"); $("#selectLocation").text($("label[for='" + idVal + "']").text()); }); 
 .field { float: left; clear: left; margin: 1em 0; width: 100%; } field-label { float: left; margin: 0 10px 0 0; width: 180px; line-height: 140%; font-size: .9375em; } .dropdown { display: inline-block; position: relative; vertical-align: middle; width: 200px; } #selectLocation a { cursor: pointer; text-decoration: none; } #content a { text-decoration: underline; color: #333; } #selectLocation { -moz-appearance: none; -webkit-appearance: none; background: #fff url(https://cdn3.iconfinder.com/data/icons/google-material-design-icons/48/ic_keyboard_arrow_down_48px-128.png) no-repeat; background-position: right 10px center; background-size: 20px; border: 1px solid #ccc; display: block; padding: 10px; width: 100%; } #locationDropdown { border: 1px solid #799BD2; height: 300px; list-style-type: none; margin: 0; margin-top: 0; overflow-y: scroll; padding-left: 0; } ul#locationDropdown li:hover { background-color: #1E90FF; } #content ul { list-style: disc outside; } .dropdown-content { background-color: #f9f9f9; box-shadow: 0 8px 16px 0px rgba(0, 0, 0, 0.2); display: none; min-width: 160px; position: absolute; z-index: 1; } .lblLocation { display: inline-block; width: 100%; } input[name="SelectedLocation"] { display: none; } .indent { padding-left: 10px; } .indentExtra { padding-left: 30px; } .dropdown-content.show { margin: 0; width: 110%; display: block; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.min.js"></script> <div class="dropdown"> <a id="selectLocation"> <span>Select location</span> </a> <ul id="locationDropdown" class="dropdown-content"> <li> <label class="lblLocation" for="8" value="Australia"> <input id="8" name="SelectedLocation" type="radio" value="8"> <span class="indent">Australia</span> </label> </li> <li> <label class="lblLocation" for="9" value="Melbourne"> <input id="9" name="SelectedLocation" type="radio" value="9"> <span class="indentExtra">Melbourne</span> </label> </li> <li> <label class="lblLocation" for="10" value="Perth"> <input id="10" name="SelectedLocation" type="radio" value="10"> <span class="indentExtra">Perth</span> </label> </li> <li> <label class="lblLocation" for="11" value="Sydney"> <input id="11" name="SelectedLocation" type="radio" value="11"> <span class="indentExtra">Sydney</span> </label> </li> <li> <label class="lblLocation" for="12" value="Bermuda"> <input id="12" name="SelectedLocation" type="radio" value="12"> <span class="indent">Bermuda</span> </label> </li> <li> <label class="lblLocation" for="13" value="Canada"> <input id="13" name="SelectedLocation" type="radio" value="13"> <span class="indent">Canada</span> </label> </li> <li> <label class="lblLocation" for="14" value="Calgary"> <input id="14" name="SelectedLocation" type="radio" value="14"> <span class="indentExtra">Calgary</span> </label> </li> <li> <label class="lblLocation" for="15" value="Toronto"> <input id="15" name="SelectedLocation" type="radio" value="15"> <span class="indentExtra">Toronto</span> </label> </li> <li> <label class="lblLocation" for="16" value="Vancouver"> <input id="16" name="SelectedLocation" type="radio" value="16"> <span class="indentExtra">Vancouver</span> </label> </li> </ul> </div> 

Thanks again to all who commented! 再次感谢所有发表评论的人!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM