jqGrid筛选器工具栏初始默认值

我正在使用jqGrid和filter工具栏,我需要为其中一个字段设置初始默认filter值,以便默认情况下只显示状态为“打开”的行,但如果需要,用户可以显示“已关闭”行。

目前我有一个像这样的解决方法

setTimeout(function() {$('#gs_Status').val('Open');$("#eventsGrid")[0].triggerToolbar()},500);

但它导致第二个请求,真的很糟糕。

有人知道怎么做这个吗?

编辑 :更多的研究告诉我这可能是不可能的:(

您是否查看了jqGrid文档维基中的工具栏搜索和附加网格方法 ? 看起来您可以使用filterToolbar设置filter,并使用triggerToolbar设置filter。 我自己没有尝试过,但是一旦为网格加载了数据,你就可以在loadComplete执行此操作。

这有帮助吗?

有几个步骤可以做到这一点:

  1. 在列模型搜索选项中传递默认值
  2. 防止默认表单数据加载过程
  3. 在表准备好时触发filter工具栏数据加载

更详细一点:

将网格数据类型设置为local(这可以防止初始数据加载)并设置搜索选项的默认值:

  $("#mytable").jqGrid({ datatype: "local", colNames:['Keyword','Selected'], colModel:[ {name:'keyword', sortable:true, searchoptions: { defaultValue:'golf' } }, {name:'selected', sortable:false, searchoptions: { } }, .... .... 

然后添加filter工具栏,设置数据类型和URL,并触发加载:

  $('#mytable').jqGrid('filterToolbar', {autosearch: true}); $('#mytable').setGridParam({datatype: 'json', url:'/get_data.php'}); $('#mytable')[0].triggerToolbar(); 

我读到的所有提示都不适合我。 所以我尝试了很多,并在jqGrid源代码中做了一些研究。 如果使用beforeRequest事件,则集成“默认值”或“已保存的搜索值”function要容易得多。

我不得不解决一些问题:

  1. 虽然它是beforeRequest事件,但是在调用triggerToolbar之前,不会使用您在此处设置的搜索参数。
  2. triggerToolbar不仅使用新的搜索参数设置新请求,还会触发表数据的重新加载 – 但之前的请求仍在运行,因此您执行相同的请求两次(两者都使用新的搜索参数)。
  3. 设置默认值但允许用户清除/覆盖它们。
  4. 避免无限循环并保留旧的搜索function。

这是代码:

 beforeRequest: function () { // Activate filter toolbar and define "beforeSearch" callback // to avoid a second search request on page load, triggered // by "triggerToolbar()" when not set. // // Important: // "beforeSearch" has to return true to avoid the second // request on page load, but it has to return false for all // following searches (otherwise it wouldn't be possible to // submit new searches by pressing Enter in search input fields). $("#myTable").jqGrid('filterToolbar', { beforeSearch: function(){ if ($(this).data('firstSearchAbortedFlag') != '1') { $(this).data('firstSearchAbortedFlag', '1'); return true; } // Return false or add your customizations here... return false; } }); if ($(this).data('defaultValuesSetFlag') != '1') { // Set flag to set default only the first time when // the page is loaded. $(this).data('defaultValuesSetFlag', '1'); // Set default values... // (id = 'gs_' + fieldname) $('#gs_field_a').val('value a'); $('#gs_field_b').val('value b'); // Call "triggerToolbar" to use the previously set // parameters for the current search. // // Important: // Set "beforeSearch" callback for "filterToolbar" to avoid // a second search request, triggered by "triggerToolbar". $("#myTable")[0].triggerToolbar(); } } 

我希望这可以帮助你!

我以不同的方式修复它然后上面的答案,我滥用beforeRequest函数将初始默认值添加到post数据。

与jqGrid的创建者联系,默认值选项仅用于高级搜索。

我写了一个可以在onBeforeRequest函数上设置的函数,该函数将获取搜索选项中的所有默认值并将其附加到post数据。 因此默认值将添加到初始请求中。

为了确保您仍然可以使用onbeforeRequest事件,我在代码的末尾更改了onBeforeRequest(this.p.beforeRequest = function(){})。 您可以将其更改为您想要的任何内容并将其调用。 下次你将要求使用该函数并且此函数将被解除(因为覆盖)。

 function() { var defaultSearchOptions = []; var colModel = this.p.colModel; // loop trough each column and check if they have an default value in search options // and add them to the array $.each(colModel, function (index, column) { if (column.hasOwnProperty('searchoptions')) { var searchOptions = column.searchoptions; if (searchOptions.hasOwnProperty('defaultValue')) { defaultSearchOptions[defaultSearchOptions.length++] = { ""field"": column.index, ""op"": ""bw"", ""data"": searchOptions.defaultValue }; } } }); // if there are any default search options retrieve the post data if (defaultSearchOptions.length > 0) { var postData = this.p.postData; var filters = {}; // check if post data already has filters if (postData.hasOwnProperty('filters')) { filters = JSON.parse(postData.filters); } var rules = []; // check if filtes already has rules if (filters.hasOwnProperty('rules')) { rules = filters.rules; } // loop trough each default search option and add the search option if the filter rule doesnt exists $.each(defaultSearchOptions, function (defaultSearchOptionindex, defaultSearchOption) { var ruleExists = false; $.each(rules, function (index, rule) { if (defaultSearchOption.field == rule.field) { ruleExists = true; return; } }); if (ruleExists == false) { rules.push(defaultSearchOption); } }); filters.groupOp = 'AND'; filters.rules = rules; // set search = true postData._search = true; postData.filters = JSON.stringify(filters); } this.p.beforeRequest = function() { // your before request function here }; this.p.beforeRequest.call(this); } 

希望这可以帮助

在阅读Ziege的回答后 ,我想到了那里发生的事情。 我想出了一种更简单的方法来在第一个请求进入服务器之前初始化默认值。

这是一个完整的工作示例。 这个想法是有一个列filter,下拉状态,“活动”和“关闭”,我希望默认为“活动”。 代码有评论来解释发生了什么:

 $('#deals').jqGrid({ colNames: ['...','Status','...'], colModel: [ { ... }, // Use the defaultValue attribute to set your defaults in the searchOptions object { name: 'Status', stype: 'select', searchoptions: { defaultValue: 'Active', value: {"":"All","Active":"Active","Closed":"Closed",}, sopt: [ 'eq'] }, width: 60 }, { ... } ], // Here's where we intercept each server request to cancel it if it's the first one. // Returning false from this method causes the request to the server to be aborted. beforeRequest: function () { // Define a local reference to the grid var $requestGrid = $(this); // Check a data value for whether we've completed the setup. // This should only resolve to true once, on the first run. if ($requestGrid.data('areFiltersDefaulted') !== true) { // Flip the status so this part never runs again $requestGrid.data('areFiltersDefaulted', true); // After a short timeout (after this function returns false!), now // you can trigger the search setTimeout(function () { $requestGrid[0].triggerToolbar(); }, 50); // Abort the first request return false; } // Subsequent runs are always allowed return true; }, url: 'Url/to/your/action', datatype: 'json', mtype: 'POST', pager: '#deals-pager', rowNum: 15, sortname: 'Status', sortorder: 'desc', viewrecords: true, height: '100%' }).jqGrid('filterToolbar', { stringResult: true }); 

这也适用于不支持local数据类型的Lib.Web.Mvc库(.NET)。

如果您有多个网格,或者想将beforeRequest逻辑移动到库以进行共享,只需将其定义为独立函数并在网格设置中按名称引用它:

 function jqGridFilterSetupRequestHandler = function () { var $requestGrid = $(this); if ($requestGrid.data('areFiltersDefaulted') !== true) { $requestGrid.data('areFiltersDefaulted', true); setTimeout(function () { $requestGrid[0].triggerToolbar(); }, 50); return false; } return true; } $('#deals').jqGrid({ colNames: ['...','Status','...'], colModel: [ { ... }, // Use the defaultValue attribute to set your defaults in the searchOptions object { name: 'Status', stype: 'select', searchoptions: { defaultValue: 'Active', value: {"":"All","Active":"Active","Closed":"Closed",}, sopt: [ 'eq'] }, width: 60 }, { ... } ], beforeRequest: jqGridFilterSetupRequestHandler, url: 'Url/to/your/action', datatype: 'json', mtype: 'POST', pager: '#deals-pager', rowNum: 15, sortname: 'Status', sortorder: 'desc', viewrecords: true, height: '100%' }).jqGrid('filterToolbar', { stringResult: true });