jQgrid在加载时发布自定义数据

jQgrid在加载时发布自定义数据

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

问题描述

想知道是否有人使用jQgrid从同一页面上的其他表单发布动态数据.动态的,我不知道要发布的输入名称,而是在渲染网格时仅发布整个序列化表格.

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.

我已经尽力在postData中设置了额外的数据,但是它并没有正确地传递到url中,因为它似乎是双重url编码的.参见:

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']
 });
});

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

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!

推荐答案

我不确定您要以哪种形式从服务器端的表单中获取数据.不过,我建议您使用以下格式的postData

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);
    }
}

如果使用以下测试表格

<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>

result变量将为

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

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

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"}

您可以使用提琴手 Firebug 检查相应的演示.

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

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

08-24 09:32