jsonfying选择框选项 [英] jsonfying the select box options
问题描述
我有一个服务器端查询,该查询通常以
I've a server side query which generically generated all select box options in the form
<option val='1'> text1 </option>
<option val='2'> text2 </option>
<option val='3'> text3 </option>
<option val='4'> text4 </option>
<option val='5'> text5 </option>
现在,我想将所有这些转换为jqgrid可以理解的下拉菜单格式.即{1:text1,2:text2 ....}条件是我不会触摸服务器端代码.我需要在客户端通过添加泛型函数对其进行修改.现在,我通过一个jQuery ajax调用获得了这个
Now I want to convert all of this to a format that jqgrid understands for its dropdowns. i.e. {1:text1, 2:text2....} Condition is that I would not touch the server side code. I need to modify it here at client side by adding a generic function. Now I get this through an jquery ajax call
getGridDropDown: function (url) {
$.ajax({
type: "GET",
url: url,
dataType: "html",
success: function (html) {
$(html).find('option').each(function(key){
alert(key)
})
},
error: function () {
console.log("Error in ajax call to url: " + url);
}
});
},
现在我尝试了各种格式,唯一的方法看起来像使用regexp是唯一的方法.我不能将html返回变量作为jquery变量来处理,在这里我可以说$ this.val()+ this.text()
Now I tried various formats, the only way looks like using regexp is the only way. Can't I handle the html return variable as a jquery variable, where I can say $this.val() + this.text()
推荐答案
如果服务器可以在<select>
内部生成<options>
的列表,则可以直接在
If your server can generate the list of <options>
inside of <select>
you can use directly dataUrl
in the editoptions or searchoptions.
如果您的服务器只能生成<options>
没有 <select>
和</select>
的列表,并且您无法更改服务器端的行为,则可以使用buildSelect
进行修复问题:
If your server can only generate the list of <options>
without <select>
and </select>
and you can't change the behavior on the server side you can use buildSelect
to fix the problem:
editoptions: {
dataUrl: 'yourUrl',
buildSelect: function (data) {
return "<select>" + data + "</select>";
}
}
(在使用旧版本的jqGrid的情况下,可能需要测试typeof(data)
并使用data
或data.responseText
)
(in case of usage of old version of jqGrid it could be required to test the typeof(data)
and use data
or data.responseText
)
也可能需要使用ajaxSelectOptions: { cache: false }
(请参见此处)
The usage of ajaxSelectOptions: { cache: false }
could be also required (see here)
这篇关于jsonfying选择框选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!