简体   繁体   English

如何使引导程序选项卡仅在单击时加载内容,而不在页面上加载

[英]How to make bootstrap tabs only load content when clicked, not on page load

My use case is I have a page I've written with 6 tabs. 我的用例是我有一个用6个选项卡编写的页面。 Those tabs load the results of a php file index function I've created. 这些选项卡加载我创建的php文件索引功能的结果。 All was fine and dandy when I had the files on a USB drive connected directly to the server. 当我将USB驱动器上的文件直接连接到服务器时,一切都很好。 Loaded the page in seconds. 以秒为单位加载页面。 However, I've moved the files to a NAS storage drive, and it takes 30-60+ seconds to load the page now. 但是,我已将文件移至NAS存储驱动器,并且现在需要30-60秒钟以上的时间才能加载页面。 That's because with bootstrap tabs, it loads all the tabs on page load. 这是因为使用引导程序选项卡时,它会在页面加载时加载所有选项卡。 So it's having to load all the files for all the tabs before the page finishes loading. 因此,必须在页面完成加载之前为所有选项卡加载所有文件。

What I'm wanting to do is have it only load the "current" tab. 我想要做的是仅加载“当前”标签。 I know I could just use $POST and reload the page each time, but honestly I think what I really need to do here is use AJAX. 我知道每次都可以使用$ POST并重新加载页面,但是老实说,我认为我真正需要做的是使用AJAX。 But the examples I've found are confusing and I'm having trouble adapting them to be useful to me. 但是我发现的示例令人困惑,并且我很难对它们进行调整以对我有用。

Here's what I'm wanting to have happen: 这是我想要发生的事情:

  1. Initially load the page with the tabs "empty". 最初使用“空”标签加载页面。 Visually this already happens as my "landing" page is a welcome message with the tabs running across the top that link to the corresponding index. 从视觉上看,这已经发生了,因为我的“着陆”页面是一条欢迎消息,其选项卡位于顶部,并链接到相应的索引。
  2. When a tab is clicked, show that page and load the files for that tab only. 单击选项卡后,显示该页面并仅加载该选项卡的文件。 The files are loaded by calling a PHP function that targets the desired directory and builds a file index. 通过调用针对所需目录的PHP函数并建立文件索引来加载文件。 So I'm wanting to run the PHP function "on demand" without reloading the page itself. 因此,我想“按需”运行PHP函数而无需重新加载页面本身。

Here's an example of the bootstrap tab link: 这是引导标签链接的示例:

<li class="nav-item">
   <a class="nav-link" data-toggle="tab" href="#movies" role="tab">Movies</a>
</li>

I think what I need to do is add an onclick to this link that calls an AJAX function, then that would in turn call the PHP function for the tab that's going to be displayed when the tab is clicked. 我认为我需要做的是向该链接添加一个onclick ,该链接调用一个AJAX函数,然后依次为该选项卡调用PHP函数,该选项卡将在单击该选项卡时显示。

Here's the code I'm currently running in one tab: 这是我当前在一个选项卡中运行的代码:

<?php  
     $allowed = array("mp4", "mkv", "avi", "mov", "mpg", "wmv", "flv", "vob");
     $excluded = array("jpg", "nfo", "htaccess", "db");
     echo php_file_tree("/smb/movies/", "/movies", "https:xxx[link]",$allowed, $excluded);
     unset($allowed, $excluded);
?>

Thinking about wrapping it up in a function like so: 考虑将其包装成如下功能:

<?php  
  function indexmovies() { 
    $allowed = array("mp4", "mkv", "avi", "mov", "mpg", "wmv", "flv", "vob");
    $excluded = array("jpg", "nfo", "htaccess", "db");
    echo php_file_tree("/smb/movies/", "/movies", "https:xxx[link]",$allowed, $excluded);
    unset($allowed, $excluded);
  }
?>

So, to sum it up. 因此,总结一下。 I want the page to load nothing but HTML on load. 我希望页面在加载时仅加载HTML。 Then when a tab is clicked, I want it to switch to that tab and load the above function "on demand". 然后,当单击一个选项卡时,我希望它切换到该选项卡并“按需”加载上述功能。

Will want I want to do work? 想要我想工作吗? If so, what's the BEST way to go about it? 如果是这样,最好的解决方法是什么?

I Used Jquery UI for the tab. 我使用Jquery UI作为选项卡。

HTML / Bootstrap TAB HTML /引导选项卡

<div id="tabs">
    <ul>
        <li><a href="#tabs-1">Tab 1</a>
        </li>
        <li><a href="#tabs-2">Tab 2</a>
        </li>
        <li><a href="#tabs-3">Tab 3</a>
        </li>
    </ul>
    <div id="tabs-1">
        <p>Content for Tab 1</p>
    </div>
    <div id="tabs-2">
        <p>Content for Tab 2</p>
    </div>
    <div id="tabs-3">
        <p>Content for Tab 3</p>
    </div>
</div>

CSS CSS

body {
    background-color: #eef;
}
#tabs {
    width: 95%;
    margin-left: auto;
    margin-right: auto;
    margin-top: 10px;
}

JQUERY JQUERY

$("#tabs").tabs({
    activate: function (event, ui) {
       var active = $('#tabs').tabs('option', 'active');
       if(active == '0'){
        alert('the tab is 0')
        //put your ajax for this tab 0
       }
       else if(active == '1')
       {
        alert('the tab is 1')
        //put your ajax for this tab 1
       }
       else if(active == '2')
       {
        alert('the tab is 2')
        //put your ajax for this tab 2
       }
    }
});

This is the Jsfiddle , Best Regards. 这是的jsfiddle ,最好的问候。

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

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