开发者

Why does focusing on an input field of a jqGrid editable row cause the row to be 'de-selected'?

I've a jqGrid instance which featured with multiselection and inline editing. I wrote some code in the beforeSelectRow event so that the multiple selection can be achieved by ctrl-key, and range selection by shift-key; as well as single selection by without pressing any hotkey. In order to enable the inline editing, I'll call editRow when the row is selected twice. However, I get into two strange problems that:

  1. Clicking on the input element of any field of the editable row, will make the row be 'de-selected'
  2. After re-selecting another row to get into the edit mode, the previous selected row can't be restored even the restoreRow is called on the target row properly.

--EDITED--

Select a row

Why does focusing on an input field of a jqGrid editable row cause the row to be 'de-selected'?

Select a row again to get into edit mode

Why does focusing on an input field of a jqGrid editable row cause the row to be 'de-selected'?

Focus the input box, will 'de-select' the row

Why does focusing on an input field of a jqGrid editable row cause the row to be 'de-selected'?

Select & edit another row, the previous row can't be restored.

Why does focusing on an input field of a jqGrid editable row cause the row to be 'de-selected'?

-- CODE --

        beforeSelectRow: function (rowid, e) {
        var $this = $(this);
        var url_for_save_edit = this.p.editurl;
        var last_selected_row = this.p.selrow;
        var cur_selected_rows = $this.data(key_selected_rows);

        if (true == e.shiftKey) {
            if (rowid != last_selected) {
                var min = parseInt((rowid < last_selected_row ) ? rowid : last_selected_row );
                var max = parseInt((rowid > last_selected_row ) ? rowid : last_selected_row );
                for (var i = min + 1; i <开发者_如何学JAVA; max; i++) {
                    $this.jqGrid(method_setSelection, i, false);
                }
            }
        } else if (false == e.ctrlKey) {
            $this.resetSelection();

            if (!$.string.isNullOrEmpty(url_for_save_edit)) {
                if (!$.isNull(cur_selected_rows) && (cur_selected_rows.length <= 1) && (rowid == last_selected_row)) {
                    var curr_edit_row = $this.data(key_curr_edit_row);
                    console.log('>>>>> curr selected rows = ', cur_selected_rows, ', last selected row = ', last_selected_row, 'curr editing row = ', curr_edit_row, ', to be editable row: ', rowid);
                    if (rowid != curr_edit_row) {
                        if (!$.isNull(curr_edit_row)) {
                            console.log('>>>>> restoring row ', curr_edit_row);
                            $this.jqGrid('restoreRow', curr_edit_row, null);
                        }
                        console.log('>>>>> set row as editable: ', rowid);
                        $this.jqGrid('editRow', rowid, false, null, null, null, null, null, null, null);
                        $this.data(key_curr_edit_row, rowid);
                    }
                }
            }
        }
        return true;
    }


In your code you suppose that the rows are sorted by rowid in asc order (see if (true == e.shiftKey)). I think you should better use sortname and sortorder parameters of jqGrid. Morover you can use jQuery.next or jQuery.prev to get to the next or previous <tr> element.

Moreover I don't understand why you use $this.data(key_selected_rows) instead of the standard this.p.selarrrow.

Sorry, but I would recommend you to rewrite your program and use more DOM properties of <tr> (grid row) and <table> elements. For example the rowIndex property of <tr> could gives you the index of the the row in the rows collection of the <table> element. You can get the next/previous row just using the this.rows[index+1] or this.rows[index+1] (this in the beforeSelectRow point to the DOM representation of the <table> element).

To your main question: if you want to prevent row selection or unselection in some situation you should return false from the beforeSelectRow.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜