【问题标题】:Datatables not aligning properly when page is resized调整页面大小时数据表未正确对齐
【发布时间】:2020-08-09 11:38:36
【问题描述】:

我的 MVC 视图中有两个数据表。我可以将它们并排显示。但是当我调整浏览器的大小(还原)时,数据表相互重叠,而不是一个在另一个之下。当浏览器为全尺寸时,如何使数据表彼此相邻,而当我调整浏览器大小时,如何使数据表在另一个下方?我在 MVC razor 视图中使用 Bootstrap 4。

注意:这是我第一次在 stackoverflow 中附加 sn-p。所以如果不正确请多多包涵!

Tables aligned next to each other when on full screen

tables squeezing next to each other when resized

.dataTables_wrapper {
  border: thin solid #808080;
  border-collapse: collapse;
  padding-top: 1rem;
  padding-right: 2rem;
  padding-left: 2rem;
  padding-bottom: 1rem;
}
<div>
  @if (Enumerable.Count(ViewBag.recentprojects) > 0) {



  <div class="col-xs-6">
    <div class="table-responsive">
      <table id="data" class="table table-bordered table-striped table-responsive">
        <thead style="background-color:lightgray">
          <tr>
            <th class="text-center" style="cursor:pointer;background-color:burlywood">
              @Html.DisplayName("My Recent Projects")
            </th>


          </tr>
          <tr>
            <th class="text-center" style="cursor:pointer">
              @Html.DisplayName("Projects")
            </th>


          </tr>
        </thead>
        <tbody id="projects">
          @foreach (var item in ViewBag.recentprojects) {
          <tr>
            <td class="col">
              <a href="#">  @item.ProjectCode</a>: @item.ProjectTitle
            </td>
          </tr>
          }
        </tbody>
      </table>
    </div>
  </div>



  }

  <div class="col-xs-1">
  </div>
  <div class="col-xs-1">
  </div>
  <div class="col-xs-1">
  </div>
  <div class="col-xs-1">
  </div>

  @if ((ViewBag.Leader is true) && (Enumerable.Count(ViewBag.teamprojects) > 0)) {


  <div class="col-xs-6">
    <div class="table-responsive">
      <table id="data1" class="table table-bordered table-striped table-responsive">
        <thead style="background-color:lightgray">
          <tr>
            <th class="text-center" style="cursor:pointer;background-color:burlywood">
              @Html.DisplayName("My Team's Recent Projects")
            </th>
          </tr>

          <tr>
            <th class="text-center" style="cursor:pointer">
              @Html.DisplayName("Projects")
            </th>


          </tr>
        </thead>
        <tbody id="projects">
          @foreach (var item in ViewBag.teamprojects) {
          <tr>
            <td class="col">
              <a href="#"> @item.ProjectCode</a>: @item.ProjectTitle
            </td>

          </tr>
          }
        </tbody>
      </table>
    </div>
  </div>


  }


</div>

【问题讨论】:

    标签: asp.net-mvc bootstrap-4


    【解决方案1】:

    您使用过 Bootstrap。尝试在您的元素上实现网格模型。这样响应速度更快,您可以将响应设置为各种屏幕尺寸,例如非常小、小、中和大。 以下是根据屏幕尺寸使用哪些类的参考链接: https://www.w3schools.com/bootstrap4/bootstrap_grid_system.asp

    【讨论】:

    • 谢谢 Aniket。我尝试使用“Col-xs-6”而不是宽度属性。行为仍然相同。当我调整浏览器的大小时,表格会重叠而不是在彼此下方对齐。
    • 你必须同时使用很多来告诉它应该采取的大小。示例:class="col-xs-12 col-sm-12 col-md-5"
    【解决方案2】:

    如果您使用的是 Bootstrap,您还需要在元素和响应式表格上实现网格模型。网格模型提供了各种屏幕尺寸的响应能力,例如非常小、小、中和大。响应式表格使您的表格响应式。所以你的代码看下面的链接--

    <div class="panel-body" id="ProjectReq">
    
        <div class="row">
            <div class="col-xs-6">
                <div class="table-responsive">
                    <table id="data" class="table table-bordered table-striped table-responsive">
                        <thead style="background-color:lightgray">
                            <tr>
                                <th class="text-center" style="cursor:pointer;background-color:burlywood">
                                    My Recent Projects
                                </th>
    
    
                            </tr>
                            <tr>
                                <th class="text-center" style="cursor:pointer">
                                    Projects
                                </th>
    
    
                            </tr>
                        </thead>
                        <tbody id="projects">
                            <tr>
                                <td class="col">
                                    Test Project
                                </td>
                            </tr>
    
                        </tbody>
                    </table>
                </div>
            </div>
            <div class="col-xs-6">
                <div class="table-responsive">
                    <table id="data1" class="table table-bordered table-striped table-responsive">
                        <thead style="background-color:lightgray">
                            <tr>
                                <th class="text-center" style="cursor:pointer;background-color:burlywood">
                                    My Team's Recent Projects
                                </th>
                            </tr>
    
                            <tr>
                                <th class="text-center" style="cursor:pointer">
                                    Projects
                                </th>
    
    
                            </tr>
                        </thead>
                        <tbody id="projects">
    
                            <tr>
                                <td class="col">
                                    Test Project 1
                                </td>
    
                            </tr>
    
                        </tbody>
                    </table>
                </div>
            </div>
    
        </div>
    
    </div>
    

    【讨论】:

    • 谢谢@Ashiquzzaman。我修改了我的代码以在“table”类之外包含“table-responsive”类。结果还是一样。但是,当我从包装样式表中删除“width:39em”时,它不再重叠。但是,当页面调整大小时,表格不会在另一个下方对齐。相反,表格并排缩小,每个表格上都有一个水平滚动条。我正在尝试使它们彼此对齐!
    • @incorrigible 你使用“row”类吗?也请发截图
    • 我已将代码 sn-p 修改为正在使用的实际代码。我已经包含了这两个快照,以及它在全屏时的显示方式以及在调整屏幕大小时的显示方式。如您所见,它们仍然彼此相邻。我希望它在调整大小时在另一个下方对齐。感谢您的帮助!
    猜你喜欢
    • 1970-01-01
    • 2014-11-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多