【问题标题】:My Bootstrap Datepicker style conflicts with my table style我的 Bootstrap Datepicker 样式与我的表格样式冲突
【发布时间】:2021-08-03 06:08:41
【问题描述】:

我有一个表格,我最近刚刚编辑了一个带有可滚动的粘性标题。但是,当我添加该更改时,它也影响了我的 Datepicker。有没有办法绕过这个?还是分开?

这是我为带有可滚动的表格粘性标题添加的代码:

thead:not(.ui-datepicker-calendar>thead), tbody:not(.ui-datepicker-calendar>tbody) {
display: block;
}
.table-container {
    height: 10rem;
}
td:not(.ui-datepicker-calendar) {
    word-wrap: break-word !important;
}
table:not(.ui-datepicker-calendar) {
    display: flex;
    flex-flow: column;
    height: 39.6rem;
    width: 100%;
}

thead:not(.ui-datepicker-calendar) {
    /*       head takes the height it requires, 
    and it's not scaled when table is resized */
    flex: 0 0 auto;
    width: calc(100% - 0.9em);
}

table:not(.ui-datepicker-calendar) tbody:not(.ui-datepicker-calendar>tbody) {
    /*  body takes all the remaining available space */
    flex: 1 1 auto;
    display: block;
    overflow-y: scroll;
}

    table:not(.ui-datepicker-calendar) tbody:not(.ui-datepicker-calendar>tbody) tr:not(.ui-datepicker-calendar>tr) {
        width: 100%;
    }

    table:not(.ui-datepicker-calendar) thead:not(.ui-datepicker-calendar), table:not(.ui-datepicker-calendar) tbody:not(.ui-datepicker-calendar>tbody) tr:not(.ui-datepicker-calendar>tr) {
        display: table;
        table-layout: fixed;
    }
tbody:not(.ui-datepicker-calendar) {
    height: 100px;
    overflow-y: auto;
    overflow-x: hidden;
   
}

日期选择器的代码

<div class="d-flex flex-row marginDateStart">
        <label for="StartDate" class="labelmargin">Start Date:</label>
        <input id="datepicker1" class="datepicker" name="datepicker1" value="@DateTime.Now.ToString("yyyy-MM-dd")" />
        <script>
            $('#datepicker1').datepicker({
                uiLibrary: 'bootstrap4',
                format: "yyyy/mm/dd",
                todayHighlight: true
            });
        </script>

    </div>

我使用的库

 <link rel="stylesheet" href="~/lib/bootstrap/dist/css/bootstrap.min.css" />
<link rel="stylesheet" href="~/css/site.css" type="text/css" />
<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
<link rel="stylesheet" href="/resources/demos/style.css">
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>

如果您检查所附图片,第一个是我制作的表格修复。它与我的日期选择器冲突,第二张图片是没有冲突的日期选择器,因为我没有添加我所做的表修复。

[带有我制作的表格修复的数据选择器][1]

[没有我制作的表格修复的日期选择器][2]

最新更新: 经过以上 CSS sn-p 代码的一些故障排除后,现在可以正常工作并且不会与 datepicker 冲突。关键是 :not(.ui-datepicker-calendar>thead) 格式以获得额外的粒度

感谢大家的帮助!

【问题讨论】:

    标签: html css asp.net bootstrap-4


    【解决方案1】:

    也许您可以尝试在您的 css 中排除 datepicker css 类。

    table:not(.ui-datepicker-calendar) {
        display: flex;
        flex-flow: column;
        height: 39.6rem;
        width: 100%;
    }
    table > :not(.ui-datepicker-calendar) tbody {
        height: 100px;  Just for the demo          
        overflow-y: auto;  Trigger vertical scroll    
        overflow-x: hidden;  Hide the horizontal scroll 
    }
    /* add more for other affected css rules */
    

    更多信息:not https://developer.mozilla.org/en-US/docs/Web/CSS/:not

    【讨论】:

    • 我尝试将它添加到表格标签上并得到了一些小结果。我是否需要将它也添加到其他人身上,比如thead 等?抱歉,我对 CSS 样式很陌生。
    • 是的,理想情况下,您还希望将其添加到其他人,例如thead,但是在从日期选择器查看呈现的 html 之后,这里是您要用于表格 css 规则的类。表:不(.ui-datepicker-日历)。看到这有帮助。如果它仍然在干扰您的 thead、tbody 等。如果您需要额外的粒度,可以使用类似 thead:not(.ui-datepicker-calendar>thead) 的东西。
    • 我在上面的代码中添加了一个更新,似乎唯一剩下的是 Datepicker 的大小,我无法在 F12 的检查中找到要编辑的内容。
    • 我认为我需要在 css 中减少一些但不知道哪个? @RobSmitha
    • 好的,我设法通过反复试验解决了问题,并在上​​面发布了正确的答案,感谢您的帮助!!
    【解决方案2】:

    您可以做的是:为您的自定义表指定类名并使用 datepicker 的表 id 来编辑 datepicker 表。

    见下面的片段:

    $('#datepicker1').datepicker({
      uiLibrary: 'bootstrap4',
      format: "yyyy/mm/dd",
      todayHighlight: true
    });
    $("#datepicker1").datepicker('setDate', new Date());
    /*Styles for datepicker*/
    
    #ui-datepicker-div thead {
      background-color: lightgreen;
    }
    
    /*Styles for other tables*/
    .otherTables{
      background-color: lightblue;
    }
    <link rel="stylesheet" href="~/lib/bootstrap/dist/css/bootstrap.min.css" />
    <link rel="stylesheet" href="~/css/site.css" type="text/css" />
    <link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
    <link rel="stylesheet" href="/resources/demos/style.css">
    <script src="https://code.jquery.com/jquery-1.12.4.js"></script>
    <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
    
    <div class="d-flex flex-row marginDateStart">
            <label for="StartDate" class="labelmargin">Start Date:</label>
            <input id="datepicker1" class="datepicker" name="datepicker1">
    </div>
    <table class="otherTables">
      <tr>
        <th>Firstname</th>
        <th>Lastname</th>
        <th>Age</th>
      </tr>
      <tr>
        <td>Jill</td>
        <td>Smith</td>
        <td>50</td>
      </tr>
    </table>

    【讨论】:

    • 但是如果我需要编辑“otherTables”的 tr/th/tbody,我需要在它们上面添加一些类还是因为它在 table 标记下而无法理解?
    • 如果您需要编辑“otherTables”的tr/th/tbody,只需使用“otherTables tr, otherTables th, otherTables tbody”语法即可。它被称为“后代选择器”。 “otherTables”是外框/父元素,“tr/th/tbody”是“otherTables”的内框/子元素。希望它能回答你。
    • 这确实完成了工作,但是它需要所有未来的表都添加“otherTables”css 类,这会增加额外的工作。这对于学习目的或小型站点来说没什么大不了的,但是当站点有很多表时,添加 css 类变得很麻烦。
    猜你喜欢
    • 2011-09-06
    • 1970-01-01
    • 2023-03-09
    • 2014-04-12
    • 2021-07-19
    • 2017-09-25
    • 2022-10-31
    • 2016-04-19
    相关资源
    最近更新 更多