简体   繁体   English

更改引导程序切换按钮的状态

[英]changing state of bootstrap toggle button

I have a button: 我有一个按钮:

<button type="button" class="btn btn-sm btn-primary" data-bind="attr: { 'data-target': '#MoreOptions' + Provider() }" data-toggle="collapse">More Options</button>

That controls when this div is open or closed to show 'more options'. 控制该div打开或关闭以显示“更多选项”的时间。

<div class="panel-collapse" data-bind="attr: { id: 'MoreOptions' + Provider() }, css: { collapse: $root.IsCollapsed }">---</div>

And I use a Knockout.js observable to keep it open when the data refreshes inside the div. 当数据在div中刷新时,我使用可观察的Knockout.js使其保持打开状态。 I don't want a data refresh to collapse the div. 我不想刷新数据来折叠div。 The issue is when I do this and keep the div open, the button thinks that the div is closed. 问题是当我这样做并保持div打开时,按钮认为div已关闭。 So the next time the user clicks the button, the div closes real quick and opens back up again, requiring two clicks on the button to get the div to close. 因此,下次用户单击该按钮时,该div会快速关闭并再次打开,需要两次单击该按钮才能使div关闭。

The expected behavior is to keep the div open during a data refresh, but have the button know that the div is open and close it with just one click. 预期的行为是在数据刷新期间使div保持打开状态,但是使按钮知道div处于打开状态,并且只需单击一下即可将其关闭。

I've set the CSS collapse with knockout in the div and I'm looking for a similar way to set the button for it to know that the div is not collapsed. 我已经在div中使用击倒设置了CSS折叠,我正在寻找一种类似的方法来设置按钮以使其知道div没有折叠。

I've noticed that when the button is clicked to expand the div, the aria-expanded attribute is set to true where it didn't exist when the button is initially rendered; 我注意到,当单击按钮以扩展div时,aria-expanded属性设置为true(在最初呈现按钮时不存在该属性的地方); and when the button is clicked to collapse the div, the CSS property 'collapsed' is added to the button. 当单击按钮以折叠div时,会将CSS属性“ collapsed”添加到按钮。 I've played around with adding and changing these via knockout with no success. 我一直在尝试通过淘汰赛添加和更改这些方法,但没有成功。

It should be noted that the button is refreshed with the data. 应当注意的是,按钮是用数据刷新的。

I've decided to remove the bootstrap toggle button functionality and just go with the jQuery toggle in combination with knockout. 我决定删除引导程序切换按钮功能,而仅将jQuery切换器与剔除结合使用。 The jQuery way just offers more control. jQuery方式仅提供更多控制。

<button type="button" class="btn btn-sm btn-primary" 
        data-bind="click: $root.ChangeMoreOptions">
    More Options
</button>

<div id="MoreOptionsDiv" style="padding-top: 10px; display: none;">
--- content ---
</div>

Knockout: 昏死:

 //Open or close div
 self.ChangeMoreOptions = function () {
     if (self.IsCollapsed() === true) {
         $('#MoreOptionsDiv').slideToggle('slow', function () {
             // Animation complete.
         });
         self.IsCollapsed(false);
     } else {
         $('#MoreOptionsDiv').slideToggle('slow', function () {
             // Animation complete.
         });
         self.IsCollapsed(true);
     }
 }

 //Keep div open on refresh if it is already open
 //Search for 'none' in the style display: none
 self.FixMoreOptions = function () {
     if (($('#MoreOptionsDiv').attr('style').search('none') > -1) 
        && (self.IsCollapsed() === false)) {
         //Toggle closed div back to open
         $('#MoreOptionsDiv').slideToggle('slow', function () {
             // Animation complete.
         });
     }
 }

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

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