简体   繁体   English

jqGrid-将表单数据提交到jqGrid并让jqGrid使用这些参数获取数据

[英]jqGrid - Submit form data to jqGrid and have jqGrid fetch data using those params

I've got a jqGrid in which I have a custom button like: 我有一个jqGrid,其中有一个自定义按钮,例如:

.jqGrid('navButtonAdd', '#pager', {
        caption: "", buttonicon: "ui-icon-note",
        onClickButton: function () {

            //Show Modal Dialog Form
            $('#selectParams').dialog().show();

            //Load Grid
            $("#grid").setGridParam({ datatype: 'json' }).trigger('reloadGrid', [{ page: 1 }]);

        }, position: "last", title: "Specify Parameters", cursor: "pointer"

And a modal dialog that looks like: 模态对话框如下所示:

<div id="selectParams" hidden="hidden">
    <form action="/Controller/Action" method="get">
        Start Date <input type="date" name="startDate" /><br/>
        End Date <input type="date" name="endDate" /><br/>
        Value 1 <input type="text" name="val1" /><br/>
        Value 2 <input type="text" name="val2" /><br/>
        <input type="submit" value="Submit" />
    </form>
</div>

Instead of the form making a request to the server, I need to have it pass these parameters (startDate, endDate, va1, val2) to the jqGrid somehow and have the grid make a request to the server passing these values (ideally as a POST request). 而不是向服务器发送请求的表单,我需要让它以某种方式将这些参数(startDate,endDate,va1,val2)传递给jqGrid,并让网格向服务器发送这些值的请求(理想情况下为POST)请求)。

Just as a side note, the grid is initially loaded with datatype: 'local' so it doesn't fetch anything. 只是附带说明,网格最初加载的datatype: 'local'因此它不会获取任何内容。 When the form values are selected and submitted, the grid is reloaded with datatype: 'json' to fetch values from the server. 选择并提交表单值后,将使用datatype: 'json'重新加载网格以从服务器获取值。

Any leads on how to go about doing that would be really appreciated. 任何有关如何做到这一点的线索将不胜感激。 Thanks! 谢谢!

Figured it out. 弄清楚了。 Perhaps not in the best way but it does work well enough to suit my needs: 也许不是最好的方法,但是它确实可以满足我的需求:

I changed the form to: 我将表格更改为:

<div id="selectParams" hidden="hidden">
    <form>
        Start Date <input type="date" id="startDateId"/><br/>
        End Date <input type="date" id="endDateId" /><br/>
        Value 1 <input type="text" id="val1Id" /><br/>
        Value 2 <input type="text" id="val2Id" /><br/>
    </form>
</div>

Added some global javascript variables: 添加了一些全局javascript变量:

//Global Variables
var startDate;
var endDate;
var val1;
var val2;

And changed the custom button as follows: 并如下更改自定义按钮:

.jqGrid('navButtonAdd', '#pager', {
            caption: "", buttonicon: "ui-icon-note",
            onClickButton: function () {

                $('#selectParams').dialog({
                    modal: true,
                    draggable: false,
                    resizable: false,
                    title: 'Specify Parameters',
                    width: 225,
                    height: 325,
                    buttons: {
                        "Submit": function () {
                            //Get values and pass them as parameters with the jqGrid AJAX request
                            startDate = $('#startDateId').val();
                            endDate = $('#endDateId').val();
                            val1 = $('#val1Id').val();
                            val2 = $('#val2Id').val();
                            $('#grid').setGridParam({ postData: { startDate: startDate, endDate: endDate, val1 : val1, val2: val2 } });

                            $(this).dialog("close");
                        }
                    },
                    close: function () {
                        //Load grid if parameters are filled out
                        if (startDate != undefined && endDate != undefined && val1 != undefined && val2 != undefined) {
                            $("#grid").setGridParam({ datatype: 'json' }).trigger('reloadGrid', [{ page: 1 }]);
                        }
                    }
                }).show();

            }, position: "last", title: "Specify Parameters", cursor: "pointer"

Hopefully this helps someone else. 希望这可以帮助其他人。 Cheers! 干杯!

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

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