2

除了在 stackoverflow 上折叠 Bootstrap 可折叠之外的所有工作,我将不胜感激一些建议。

any opened itemclickedto 时collapse,其类按以下顺序变化:panel-collapse collapse in> panel-collapse collapsing> panel-collapse collapse in。在正确的顺序中,它应该导致panel-collapse collapse collapsed. 这还没有解决。问题是它collapse被调用了两次。

对此有什么解决方案,以便我可以将其用作模板的一部分?

更新:只有一个panel-group可以独占打开。现在通过删除解决data-parent了这个问题,它现在允许同时打开多个面板。

我添加了登录bootstrap.js(第 608-628 行):

  // COLLAPSE DATA-API
  // =================

  $(document).on('click.bs.collapse.data-api', '[data-toggle=collapse]', function (e) {
    var $this   = $(this), href
    var target  = $this.attr('data-target')
        || e.preventDefault()
        || (href = $this.attr('href')) && href.replace(/.*(?=#[^\s]+$)/, '') //strip for ie7
    var $target = $(target)
    var data    = $target.data('bs.collapse')
    var option  = data ? 'toggle' : $this.data()
    var parent  = $this.attr('data-parent')
    var $parent = parent && $(parent)

    console.log("A",data);
    if (!data || !data.transitioning) {console.log("B1");
      if ($parent) { console.log("B2");
          $parent.find('[data-toggle=collapse][data-parent="' + parent + '"]').not($this).addClass('collapsed')
      }
      console.log("B3",$target.hasClass('in'));
      $this[$target.hasClass('in') ? 'addClass' : 'removeClass']('collapsed')
    }
    console.log("C", option);
    $target.collapse(option)
    console.log("D");
  })

结果日志:

a)collapse第一次按下时,它看起来很乱

bootstrap.js:622 A undefined
bootstrap.js:623 B1
bootstrap.js:624 B2
bootstrap.js:627 B3 false
bootstrap.js:630 C Object {toggle: "collapse", bind: "text:displayLabel, attr:{href:idhash}"}
bootstrap.js:632 D
bootstrap.js:622 A Collapse {$element: jQuery.fn.jQuery.init[1], options: Object, transitioning: 1}
bootstrap.js:630 C toggle
bootstrap.js:632 D

b)collapse任何其他时间按下时

bootstrap.js:622 A Collapse {$element: x.fn.x.init[1], options: Object, transitioning: 0}
bootstrap.js:623 B1
bootstrap.js:624 B2
bootstrap.js:627 B3 true
bootstrap.js:630 C toggle
bootstrap.js:632 D
bootstrap.js:622 A Collapse {$element: jQuery.fn.jQuery.init[1], options: Object, transitioning: 1}
bootstrap.js:630 C toggle
bootstrap.js:632 D

正确的输出应该是:

a)collapse第一次按下时

bootstrap.js:622 A undefined
bootstrap.js:623 B1
bootstrap.js:624 B2
bootstrap.js:627 B3 false
bootstrap.js:630 C Object {toggle: "collapse", bind: "text:displayLabel, attr:{href:idhash}"}
bootstrap.js:632 D

b)collapse任何其他时间按下时

bootstrap.js:622 A Collapse {$element: jQuery.fn.jQuery.init[1], options: Object, transitioning: 0}
bootstrap.js:623 B1
bootstrap.js:624 B2
bootstrap.js:627 B3 (true or false)
bootstrap.js:630 C toggle
bootstrap.js:632 D

完整代码:

var confItems = {};
var childrenLength = 2;
confItems["children"] = new Array(childrenLength);
for (var i = 0; i < childrenLength; i++) {
  confItems.children[i] = {};
  confItems.children[i]["idhash"] = "#col-" + (i + 1);
  confItems.children[i]["id"] = "col-" + (i + 1);
  confItems.children[i]["displayLabel"] = "Item " + (i + 1);
  confItems.children[i]["children"] = new Array(childrenLength);
  for (var j = 0; j < childrenLength; j++) {
    confItems.children[i].children[j] = {};
    confItems.children[i]["idhash"] = "#col-" + (i + 1) + "-" + (j + 1);
    confItems.children[i]["id"] = "col-" + (i + 1) + "-" + (j + 1);
    confItems.children[i].children[j]["displayLabel"] = "Item " + (i + 1) + "." + (j + 1);
    confItems.children[i].children[j]["children"] = new Array(0);
  }
}
var viewModel = function() {
  this.tree = ko.observable(confItems);
};
ko.applyBindings(new viewModel());
<link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.0.3/css/bootstrap.min.css">
<link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.0.3/css/bootstrap-theme.min.css">
<script src="https://code.jquery.com/jquery-1.10.2.js"></script>
<script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.1.1/js/bootstrap.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.2.0/knockout-min.js"></script>
<div data-bind="template: {name:'largeTemplate', data: tree}"></div>
<script id='largeTemplate' type='text/html'>
  <div class="panel-group">
	<div id="accordion" class="panel panel-default" data-bind="foreach: children">
      <div class="panel-heading">
        <h4 class="panel-title">
			<a data-toggle="collapse" data-parent="#accordion" data-bind="text:displayLabel, attr:{href:idhash}">Lorem</a>
		</h4>
      </div>
      <div data-bind="attr:{id:id}" class="panel-collapse collapse">
        <div class="panel-body" data-bind="foreach: children">
          <div class="panel-heading">
            <a data-bind="text:displayLabel">Donec</a>
          </div>
        </div>
      </div>
    </div>
  </div>
</script>

4

1 回答 1

0

一个便宜的解决方案是修改bootstrap.js和删除'[data-toggle=collapse]'.

我仍在寻找造成这种情况的原因以及发生了什么,所以我不必求助于修改bootstrap.js. 即使在修改之后,也会collapse被调用两次。

对此我还没有找到答案。

  // COLLAPSE DATA-API
  // =================

  $(document).on('click.bs.collapse.data-api', function (e) {
    var $this   = $(this), href
    var target  = $this.attr('data-target')
        || e.preventDefault()
        || (href = $this.attr('href')) && href.replace(/.*(?=#[^\s]+$)/, '') //strip for ie7
    var $target = $(target)
    var data    = $target.data('bs.collapse')
    var option  = data ? 'toggle' : $this.data()
    var parent  = $this.attr('data-parent')
    var $parent = parent && $(parent)

    console.log("A",data);
    if (!data || !data.transitioning) {console.log("B1");
      if ($parent) { console.log("B2");
          $parent.find('[data-toggle=collapse][data-parent="' + parent + '"]').not($this).addClass('collapsed')
      }
      console.log("B3",$target.hasClass('in'));
      $this[$target.hasClass('in') ? 'addClass' : 'removeClass']('collapsed')
    }
    console.log("C", option);
    $target.collapse(option)
    console.log("D");
  })

更新日志:

bootstrap.js:622 A null
bootstrap.js:623 B1
bootstrap.js:627 B3 false
bootstrap.js:630 C Object {}
bootstrap.js:632 D
bootstrap.js:622 A null
bootstrap.js:623 B1
bootstrap.js:627 B3 false
bootstrap.js:630 C Object {keycount: 0}
bootstrap.js:632 D
于 2016-01-06T18:07:24.027 回答