使用物化css

时间:2015-12-23 19:03:06

标签: javascript jquery html css materialize

我目前正在使用ajax动态创建一个包含营养数据的表。我正在使用Materialize.css中的响应表功能,以允许较小屏幕上的表数据可以水平滚动。我希望响应表中的最大宽度为300px,如我的CSS中所示。

但是,当我将屏幕缩小到较小的尺寸(~400px)时,每个td中的文本不会中断并与其他列重叠,如下所示:

如何在较小的屏幕上保持列宽(400px),同时防止文本重叠?提前谢谢!

我为我的项目提供了Codepen

HTML:

<div class="row valign-wrapper">
  <div class="input-field col s3 valign">
    <input id="food" type="text" class="validate" />
    <label for="food">Keyword</label>
  </div>
  <div class="col s2 valign">
    <a class="waves-effect waves-light btn cyan food-search">Search</a>
  </div>
</div>

<div class="row">
  <table>
    <thead>
    </thead>
    <tbody>
    </tbody>
  </table>
</div>


<!-- Modal Structure -->
<div id="nutrition-facts" class="modal modal-fixed-footer">
  <div class="modal-content">
    <h4>Nutrition Facts</h4>
    <p>A bunch of text</p>
  </div>
  <div class="modal-footer">
    <a href="#!" class="modal-action modal-close waves-effect waves-green btn-flat">Close</a>
  </div>
</div>

CSS:

body {
  background: #f9f4ed;
}

h1 {
 color: white;
}

tr td {
  max-width: 300px;
}

jQuery的:

var apiKey = "rHkKtH6RMPiYlFkjl3jBGWpfcEJB3ZMqkyZmAxHK";

$(document).ready(function() {

  $(".food-search").on("click", function(e) {

    var search = $("input:text").val();

    e.preventDefault();

    $("thead").empty();
    $("tbody").empty();

    $.ajax({
      type: "GET",
      url: "http://api.nal.usda.gov/ndb/search/?format=json&q=" + search + "&sort=r&max=25&offset=0&api_key=" + apiKey,
      success: function(data) {
      var foodList = buildTable(data);
     },
      error: function(jqXHR, textstatus, errorThrown) {
        console.log(jqXHR);
        console.log(textstatus);
        console.log(errorThrown);
      }

    });

  });

  function buildTable(foodData) {
    var itemList = foodData.list.item;
    var foodGroup, foodName, newDiv, createButton, ndbNumber, createTable, tableHead, categoryHeading, nameHeading, tr;
    $("table").addClass("bordered centered responsive-table");
    categoryHeading = $("<th>").html("Category");
    nameHeading = $("<th>").html("Name");
    $("thead").append(categoryHeading).append(nameHeading);
    for (var i = 0; i < itemList.length; i++) {
      foodGroup = $("<td>").html(itemList[i].group);
      foodName = $("<td>").html(itemList[i].name);
      ndbNumber = itemList[i].ndbno;
      newDiv = $("<td>");
      createButton = $("<a>")
      .addClass("waves-effect waves-light btn cyan nutrition modal-trigger")
      .attr("href", "#nutrition-facts")
      .html("Nutrition Facts")
      .attr('data-ndbnum', ndbNumber);
      addButton = newDiv.append(createButton);
      tr = $("<tr>").append(foodGroup).append(foodName).append(addButton);
     $("tbody").append(tr);
   }
 }

  $(document).on('click', '.nutrition', function(e) {
    e.preventDefault();
    var ndbNumber = $(this).attr('data-ndbnum');
    $(".modal-trigger").leanModal();
  });

});

1 个答案:

答案 0 :(得分:1)

您的materialize.min.css添加此媒体查询:

@media only screen and (max-width: 992px)
table.responsive-table tbody {
    display: block;
    width: auto;
    position: relative;
    overflow-x: auto;
    white-space: nowrap;
}

&#34;白色空间:nowrap&#34;是你的问题。您可以在tr / td:

上覆盖它
tr td {
  max-width: 300px;
  white-space:normal;
}