简体   繁体   English

将Javascript变量转换为jqGrid的postdata

[英]Javascript variable into postdata of jqGrid

Right now I have this postData: { search: function() { return $("#search").val(); },}, 现在我有这个postData: { search: function() { return $("#search").val(); },}, postData: { search: function() { return $("#search").val(); },},

I then have jQuery function above the jqGrid function that performs some logic and produces a string variable. 然后,我在jqGrid函数上方有jQuery函数,该函数执行一些逻辑并产生一个字符串变量。 Right now I am just taking that variable and setting the value of the search element like this: 现在,我只是使用该变量并按如下所示设置search元素的值:

$("#startSearch").click(function() {

  $("#search").val(searchVal);

   $("#grid").trigger("reloadGrid");

});

This works but I was hopping to do this differently. 这可行,但是我希望以不同的方式来做。 I want to just pass the seachVal variable that my jQuery function produces right into the postdata. 我只想将我的jQuery函数产生的seachVal变量直接传递到postdata中。

I tried it like this but its not working postData: { search: function() { return searchVal; },}, 我这样尝试过,但postData无法正常工作: { search: function() { return searchVal; },}, { search: function() { return searchVal; },},

I'm getting an error that says searchVal is not defined. 我收到一条错误消息,说未定义searchVal。 I made sure that the searchVal variable is global but it is still not working. 我确保searchVal变量是全局变量,但仍无法正常工作。

Is this possible or am I just looking at this wrong? 这可能吗,还是我只是看错了?

Any help would be great. 任何帮助都会很棒。

Thanks 谢谢

UPDATE: 更新:

Here is a stripped down version of the page: 这是页面的精简版本:

<fieldset>
<input type='text' id='search' />
<button type='button' id='startSearch'>Search</button>
</fieldset>


<script type="text/javascript">

    $(function(){

            $("#startSearch").click(function() {

                serchVal = 'transID > "5"';

                $("#search").val(serchVal);

                $("#grid").trigger("reloadGrid");


            });


      $("#list").jqGrid({

        url:'data.cfc?method=gridData',
        datatype: 'json',
        mtype: 'POST',

        jsonReader : {
         root: "rows",
         page: "currentpage",
         total: "totalpages",
         records: "totalrecords",
         repeatitems: false,
         id: "0",
       },

      postData: { search: function() { return $("#search").val(); },},      

        colModel :[ 
          {name:'transid', label:'Trans ID', width:60}, 
          {name:'companyname', label:'Company Name', width:245},
          {name:'companycode', label:'Company Code', width:245},
          {name:'datasource', label:'Datasource', width:245}

        ],
        pager: '#pager',
        rowList:[10,50,100],
        rowNum:'10',
        height:221,
        sortname: 'transid',
        sortorder: 'asc',
        viewrecords: true,
        gridview: true,
        caption: 'Get Trans',
        altRows: false,
        autowidth: true,
        forceFit: true,
        rownumbers: true,
        scroll: false,
        sortable: true

      });       

      $("#list").jqGrid('navGrid','#pager',{edit:false,add:false,del:false,search:false,view:true}); 

    }); 

</script>

    <table id="grid"></table>
    <div id="pager"></div>

I suppose the problem exist just because you didn't declared the variable serchVal (you means probably searchVal ). 我想这个问题的存在仅仅是因为您没有声明变量serchVal (您可能意味着searchVal )。 You can try to modify the code to the following 您可以尝试将代码修改为以下内容

$(function () {
    var searchVal = '';

    $("#startSearch").click(function () {
        searchVal = 'transID > "5"';
        $("#grid").trigger("reloadGrid");
    });

    $("#list").jqGrid({
        url: 'data.cfc?method=gridData',
        datatype: 'json',
        mtype: 'POST',
        jsonReader : {
            page: "currentpage",
            total: "totalpages",
            records: "totalrecords",
            repeatitems: false,
            id: "0"
        },
        postData: { search: function () { return searchVal; }},
        colModel: [
            {name: 'transid', label: 'Trans ID', width: 60},
            {name: 'companyname', label: 'Company Name', width: 245},
            {name: 'companycode', label: 'Company Code', width: 245},
            {name: 'datasource', label: 'Datasource', width: 245}
        ],
        pager: '#pager',
        rowList: [10, 50, 100],
        rowNum: 10,
        height: 221,
        sortname: 'transid',
        viewrecords: true,
        gridview: true,
        caption: 'Get Trans',
        autowidth: true,
        rownumbers: true,
        sortable: true
    }).jqGrid('navGrid', '#pager',
        {edit: false, add: false, del: false, search: false, view: true});
});

After that you can remove <input type='text' id='search' /> . 之后,您可以删除<input type='text' id='search' />

Why don't you just wrap your logic in a function 为什么不将逻辑包装在函数中

function SomeLogic() { 
   return("some logic here!");
}

which is going to be called like this: 它将被这样称呼:

postData: { search: function() { return SomeLogic() } },

so you just have to reload the grid 所以你只需要重新加载网格

$("#startSearch").click(function() {
   $("#grid").trigger("reloadGrid");
});

obviously your function SomeLogic can just return searchVal 显然,您的功能SomeLogic可以返回searchVal

function SomeLogic() { 
   return(searchVal);
}

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

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