【问题标题】:Delete row from one jqGrid and add it to another从一个 jqGrid 中删除行并将其添加到另一个
【发布时间】:2012-08-05 04:10:00
【问题描述】:

我有两个相同的 jqGrid(“in_table”和“out_table”),除了它们的数据。感谢我在this post 上收到的帮助,我现在了解如何添加可自定义的按钮。按下按钮时,我想从表中删除该行并将其添加到另一行。

按下按钮时调用的以下代码是不可预测的 - 它会工作一段时间然后停止工作!
控制台显示错误:

Uncaught TypeError: Cannot read property 'name' of undefined 

代码:

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));
    }
    }

删除和添加数据似乎很简单。如果能深入了解我做错了什么,我将不胜感激。

【问题讨论】:

    标签: javascript jquery ajax jquery-plugins jqgrid


    【解决方案1】:

    我认为你可以定义两个函数,一个是为网格添加一行,另一个是删除一行。

    然后可以为自定义按钮添加点击功能。

    // 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);
            }
        }
    

    c1、c2 是列值。(您可能有五列,因为您可以定义 c1、c2、c3、c4、c5)。

    以下代码用于获取单元格值:

    var c1 = $('#' + tableId).getCell(rowId, 'Id');
    var c2 = $('#' + tableId).getCell(rowId, 'Name');
    

    【讨论】:

    • 我看到您正在使用 getCell 方法一个接一个地获取单元格值。但为什么这是必要的?我的行不应该是: var this_row = 我自己.getRowData(rowid);一次获取所有行数据的对象?另外,您的代码中的“id”和“index”是什么?他们是全球性的吗?感谢您查看我的问题!
    • 我很抱歉代码不好。 index 是全局值,不需要 id 变量。在这里,我将索引声明为 999,而不是网格中的最大 id,您也可以编写自己的函数,如“GetMaxId()”,以从一个网格中的其他行获取不同的 id。
    猜你喜欢
    • 2011-08-17
    • 2010-12-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多