jQgrid 在加载时发布自定义数据 [英] jQgrid posting custom data on load

查看:16
本文介绍了jQgrid 在加载时发布自定义数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

想知道是否有人使用 jQgrid 在同一页面上发布来自另一个表单的动态数据.动态的,因为我不知道要发布的输入名称,但宁愿在渲染网格时只发布整个序列化表单.

我已经在 postData 中设置了额外的数据,但它没有正确传递到 url,因为它看起来是双 url 编码的.见:

$(document).ready(function() {$("#rpt").jqGrid({ url:'/get.json',postData: {filter: $('form').serialize()},数据类型:json",网格视图:是的,colModel:[id:'col1']});});

通过此处和其他站点上的各种线程,我尝试了表单上建议的 JSON.stringify 和 serializearray() 以及自定义函数,但均无济于事.表单数据显示为已编码,无法通过 _GET 在另一端使用.

任何建议都会很棒 - 谢谢!

解决方案

我不知道你想从服务器端的表单中以哪种形式获取数据.不过,我建议您以以下形式使用 postData

postData: {过滤器:函数(){var 结果 = {},我,项目,formInfo = $('form#myForm').serializeArray(),l = formInfo.length;对于 (i = 0; i < l; i++) {项目 = formInfo[i];结果[item.name] = item.value;}返回 JSON.stringify(结果);}}

如果是以下测试表格

<form id="myForm"><div><input type="text" name="a" value="1 from a" id="a"/></div><div><input type="text" name="b" value="2 from b" id="b"/></div><div><input type="hidden" name="c" value="3 from c" id="c"/></div>

<textarea name="d" rows="8" cols="40">4</textarea></div><div><选择名称="e"><option value="5" selected="selected">5</option><option value="6">6</option><option value="7">7</option></select></div>

result 变量将是

var result = {一个:来自一个",b:来自 b 的 2",c:来自 c 的 3",d:4",e:5"}

因此不会进行数据转换.然后我建议使用 JSON.stringify 将对象 result 转换为 JSON 字符串.(取决于可能不需要的服务器代码.)所以 filters 参数将被发送为

{"a":"1 from a","b":"2 from b","c":"3 from c","d":"4","e":"5"}

您可以使用 FiddlerFirebug 检查相应小演示.

Wondering if anyone has used jQgrid to post dynamic data from another form on the same page. Dynamic in that I don't know the input names to post, but would rather just post the whole serialized form when rendering the grid.

I gotten as far as setting the extra data in postData, but it doesn't get passed in the url properly, as it appears double url-encoded. See:

$(document).ready(function() {
  $("#rpt").jqGrid( 
  { url:'/get.json',
   postData: {filter: $('form').serialize()}, 
  datatype: "json", 
  gridview: true,
  colModel:[id:'col1']
 });
});

Through various threads here and on other sites, I've tried the suggested JSON.stringify and serializearray() on the form, as well as custom functions to no avail. The form data appears encoded and is not available on the other side via _GET.

Any suggestions would be great - thanks!

解决方案

I am not sure in which form you want to get the data from the form on the server side. Nevertheless I would suggest you to use postData in the following form

postData: {
    filter: function () {
        var result = {}, i, item,
            formInfo = $('form#myForm').serializeArray(),
            l = formInfo.length;
        for (i = 0; i < l; i++) {
            item = formInfo[i];
            result[item.name] = item.value;
        }

        return JSON.stringify(result);
    }
}

In case of the following test form

<form id="myForm">
  <div><input type="text" name="a" value="1 from a" id="a" /></div>
  <div><input type="text" name="b" value="2 from b" id="b" /></div>
  <div><input type="hidden" name="c" value="3 from c" id="c" /></div>
  <div>
    <textarea name="d" rows="8" cols="40">4</textarea>
  </div>
  <div><select name="e">
    <option value="5" selected="selected">5</option>
    <option value="6">6</option>
    <option value="7">7</option>
  </select></div>
  <div>
    <input type="checkbox" name="f" value="8" id="f" />
  </div>
</form>

The result variable will be

var result = {
    a: "1 from a",
    b: "2 from b",
    c: "3 from c",
    d: "4",
    e: "5"
}

So no conversion of the data will be done. Then I suggest to convert object result to JSON string using JSON.stringify. (Depend on the server code it could be not needed.) So the filters parameter will be sent as

{"a":"1 from a","b":"2 from b","c":"3 from c","d":"4","e":"5"}

You can use Fiddler or Firebug to examine the HTTP traffic of the corresponding small demo.

这篇关于jQgrid 在加载时发布自定义数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆