简体   繁体   English

如何默认隐藏排序图标并通过单击列 header 名称显示

[英]How do I hide sort icon by default and show by click on column header name

I wanna to hide sorting icons by default and show them by clicking the column header name.我想默认隐藏排序图标并通过单击列 header 名称来显示它们。 This is my.cshtml code:这是 my.cshtml 代码:

    @model string[]
<style>
</style>
<link href="//maxcdn.bootstrapcdn.com/font-awesome/4.1.0/css/font-awesome.min.css" rel="stylesheet">
    <div class="tableHeader" id ="table">   
        <div class="card w-100 mb-1 border-0">
            <div class="card-dark-mode-body text-dark headerRow fw-bolder mainColor" >
                <div class="row p-2">
                    @foreach(var prop in Model)
                    {
                       
                        <div class="col text-center d-table sortable" >
                             <div class="btn-group" id="sorting" onclick="sortingVisibility()">
                            <p class="mb-0 d-lg-table-cell align-middle" id="columnName" style='padding-top:3px'>
                                @prop
                                <div class="btn-group-vertical" style='padding-left: 20px;' id="divIcon">
                                <a href="/Administracija/Ocitavanja/Index?OrderBy=@prop&Sorting=asc" class="btn btn-xs btn-link p-0">
                                    <i class="fa fa-sort-up" style='color:white;' id="sortUpIcon" aria-hidden="true"></i>
                                </a>
                                <a href="/Administracija/Ocitavanja/Index?OrderBy=@prop&Sorting=desc" class="btn btn-xs btn-link p-0">
                                    <i class="fa fa-sort-down" style='color:white;' id="sortDownIcon" aria-hidden="true"></i>
                                    @*<span class="glyphicon glyphicon-triangle-bottom" style='color:white;'></span>*@
                                </a>
                                </div>
                            </p>
                            </div>
                        </div>
                    }
                </div>
            </div>
        </div>
    </div>
    @*location.href='/Administracija/Ocitavanja/Index?OrderBy=@prop&Sorting=desc'*@
    <script>

       function sortingVisibility() 
        {  
            var iconUp = document.getElementById("sortUpIcon");
            var iconDown = document.getElementById("sortDownIcon");
            var div = document.getElementById("divIcon");  
            if (div.style.display !== "none") {  
                div.style.display = "none";
            }  
            else {  
                div.style.display = "block";
                
            }  
        }  

I tried to do like this but its not working on click it only hide the icon and doesnt change anything.我试图这样做,但点击它不起作用,它只会隐藏图标并且不会改变任何东西。 Can anyone give me some hints about this?谁能给我一些提示?

In order to hide the div, you have to initially set the display property of the div to none and then we have to set the display property to block or none .为了隐藏 div,您必须首先将 div 的display 属性设置为none ,然后我们必须将 display 属性设置为blocknone Below I have modified your code with the changes.下面我通过更改修改了您的代码。

 @model string[]
<style>
</style>
<link href="//maxcdn.bootstrapcdn.com/font-awesome/4.1.0/css/font-awesome.min.css" rel="stylesheet">
    <div class="tableHeader" id ="table">   
        <div class="card w-100 mb-1 border-0">
            <div class="card-dark-mode-body text-dark headerRow fw-bolder mainColor" >
                <div class="row p-2">
                    @foreach(var prop in Model)
                    {
                       
                        <div class="col text-center d-table sortable" >
                             <div class="btn-group" id="sorting" onclick="sortingVisibility()">
                            <p class="mb-0 d-lg-table-cell align-middle" id="columnName" style='padding-top:3px'>
                                @prop  
{/* added style="display:none" */}                            
<div class="btn-group-vertical" style='padding-left: 20px; display: none' id="divIcon">
                                <a href="/Administracija/Ocitavanja/Index?OrderBy=@prop&Sorting=asc" class="btn btn-xs btn-link p-0">
                                    <i class="fa fa-sort-up" style='color:white;' id="sortUpIcon" aria-hidden="true"></i>
                                </a>
                                <a href="/Administracija/Ocitavanja/Index?OrderBy=@prop&Sorting=desc" class="btn btn-xs btn-link p-0">
                                    <i class="fa fa-sort-down" style='color:white;' id="sortDownIcon" aria-hidden="true"></i>
                                    @*<span class="glyphicon glyphicon-triangle-bottom" style='color:white;'></span>*@
                                </a>
                                </div>
                            </p>
                            </div>
                        </div>
                    }
                </div>
            </div>
        </div>
    </div>
    @*location.href='/Administracija/Ocitavanja/Index?OrderBy=@prop&Sorting=desc'*@
    <script>

       function sortingVisibility() 
        {  
            var iconUp = document.getElementById("sortUpIcon");
            var iconDown = document.getElementById("sortDownIcon");
            var div = document.getElementById("divIcon");  
            //we check if the display property is none by default.
if (div.style.display === "none") {  
                div.style.display = "block";
            }  
            else {  
                div.style.display = "none";
                
            }  
        }  

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

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