简体   繁体   English

展开/折叠全部带有嵌套的手风琴和非手风琴的商品:BOOTSTRAP

[英]Expand/Collapse All with a nested accordion and non-accordion item: BOOTSTRAP

I'm about 2 months new to programming and Twitter Bootstrap, so please be kind! 我刚刚接触编程和Twitter Bootstrap大约2个月,所以请客气!

I've done a lot of digging and searching, but am not sure I am using the correct terms so I figure I'd ask directly. 我已经做了很多挖掘和搜索工作,但是不确定我是否使用了正确的术语,因此我想直接问一下。

I am trying to create an Expand/Collapse all button that will expand or collapse all items under a Title Header. 我正在尝试创建一个“全部展开/折叠”按钮,以展开或折叠“标题标题”下的所有项目。 The items under a title header include a text piece and an image. 标题标题下的项目包括文本和图像。 I want to be able to expand/collapse the text piece by clicking on the Title Header. 我希望能够通过单击标题标题来展开/折叠文本。 I want to be able to expand/collapse the text AND the image by clicking on the button. 我希望能够通过单击按钮来扩展/折叠文本和图像。

The problem is: I want to button to expand/collapse everything regardless if the text piece is expanded or collapsed. 问题是:无论文本片段是展开还是折叠,我都想通过按钮来展开/折叠所有内容。

This is the jsfiddle: http://jsfiddle.net/mcfly303/XXXYn/1/ 这是jsfiddle: http : //jsfiddle.net/mcfly303/XXXYn/1/

As you can see, the button will toggle items open or closed opposite of their current state. 如您所见,该按钮将在与当前状态相反的状态下切换打开或关闭的项目。 Is there a way to do a "universal" expand/collapse, regardless if the nested items are expanded or collapsed? 有没有一种方法可以进行“通用”展开/折叠,而不管嵌套项目是展开还是折叠?

Basically I'd like the "toggle all" button to result only in a) the title or b) the title and the image. 基本上,我希望“全部切换”按钮仅产生a)标题或b)标题和图像。

Thanks in advance for any help!!!! 在此先感谢您的帮助!!!

my html: 我的html:

<div class="well sidebar-nav" id="sidebar">
    <ul class="nav nav-list">
        <li><a href="#" class="expandcollapse">Toggle All</a>

        </li>
    </ul>
</div>
<div class="accordion" id="accordion1">
    <div class="accordion-group">
        <div class="row-fluid">
           <div class="span9">
                <div class="accordion-heading"> <a class="accordion-toggle" data-toggle="collapse" data-parent="#accordion1" href="#collapseOne">
                                    <h2>COOL HAND LUKE</h2>
                    </a>

                </div>
            </div>
        </div>
        <div id="collapseOne" class="accordion-body collapse">
            <div class="accordion-inner">
               <div class="mainList"> <a href="#">Paul Newman Movies</a>  <a href="#"> <i class="icon-white icon-th-list"></i></a>

                </div>
            </div>
        </div>
    </div>
</div>

<div id="collapseMain" class="accordion-body collapse in">
    <div class="accordion-inner">
        <div class="row-fluid">
            <div class="span12">
                <img src="http://www.samefacts.com/wp-content/uploads/2012/12/CoolHandLuke_135Pyxurz.jpg" alt="">

            </div>
        </div>
    </div>
</div>

my javascript: 我的JavaScript:

$('.expandcollapse').click(function () {

    $('.collapse').each(function (index) {
        $(this).collapse("toggle");
    });
});

EDIT: So I partially figured it out. 编辑:所以我部分想通了。 jsfiddle: http://jsfiddle.net/mcfly303/XXXYn/4/ I split to button into two separate buttons to clearly label each function I want to achieve.However, there is still a kink. jsfiddle: http : //jsfiddle.net/mcfly303/XXXYn/4/我将按钮拆分为两个单独的按钮,以清晰地标记要实现的每个功能。但是,仍然存在一个纽结。 Namely on loading, the FIRST CLICK on the Title View button will toggle the text open and the picture closed. 即在加载时,“标题视图”按钮上的“第一次单击”将切换文本打开和图片关闭。 And also on load, the first click on the Title and Image button will toggle the text open and keep the image open. 同样在加载时,第一次单击“标题和图像”按钮将使文本切换为打开状态并使图像保持打开状态。

After the first click, everything works fine, which to clarify (and edit my misstatement above) is: Click on TITLE VIEW - collapses all except the Title. 第一次单击后,一切工作正常,可以澄清(并在上面编辑我的错误陈述)是:单击TITLE VIEW-折叠除Title以外的所有内容。 Click on the Title will expand/collapse the text piece Click on TITLE AND IMAGE VIEW - expands to Title and Image. 单击标题将展开/折叠文本。单击标题和图像视图-展开为标题和图像。 Click on the Title will expand/collapse the text piece 单击标题将展开/折叠文本

If anyone can please help with this last kink of the "first click problem" I would really appreciate it! 如果有人可以帮助您解决“首次点击问题”的最后一个环节,我将不胜感激!

Thanks. 谢谢。

Updated JS: 更新的JS:

var isCollapsed = false;
$('.expandcollapse').click(function () {
  var collapseCommand = isCollapsed ? "show" : "hide";

  $('.collapse').each(function () {  
    $(this).collapse(collapseCommand);
  });

  isCollapsed = !isCollapsed;
});

I also added the in class to #collapseOne so the page displays correctly initially. 我也将in类添加到#collapseOne以便页面最初正确显示。

Edit: 编辑:

New code based on your edit: 根据您的编辑的新代码:

$('.titleView').click(function () {
  $('#collapseOne, #collapseMain').collapse('hide');
});

$('.fullStubView').click(function () {
  $('#collapseOne, #collapseMain').collapse('show');
});

Unless I'm missing something, seems like this solves the problem. 除非我缺少任何东西,否则似乎可以解决问题。 jsFiddle 的jsfiddle

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

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