折叠在手风琴模板

时间:2016-01-06 16:36:28

标签: jquery twitter-bootstrap knockout.js twitter-bootstrap-3 knockout-templating

除了在stackoverflow上折叠Bootstrap可折叠以外的所有工作,我会很感激一些建议。

any opened itemclickedcollapse时,其类别按以下顺序更改: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());
&#13;
<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>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分: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