简体   繁体   English

Web2py - 如何在 ajax 之后确定负载控制的结束

[英]Web2py - How to determine end of load control after ajax

I run a ajax call that updates interpolated value as a fully built selectbox element.我运行一个 ajax 调用,将内插值更新为一个完全构建的选择框元素。

I would like to transform the created/returned selectbox into a chosen selectbox once it has finished fully loading otherwise it reverts back to a regular selectbox.一旦完成完全加载,我想将创建/返回的选择框转换为选定的选择框否则它会恢复为常规选择框。

I have tried:我试过了:

  • Listening to 'DOMSubtreeModified'(and other similar events) and then activating chosen lines but, these run too often and I have to know when the selectbox with all options are fully initiated .收听“DOMSubtreeModified”(和其他类似事件),然后激活所选行,但是,这些行运行得太频繁,我必须知道何时完全启动带有所有选项的选择框。 I can force initiation on each change, it's terrible - resource consuming and just plain wrong.我可以强制启动每个更改,这很糟糕 - 消耗资源并且完全错误。
  • Initiate some callback on ajax return('ajax:complete' event) but, again - this does not guarantee the html controls are fully initiated as stated in this answer (and tested).在 ajax return('ajax:complete' event) 上启动一些回调,但是,再次 - 这并不能保证 html 控件完全启动,如本答案所述(并经过测试)。
  • I've tried to set a onload event for the select control.我试图为选择控件设置一个 onload 事件。

Main lines of code:主要代码行:

JavaScript call: JavaScript 调用:

ajax('{{=URL('controller_name', 'func_name')}}', ['param1'], 'target_div');

Python controller returns(this returns a select control with option objects initiated in it and overrides the target_div inner html): Python 控制器返回(这将返回一个选择控件,其中启动了选项对象并覆盖了 target_div 内部 html):

return SELECT(distinct_values, _id = 'selectbox_id' , _multiple = 'true' , _class='SelectBoxSingleDisabled');

Looking for a web2py oriented solution.寻找面向 web2py 的解决方案。 No brute force/hacky stuff if possible.如果可能,不要使用蛮力/hacky 的东西。 thanks!谢谢!

There are several options (the first two are suggested here ):有几个选项(这里建议前两个):

  1. In the controller, add the Chosen initialization code to response.js -- this will be executed after the returned HTML is added to the DOM.在控制器中,将 Chosen 初始化代码添加到response.js —— 这将在返回的 HTML 添加到 DOM 后执行。

  2. Add the Chosen initialization code to a script element after the select element:将 Chosen 初始化代码添加到 select 元素之后的脚本元素中:

     CAT(SELECT(distinct_values, _id = 'manual_group_selectbox' , _multiple = 'true' , _class='SelectBoxSingleDisabled'), SCRIPT('[Chosen code]'))
  3. The third argument to the ajax() function can be a Javascript function that takes the data returned by the server. ajax()函数的第三个参数可以是一个 Javascript 函数,它接受服务器返回的数据。 So, you could write a function that adds the returned HTML to the DOM and then initializes Chosen:因此,您可以编写一个函数,将返回的 HTML 添加到 DOM 中,然后初始化 Chosen:

     ajax( '{{=URL('controller_name', 'func_name')}}', ['param1'], function(html) { [add html to DOM] [initialize Chosen] } );
  4. Set up a jQuery .ajaxSuccess() event handler, which should run after the ajax() function updates the DOM.设置一个 jQuery .ajaxSuccess()事件处理程序,它应该在ajax()函数更新 DOM 之后运行。

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

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