捕获jqGrid中下拉列表的更改/单击事件

时间:2012-11-08 12:16:14

标签: javascript asp.net javascript-events jqgrid

我的下拉菜单上的点击事件不会触发。我没有看到警报,当我在Firebug中添加断点时,它没有到达断点。如果我断点那么它确实会触及点击和双击事件。

具体来说,当我:

  1. 双击以编辑一行
  2. 选择客户下拉并选择其他内容
  3. 转到另一行
  4. 然后是行

    alert("I changed");
    

    不运行。

    我不知道是不是因为当我移动到另一条线时,该值会被恢复(即使我已经注释了' restoreRow'方法)

    当我的下拉值发生变化时(无需更改焦点),我是否使用正确的事件进行捕捉

    感谢您的协助。

    $(document).ready(
        function () {
            // This is executed as soon as the DOM is loaded and before the page contents are loaded
            var lastsel;
            // $ is short for JQuery which is in turn a super overloaded function that does lots of things.
            // # means select an element by its ID name, i.e. below we have <table id="ts"
            $("#ts").jqGrid({
                //=============
                // Grid Setup
                url: 'Timesheet/GridData/',
                datatype: 'json',
                mtype: 'GET',
                pager: $('#pager'),
                rowNum: 30,
                rowList: [10, 20, 30, 40, 80],
                viewrecords: true,
                imgpath: '/Content/themes/base/images',
                caption: 'Timesheet',
                height: 450,
                // Column definition
                colNames: ['hCustomer_ID', 'hProject_ID', 'hTask_ID', 'Date', 'Customer', 'Project', 'Task', 'Description', 'Hours', '$'],
                colModel: [
                  { name: 'hCustomer_ID', index: 'hCustomer_ID', editable: false, hidden: true },
                  { name: 'hProject_ID', index: 'hProject_ID', editable: false, hidden: true },
                  { name: 'hTask_ID', index: 'hTask_ID', editable: false, hidden: true },
                  { name: 'tsdate', index: 'tsdate', width: 80, editable: true, datefmt: 'yyyy-mm-dd' },
                  { name: 'Customer', index: 'Customer', width: 250, align: 'left', editable: true, edittype: "select",
                      editoptions: { dataUrl: 'Timesheet/CustomerList' },
                      dataEvents: [
                        {
                            type: 'click',
                            fn: function (e) {
                                alert("I changed");
                            }
                        }]
                  },
                  { name: 'Project', index: 'Project', width: 250, align: 'left', editable: true, edittype: "select", editoptions: { dataUrl: 'Timesheet/ProjectList'} },
                  { name: 'Task', index: 'Task', width: 250, align: 'left', editable: true, edittype: "select", editoptions: { dataUrl: 'Timesheet/TaskList'} },
                  { name: 'Desc', index: 'Desc', width: 300, align: 'left', editable: true },
                  { name: 'Hours', index: 'Hours', width: 50, align: 'left', editable: true },
                  { name: 'Charge', index: 'Charge', edittype: 'checkbox', width: 18, align: 'center', editoptions: { value: "0:1" }, formatter: "checkbox", formatoptions: { disabled: false }, editable: true }
                ],
                //=============
                // Grid Events
                // when selecting, undo anything else
                onSelectRow: function (rowid, iRow, iCol, e) {
                    if (rowid && rowid !== lastsel) {
                        // $(this).jqGrid('restoreRow', lastsel);
                        lastsel = rowid;
                    }
                },
                // double click to edit
                ondblClickRow: function (rowid, iRow, iCol, e) {
                    // browser independent stuff
                    if (!e) e = window.event;
                    var element = e.target || e.srcElement
    
                    // When editing, change the drop down datasources to filter on the current parent
                    $(this).jqGrid('setColProp', 'Project', { editoptions: { dataUrl: 'Timesheet/ProjectList?Customer_ID=' + $(this).jqGrid('getCell', rowid, 'hCustomer_ID')} });
                    $(this).jqGrid('setColProp', 'Task', { editoptions: { dataUrl: 'Timesheet/TaskList?CustomerProject_ID=' + $(this).jqGrid('getCell', rowid, 'hProject_ID')} });
    
                    // Go into edit mode (automatically moves focus to first field)
                    // Use setTimout to apply the focus and datepicker after the first field gets the focus
                    $(this).jqGrid(
                        'editRow',
                        rowid,
                        {
                            keys: true,
                            oneditfunc: function (rowId) {
                                setTimeout(function () {
                                    $("input, select", element).focus();
                                    $("#" + rowId + "_tsdate").datepicker({ dateFormat: 'yy-mm-dd' });
                                }, 50);
                            }
                        }
                    );
    
                },  // end ondblClickRow event handler
                postData:
                    {
                        startDate: function () { return $('#startDate').val(); }
                    }
            }); // END jQuery("#ts").jqGrid
    
            $("#ts").jqGrid('navGrid', '#pager', { view: false, edit: false, add: false, del: false, search: false });
            $("#ts").jqGrid('inlineNav', "#pager");
    
        });                  // END jQuery(document).ready(function () {
    

    修订后的工作代码:

    $(document).ready(
        function () {
            // This is executed as soon as the DOM is loaded and before the page contents are loaded
            var lastsel;
            // $ is short for JQuery which is in turn a super overloaded function that does lots of things.
            // # means select an element by its ID name, i.e. below we have <table id="ts"
            $("#ts").jqGrid({
                //=============
                // Grid Setup
                url: 'Timesheet/GridData/',
                datatype: 'json',
                mtype: 'GET',
                pager: $('#pager'),
                rowNum: 30,
                rowList: [10, 20, 30, 40, 80],
                viewrecords: true,
                caption: 'Timesheet',
                height: 450,
                // Column definition
                colNames: ['hCustomer_ID', 'hProject_ID', 'hTask_ID', 'Date', 'Customer', 'Project', 'Task', 'Description', 'Hours', '$'],
                colModel: [
                  { name: 'hCustomer_ID', index: 'hCustomer_ID', editable: false, hidden: true },
                  { name: 'hProject_ID', index: 'hProject_ID', editable: false, hidden: true },
                  { name: 'hTask_ID', index: 'hTask_ID', editable: false, hidden: true },
                  { name: 'tsdate', index: 'tsdate', width: 80, editable: true, datefmt: 'yyyy-mm-dd' },
                  { name: 'Customer', index: 'Customer', width: 250, align: 'left', editable: true, edittype: "select",
                      editoptions: {
                          dataUrl: 'Timesheet/CustomerList',
                          dataEvents: [
                          {
                            type: 'change',
                            fn: function (e) {
                                alert("I changed");
                                }
                          }]
                      }
                  },
                  { name: 'Project', index: 'Project', width: 250, align: 'left', editable: true, edittype: "select", editoptions: { dataUrl: 'Timesheet/ProjectList'} },
                  { name: 'Task', index: 'Task', width: 250, align: 'left', editable: true, edittype: "select", editoptions: { dataUrl: 'Timesheet/TaskList'} },
                  { name: 'Desc', index: 'Desc', width: 300, align: 'left', editable: true },
                  { name: 'Hours', index: 'Hours', width: 50, align: 'left', editable: true },
                  { name: 'Charge', index: 'Charge', edittype: 'checkbox', width: 18, align: 'center', editoptions: { value: "0:1" }, formatter: "checkbox", formatoptions: { disabled: false }, editable: true }
                ],
                //=============
                // Grid Events
                // when selecting, undo anything else
                onSelectRow: function (rowid, iRow, iCol, e) {
                    if (rowid && rowid !== lastsel) {
                        // $(this).jqGrid('restoreRow', lastsel);
                        lastsel = rowid;
                    }
                },
                // double click to edit
                ondblClickRow: function (rowid, iRow, iCol, e) {
                    // browser independent stuff
                    if (!e) e = window.event;
                    var element = e.target || e.srcElement
    
                    // When editing, change the drop down datasources to filter on the current parent
                    $(this).jqGrid('setColProp', 'Project', { editoptions: { dataUrl: 'Timesheet/ProjectList?Customer_ID=' + $(this).jqGrid('getCell', rowid, 'hCustomer_ID')} });
                    $(this).jqGrid('setColProp', 'Task', { editoptions: { dataUrl: 'Timesheet/TaskList?CustomerProject_ID=' + $(this).jqGrid('getCell', rowid, 'hProject_ID')} });
    
                    // Go into edit mode (automatically moves focus to first field)
                    // Use setTimout to apply the focus and datepicker after the first field gets the focus
                    $(this).jqGrid(
                        'editRow',
                        rowid,
                        {
                            keys: true,
                            oneditfunc: function (rowId) {
                                setTimeout(function () {
                                    $("input, select", element).focus();
                                    $("#" + rowId + "_tsdate").datepicker({ dateFormat: 'yy-mm-dd' });
                                }, 50);
                            }
                        }
                    );
    
                },  // end ondblClickRow event handler
                postData:
                    {
                        startDate: function () { return $('#startDate').val(); }
                    }
            }); // END jQuery("#ts").jqGrid
    
            $("#ts").jqGrid('navGrid', '#pager', { view: false, edit: false, add: false, del: false, search: false });
            $("#ts").jqGrid('inlineNav', "#pager");
    
        });                   // END jQuery(document).ready(function () {
    

1 个答案:

答案 0 :(得分:1)

此外,还不清楚为什么在inlineNav内使用 editRowondblClickRow的人工调用?如果您要选择行,然后单击导航器中的按钮,则不会调整dataUrl

您描述的主要问题的原因是dataEvents的错误使用。在editoptions的文档中,您会发现dataEvents属于editoptions的属性,例如dataUrl。目前,您已将dataEvents放在colModel的错误位置。

我建议你另外阅读the answer,其中描述了如何使用dataUrl哪些参数取决于当前选定的行。另外,我建议您验证是否在Cache-Control的回复中设置了dataUrl(请参阅the answer)。或者,您可以使用here描述的其他选项。