0

I have two jqGrids ("in_table", and "out_table") that are identical except for their data. Thanks to help I received on this post, I now understand how to add a customizable button. When the button is pushed, I'd like to delete the row from the table and add it to the other.

The following code, which is called when the button is pushed, is unpredictable - it works for a while and then it stops working!
The console shows an error:

Uncaught TypeError: Cannot read property 'name' of undefined 

Code:

function sign_in_out_action(myself,rowid,icol,cellcontent,e){
    var this_row = myself.getRowData(rowid);
    if( in_out_button_content(cellcontent)== "In"){
    alert('Signing OUT');
    this_row.in_out = "Out";
    $('#out_table').jqGrid('addRowData',1,this_row);
    myself.delRowData(rowid);
    }
    else{
    if( in_out_button_content(cellcontent)== "Out"){
        alert('Signing in');
        this_row.in_out = "In";
        $('#in_table').jqGrid('addRowData',1,this_row);
        myself.delRowData(rowid);
    }
    else{
        alert("what?  "+in_out_button_content(cellcontent));
    }
    }

It would seem pretty straightforward to delete and add data. I would appreciate any insight into what I am doing wrong.

Community
  • 1
  • 1
jpl
  • 38
  • 1
  • 1
  • 6

1 Answers1

0

I think you can define two functions , one is adding a row for a grid ,another is deleting a row of a grid.

Then you can add a click function for a custom button.

// delete row
function deleteRow(tableId, rowId) {
        $('#' + tableId).jqGrid('delRowData', rowId);
        return false;
}


    var index = 999;
// add a row
    function addRow(tableId, c1, c2) {
        var datarow = { Id: c1, Name: c2 };

        var lastsel2 = index;
        index++;
        var su = jQuery('#' + tableId).addRowData(lastsel2, datarow, 'last');
        if (su) {
            jQuery('#' + tableId).jqGrid('editRow', 0, true);
        }
    }

The c1, c2 are column values.(You may have five columns, as you can define c1,c2,c3,c4,c5).

The following code is for getting cell value:

var c1 = $('#' + tableId).getCell(rowId, 'Id');
var c2 = $('#' + tableId).getCell(rowId, 'Name');
keep fool
  • 101
  • 4
  • I see that you are getting the cell values one-by-one, using the getCell method. But why is that necessary? Shouldn't my line: var this_row = myself.getRowData(rowid); get the object of the all of the row data at once? Also, what is 'id' and 'index' in your code? Are they globals? Thanks for looking at my problem! – jpl Aug 08 '12 at 19:27
  • I'm so sorry for the bad code. Index is a global value, id variable is not required. And here I declared index as 999 to instead the max id from a grid, also you can write your own function like "GetMaxId()" to get a different id from other rows in one grid. – keep fool Aug 09 '12 at 00:45