【问题标题】:jqGrid DatePicker filtering without hitting Enter keyjqGrid DatePicker 过滤而不按 Enter 键
【发布时间】:2011-10-16 03:01:03
【问题描述】:

我正在构建我的第一个 ASP.NET MVC 3 应用程序并使用 jqGrid。我的其中一个列“Flavor Created”是一个日期列,我想使用 DatePicker 过滤该列上的网格。这是当前发生的情况:用户单击列标题过滤器框,显示日期选择器,然后用户选择年、月并单击一天。选择器离开并在文本框中留下日期,例如 2009 年 3 月 28 日。要真正让过滤器工作,我必须单击该框并按 Enter 键,这对用户来说有点烦人。

有没有办法让过滤器在用户当天点击时自动触发?

(顺便说一句,我不确定“完成”按钮的用途是什么,因为每当我点击一天时选择器就会消失。也许这是我缺少的设置。) em>

还有其他人需要这个功能并解决了吗?

我试图做这样的事情:

dataInit: function (elem) {
  $(elem).datepicker({ changeYear: true, changeMonth: true, showButtonPanel: true,
    onSelect: function (dateText, inst) {
       $("#icecreamgrid")[0].trigger("reloadGrid");
    }
  })
}

正如我在某些网站上看到有人建议的那样,但这似乎不起作用。

【问题讨论】:

    标签: jqgrid


    【解决方案1】:

    你可以试试

    dataInit: function (elem) {
        $(elem).datepicker({
            changeYear: true,
            changeMonth: true,
            showButtonPanel: true,
            onSelect: function() {
                if (this.id.substr(0, 3) === "gs_") {
                    // in case of searching toolbar
                    setTimeout(function(){
                        myGrid[0].triggerToolbar();
                    }, 50);
                } else {
                    // refresh the filter in case of
                    // searching dialog
                    $(this).trigger("change");
                }
            }    
        });
    }
    

    更新:从 4.3.3 版本开始,jqGrid 将网格的 DOM 初始化为 thisdataInit。因此不需要在上面的代码中使用myGrid 变量。而不是那个可以使用:

    dataInit: function (elem) {
        var self = this; // save the reference to the grid
        $(elem).datepicker({
            changeYear: true,
            changeMonth: true,
            showButtonPanel: true,
            onSelect: function() {
                if (this.id.substr(0, 3) === "gs_") {
                    // in case of searching toolbar
                    setTimeout(function () {
                        self.triggerToolbar();
                    }, 50);
                } else {
                    // refresh the filter in case of
                    // searching dialog
                    $(this).trigger("change");
                }
            }    
        });
    }
    

    使用dataInit 的第二个options 参数进行免费jqGrid 调用,其中包含附加信息,例如mode 属性。 mode 属性的值是 "filter" 在过滤工具栏内部调用的情况下(和"search" 在搜索对话框的情况下)。因此可以使用以下代码

    dataInit: function (elem, options) {
        var self = this; // save the reference to the grid
        $(elem).datepicker({
            changeYear: true,
            changeMonth: true,
            showButtonPanel: true,
            onSelect: function() {
                if (options.mode === "filter") {
                    // in case of searching toolbar
                    setTimeout(function () {
                        self.triggerToolbar();
                    }, 0);
                } else {
                    // refresh the filter in case of
                    // searching dialog
                    $(this).trigger("change");
                }
            }    
        });
    }
    

    【讨论】:

    • 这很好奥列格。谢谢。点击 Esc 会导致 GET 发生,但我可以忍受 - 我不会撤回那么多数据。我也许可以在调用 triggerToolbar() 之前调用一个检查函数。
    • 或许您最好在onSelect 内部而不是onClose 内部调用triggerToolbar?您可以准确描述在哪种情况下要强制工具栏搜索,以及在哪种情况下不强制。一般来说,onSelect 是用户选择新日期时调用的事件。
    • @itsmatt:不客气!我将代码从我的答案直接更改为更好的方法。
    • @itsmatt:我想了一下这个问题并尝试做了一个演示。现在我认为像我在这里描述的那样使用onSelect 在高级搜索和过滤器工具栏的使用情况下并不是最好的。我会在周末更准确地检查这个问题,稍后会发布更多信息。
    • @itsmatt:我又修改了一次答案。现在它适用于两种搜索。
    猜你喜欢
    • 1970-01-01
    • 2016-10-25
    • 2017-08-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-18
    • 1970-01-01
    相关资源
    最近更新 更多