简体   繁体   English

获取长度<li>使用 Bootstrap 4 选项卡内容时</li>

[英]Getting length of <li> when using Bootstrap 4 tab-content

I am a bit stuck on this.我有点坚持这一点。 I am trying to get the length of a list using Bootstrap 4's tab-content.我正在尝试使用 Bootstrap 4 的选项卡内容获取列表的长度。 Since the tab is not active on page load, the length is 0. When I open the tab, it is still 0.由于选项卡在页面加载时未处于活动状态,因此长度为 0。当我打开选项卡时,它仍然为 0。

I added the html that gets printed as well as the ajax call to get the data.我添加了打印的 html 以及 ajax 调用以获取数据。

// print applicant status
if (applicantStatus == 'true') {
  $('#aStatus').html('Applicant');

  // video module count
  var totalModCount = $("#vidList").find("li").length;
  console.log(totalModCount); // 0 Supposed to be 9
  $('#totalCount').html(totalModCount)

  // form total count
  var totalFormCount = $("#vidList").find("li").length;
  console.log(totalFormCount); // 0 Supposed to be 4
  $('#formTotalCount').html(totalFormCount);
}
else {
  $('#aStatus').html('Employee');
};

var listOfTypes = "";
$.each(parsedData, function (index, item) {

  var dateOfCompletion = moment(item.DateCompleted).format('MM/DD/YYYY');
  lastAttempt = moment(item.DateOfExam).format('MM/DD/YYYY');
  isValid = item.Valid;
  isOrientation = item.IsOrientation;
  isInservice = item.IsInservice;

  if (applicantStatus == "true") {
    if (isOrientation) {
      if (isValid) {
        if (item.IsComplete == true) {
          listOfTypes += "<li class='list-group-item disabled videoItem text-success'><a class='video-btn gray' data-toggle='modal' data-src='" + item.URL
          listOfTypes += "' id='tID_" + item.TypeID + "' href='#myModal'>";
          listOfTypes += item.TypeName + "</a>";
          listOfTypes += "<br><p id='typeStatus' value='" + item.TypeID + "' class='completionDate'>Completed on: " + dateOfCompletion + "</p></li>";

        }
        else {
          listOfTypes += "<li class='list-group-item videoItem'><a class='video-btn' data-toggle='modal' data-src='" + item.URL
          listOfTypes += "' id='tID_" + item.TypeID + "' href='#myModal'>";
          listOfTypes += item.TypeName + "</a>";
          listOfTypes += "<br><p id='typeStatus' value='" + item.TypeID + "' class='timeDuration'>Time Duration: " + item.Duration + "</p></li>";
        }
      }
      else {
        listOfTypes += "<li class='list-group-item d-none videoItem'><a class='video-btn' data-toggle='modal' data-src='" + item.URL
        listOfTypes += "' id='tID_" + item.TypeID + "' href='#myModal'>";
        listOfTypes += item.TypeName + "</a>";
      }
    }
  }
});
<div class='tab-content'>
  <div id="videoList" class="tab-pane fade">
    <ul class='list-group' id='vidList'>
      <li class="list-group-item videoItem"></li>
      <li class="list-group-item videoItem"></li>
      <li class="list-group-item videoItem"></li>
      <li class="list-group-item videoItem"></li>
      <li class="list-group-item videoItem"></li>
      <li class="list-group-item videoItem"></li>
    </ul>
  </div>
  <div id="formsList" class="tab-pane fade">
    <ul class='list-group' id='fList'>
      <li class="list-group-item formItem"></li>
      <li class="list-group-item formItem"></li>
      <li class="list-group-item formItem"></li>
      <li class="list-group-item formItem"></li>
    </ul>
  </div>
</div>

If the call is asynchronous , you need to check after you have loaded the tabs.如果调用是异步的,则需要在加载选项卡后进行检查。 So take the below code and its respective function into the callback function of the async call and check.因此,将以下代码及其各自的 function 放入异步调用的回调 function 并检查。

var totalModCount = $("#vidList").find("li").length;

Until then it's going to be empty.在那之前它将是空的。

I imagine that the lists are loaded from an ajax call?我想这些列表是从 ajax 调用加载的? in this case the code you entered must be executed at the callback in the success of the call在这种情况下,您输入的代码必须在调用成功的回调中执行

$ajax({
    url : '',
    type: ...,
    success : function(response) {
    // add the <li> </li> AND THEN DO THE COUNT
    var totalModCount = $("#vidList").find("li").length;
    }

})

This is what I had to do to make it work.这是我必须做的才能让它工作。

Thank you everyone for your help.感谢大家的帮助。 I believe my explanations were very confusing, but your input helped me land the answer.我相信我的解释非常混乱,但您的意见帮助我找到了答案。

var completedCount = parsedData.filter(function (item) {
  if (item.Valid == true) {
    return item.IsComplete
  }
}).length;
$('#completionCount').html(completedCount);

// totalCount
var totalCount = parsedData.filter(function (item) {
  if (item.IsOrientation == true && item.Valid == true) {
    return item.IsOrientation
  }
}).length;
$('#totalCount').html(totalCount);

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

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