在编辑表单中添加自定义按钮

时间:2014-04-15 06:06:36

标签: jquery jqgrid

我在jqGrid编辑表单中添加了一个自定义按钮,使用 beforeShowForm 事件创建。单击自定义按钮我想执行 ADD 操作。 下面是我的代码..

 beforeShowForm: function(form) { 
          //Create Custom ADD Button
          $('<a href="#">Add<span class="ui-icon ui-icon-disk"></span></a>')
            .click(function() {
                var $self = $(this);
               $self.jqGrid("editGridRow", $self.jqGrid("getGridParam", "selrow"),
                {
                  editData: {//Function to Add parameters to the status 
                    oper: 'add', //trying to pass parameter to server
                  },
                });
            }).addClass("fm-button ui-state-default ui-corner-all fm-button-icon-left")
              .prependTo("#Act_Buttons>td.EditButton");

守则
 editData: {//Function to Add parameters to the status oper: 'add', //trying to pass parameter to server },

显示 TypeError:a(...)[0] .p未定义错误。知道如何实现这个??

Add Button

1 个答案:

答案 0 :(得分:2)

代码中的主要错误是您使用this的地方。 beforeShowForm内部的this初始化为网格的DOM,但在click事件处理程序内部$('<a href="#">Add<span class="ui-icon ui-icon-disk"></span></a>')初始化为<a>元素。所以你得到错误“TypeError:a(...)[0] .p is undefined”。要修复所需的代码,只需在外部回调函数$this 中移动初始化beforeShowForm变量:

beforeShowForm: function(form) { 
    var $self = $(this); // !!! place it here !!!

    //Create Custom ADD Button
    $('<a href="#">Add<span class="ui-icon ui-icon-disk"></span></a>')
        .click(function() {
            $self.jqGrid("editGridRow", $self.jqGrid("getGridParam", "selrow"), {
                editData: {//Function to Add parameters to the status 
                    oper: 'add', //trying to pass parameter to server
                },
            });
        }).addClass("fm-button ui-state-default ui-corner-all fm-button-icon-left")
          .prependTo("#Act_Buttons>td.EditButton");
}