[英]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:我试过了:
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 ):有几个选项(这里建议前两个):
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 后执行。
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]'))
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] } );
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.