0

Hi guys I am creating a web app with an editable jqGrid (v4.4) and by default when you are in edit mode (inline editing) and you press the Enter key it invokes the saveRow method of the jqGrid, which actually tries to save the row.

The problem is that I have been using a custom way of saving my rows and I only use saveRow after I do some other stuff first.

I created a keybinding for Enter like this:

$(document).keyup(function (e) {
                if (e.keyCode == 13) {
                    if (lastsel) {
                        jQuery('#list').jqGrid('saveRow', lastsel, false, 'clientArray');
                        var rowData = jQuery('#list').jqGrid('getRowData', lastsel);
                        // alert(rowData);
                       // alert(JSON.stringify(rowData));
                        jQuery.post('Home/Save?inputEmployee=', JSON.stringify(rowData));
                        lastsel = false;
                    }
                }
              });

The problem is that it does both what jqGrid had defined for the binding AND what I do above.

Is there any way I can get rid of the previous binding for Enter before I use mine?

EDIT: I inculed my HomeController Save method:

public void Save(string inputEmployee)
{
    Employee employeeData = JsonConvert.DeserializeObject(inputEmployee.ToString(), typeof(Employee)) as Employee;
    var context = new EmployeeDataContext();
    Employee employee = context.Employees.Single(e => e.ID == employeeData.ID);
    employee = employeeData;
    context.SubmitChanges();
}

So what I do is I have created a LINQ to SQL model and I use it's context to get/set data from/to database. So I thought I'd pass an Employee stringified object as a parameter and then deserialize it with my controller.

4

2 に答える 2

1

要件を実装する最善の方法を選択していないように思われます。投稿したコードから、ローカル編集(editRowurl: "clientArray"のオプション)を使用したいが、行を保存した後に追加のアクションを実行したいことがわかります。この場合、最良の選択はコールバックを使用することです。したがって、の呼び出しは次のようなものになる可能性がありますaftersavefunceditRow

onSelectRow: function (id) {
    var $this = $(this);
    ...
    $this.jqGrid("editRow", id, {
        keys: true,
        url: "clientArray",
        aftersavefunc: function (rowid) {
            $.post("Home/Save",
                JSON.stringify($this.jqGrid("getRowData", rowid)));
        }
    });
    ....
}

Home/SaveどのようにデータをURLに送信したいかわかりませんでした。inputEmployee値なしでパラメーターを使用しました。上記のコードは、正確な要件に対応するように調整できます。

于 2013-02-07T09:27:48.597 に答える
0

You can make use of jQuerys .data() Method to get the existing event Handlers for an element

Basically you

  • Get the old Handler
  • Remove the event
  • Add you Own Handler
    • execute the original handler if neccessary

Heres an Example

    var el = $(document)
    el.on("keydown",function (e) {
       console.log("First Event"); //Set the first Handler
    });

    var h = jQuery._data( document, "events" ).keydown[0].handler; //The (first) Handler

    el.off("keydown",h)

    el.on("keydown",function (e) {
        console.log("Second Handler")
        h(e);
    });

Then the output would look like

//Second Handler
//First Handler

I rember that i used to do this like el.data("events") but that seems deprecated

于 2013-02-07T09:12:59.870 に答える