knockout.js 和 jQueryUI 创建手风琴菜单
尝试让 jquery UI 和 knockout js 协作时遇到了一个小问题。基本上我想创建一个手风琴,其中的项目是通过 foreach (或模板)从淘汰赛中添加的。
基本代码如下:
<div id="accordion">
<div data-bind="foreach: items">
<h3><a href="#" data-bind="text: text"></a></h3>
<div><a class="linkField" href="#" data-bind="text: link"></a></div>
</div>
</div>
这里没有什么令人印象深刻的...问题是,如果我执行以下操作:
$('#accordion').accordion();
将创建手风琴,但内部 div 将是标头选择器(第一个子级,默认情况下),因此效果不是想要一个。
用这个修复东西:
$('#accordion').accordion({ header: 'h3' });
似乎工作得更好,但实际上创建了 2 个手风琴,而不是一个有 2 个部分的......奇怪。
我尝试探索淘汰模板并使用“afterRender”重新手风琴化 div,但无济于事......它似乎仅将第一个链接重新渲染为手风琴,而不是第二个链接。无论如何,这可能是由于我对 jquery UI 的初学者了解。
您知道如何使一切协同工作吗?
Got a slight problem trying to have jquery UI and knockout js to cohoperate. Basically I want to create an accordion with items being added from knockout through a foreach (or template).
The basic code is as follows:
<div id="accordion">
<div data-bind="foreach: items">
<h3><a href="#" data-bind="text: text"></a></h3>
<div><a class="linkField" href="#" data-bind="text: link"></a></div>
</div>
</div>
Nothing impressive here... The problem is that if I do something like:
$('#accordion').accordion();
The accordion will be created but the inner div will be the header selector (first child, as default) so the effect is not the wanted one.
Fixing stuff with this:
$('#accordion').accordion({ header: 'h3' });
Seems to work better but actually creates 2 accordions and not one with 2 sections... weird.
I have tried to explore knockout templates and using "afterRender" to re-accordionise the div but to no avail... it seems to re-render only the first link as an accordion and not the second. Probably this is due to my beginner knowldge of jquery UI anyway.
Do you have any idea how to make everything work together?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
我会使用自定义绑定来实现此类功能。
就像 RP Niemeyer 一样,有一个 jQuery Accordion 绑定到 knockoutjs 的示例 http://jsfiddle.net/rniemeyer/MfegM/
I would go with custom bindings for such functionality.
Just like RP Niemeyer with an example of jQuery Accordion binding to knockoutjs http://jsfiddle.net/rniemeyer/MfegM/
我曾尝试集成淘汰赛和 JQuery UI 手风琴,后来又集成了 Bootstrap 可折叠手风琴。在这两种情况下它都有效,但我发现我必须实施一些解决方法才能使所有内容正确显示,特别是在通过淘汰赛动态添加元素时。提到的小部件并不总是知道淘汰赛中发生的情况,并且事情可能会变得混乱(div 高度计算错误等......)。特别是使用 JQuery 手风琴时,它往往会重写 html,因为它认为合适,这可能是一个真正的痛苦。
因此,我决定使用核心 JQuery 和 Knockout 制作自己的手风琴小部件。看看这个工作示例: http://jsfiddle.net/matt_friedman/KXgPN/
当然,使用不同的标记和 CSS,这可以根据您的需要进行定制。
好处是它完全是数据驱动的,并且除了您决定使用的任何 CSS 之外,不会对布局做出任何假设。您会注意到标记非常简单。这只是一个例子。它的目的是定制。
标记:
JavaScript:
I had tried to integrate knockout and the JQuery UI accordion and later the Bootstrap collapsible accordion. In both cases it worked, but I found that I had to implement a few workarounds to get everything to display correctly, especially when dynamically adding elements via knockout. The widgets mentioned aren't always aware of what is happening with regards to knockout and things can get messed up (div heights wrongly calculated etc...). Especially with the JQuery accordion it tends to rewrite the html as it sees fit, which can be a real pain.
So, I decided to make my own accordion widget using core JQuery and Knockout. Take a look at this working example: http://jsfiddle.net/matt_friedman/KXgPN/
Of course, using different markup and css this could be customized to whatever you need.
The nice thing is that it is entirely data driven and doesn't make any assumptions about layout beyond whatever css you decide to use. You'll notice that the markup is dead simple. This is just an example. It's meant to be customized.
Markup:
Javascript:
有什么原因不能将折叠小部件应用到此处的内部 div 吗?例如:
Is there any reason why you can't apply the accordion widget to the inner div here? For example:
我尝试了公认的解决方案并且它有效。只需要进行一些更改,因为我收到以下错误,
只需添加以下内容即可,
详细信息请参阅 淘汰赛手风琴绑定断裂
I attempted the accepted solution and it worked. Just had to make a little change since i was getting following error
just had to add following and it worked
for details please see Knockout accordion bindings break
您可以尝试将其模板化,类似于:
“Tasks()”是一个 ko.observableArray,其中填充了任务,并带有属性
“TaskId”、“TaskName”、“Description”、“SelectedTask”声明为 ko.observable();
“我的手风琴”是
You could try this to template it, similar to this:
"Tasks()" is a ko.observableArray with populated with task-s, with attributes
"TaskId", "TaskName","Description", "SelectedTask" declared as ko.observable();
"myAccordion" is a
我所做的是,由于我的数据是从 AJAX 加载的,并且我显示了一个“正在加载”微调器,所以我将手风琴附加到 ajaxStop,如下所示:
工作完美。
What I did was, since my data was being loaded from AJAX and I was showing a "Loading" spinner, I attached the accordion to ajaxStop like so:
Worked perfectly.