简体   繁体   English

在jqgrid中编辑时如何计算列值

[英]How to compute a column value while editing in jqgrid

I have a supposedly common problem to solve (done easily with other grid controls I'm familiar with). 我有一个应该解决的常见问题(与我熟悉的其他网格控件轻松完成)。 In jqgrid, i'm quite stumped. 在jqgrid中,我很困惑。 I have a jqgrid with inline editing enabled. 我有一个启用了内联编辑的jqgrid。 I would like to add some scripting so that when editing a row (or adding a new row), the value of ColumnC is automatically computed as ColumnA * ColumnB as default. 我想添加一些脚本,以便在编辑一行(或添加新行)时,默认情况下自动将ColumnC的值计算为ColumnA * ColumnB。 The user can change the values in any column at any time. 用户可以随时更改任何列中的值。 I want this value to be computed as soon as the user enters it and not wait till the data is saved. 我希望用户输入后立即计算该值,而不要等到数据保存后再计算。

My approach so far has been to attach a dataEvent of type "change" to ColumnA and ColumnB - 到目前为止,我的方法是将类型为change的dataEvent附加到ColumnA和ColumnB上-

dataEvents: [
              { type: 'change', fn: function(e) {
                   var rowid = $("#myGrid").jqGrid('getGridParam','selrow');
                   var rowData = $("#myGrid").getRowData(rowid); 
                   var cell1 = rowData['ColumnA'];
                   var cell2 = rowData['ColumnB'];
                   var newValue = cell1 * cell2;
                   $("#myGrid").jqGrid('setCell', rowid, 'ColumnC', newValue);
                   } 
               },
          ]

Obviously, cell1 & cell2 don't actually return the value but the whole cell content as already discovered by other users here How to get a jqGrid cell value . 显然,cell1和cell2实际上并不返回该值,而是整个单元格内容,如其他用户在此处已获得的如何获取jqGrid单元格值 The only way to get a cell value seems to be to use a custom regex user function that strips out that value. 获取单元格值的唯一方法似乎是使用自定义的regex用户函数来去除该值。 Apart from that, is there a better/alternate way to achieve what I need? 除此之外,是否有更好/替代的方法来实现我所需要的? Something as simple as jqGrid - How to calculated column to jqgrid? jqGrid一样简单-如何计算jqgrid的列? though obviously that won't cut it for me since it will only displaying data and user cannot update it. 尽管显然这对我来说不会有用,因为它只会显示数据,而用户无法更新它。

Any help would be appreciated. 任何帮助,将不胜感激。

UPDATE : After guidance from Oleg, I was able to extend getTextFromCell to support what I need. 更新 :在Oleg的指导下,我能够扩展getTextFromCell来支持我所需要的。

            function getTextFromCell(cellNode) {
            var cellValue;
            //check for all INPUT types
            if (cellNode.childNodes[0].nodeName == "INPUT") {              
                //special case for handling checkbox
                if (cellNode.childNodes[0].type == "checkbox") {            
                    cellValue = cellNode.childNodes[0].checked.toString();  
                }
                else {
                    //catch-all for all other inputs - text/integer/amount etc
                    cellValue = cellNode.childNodes[0].value;               
                }
            }
            //check for dropdown list
            else if (cellNode.childNodes[0].nodeName == "SELECT") {         
                var newCell = $("select option:selected", cellNode);
                cellValue = newCell[0].value;
            }
            return cellValue;             
        }

        function getCellNodeFromRow(grid,rowId,cellName) {
            var i = getColumnIndexByName(grid, cellName);
            var cellValue;
            //find the row first
           $("tbody > tr.jqgrow", grid[0]).each(function() {
                //The "Id" column in my grid is at index 2...
                var idcell = $("td:nth-child(2)", this); 
                var currRowId = getTextFromCell(idcell[0])
                if (currRowId == rowId) {
                    cellValue = getTextFromCell($("td:nth-child(" + (i + 1) + ")", this)[0]);
                    return false;
                }
            });
            return cellValue;
        }

The code in getCellNodeFromRow is not the most efficient. getCellNodeFromRow的代码不是最有效的。 There is a .each() loop for find the matching row node. 有一个.each()循环用于查找匹配的行节点。 I can see this being slow when the grid has thousands of rows. 当网格具有数千行时,我可以看到这很慢。 Is there a better/more efficient way to find the row node? 有没有更好/更有效的方法来找到行节点? I have the row Id already. 我已经有了该行的ID。

Look at the demo from the answer . 答案演示 It uses cell editing, but the same technique work for inline editing too. 它使用单元格编辑,但是相同的技术也可以用于内联编辑。 Just click on any cell in the "Amount" column and modify the numerical value. 只需单击“金额”列中的任何单元格,然后修改数值。 You will see that the value in the "Total" row (footer) will be changed dynamically during the cell is still in the editing mode. 您将看到,在单元格仍处于编辑模式期间,“总计”行(页脚)中的值将动态更改。 I think it is what you need. 我认为这就是您所需要的。

    you can achieve this using onCellSelect event of jqgrid as below



//global section
var columnA;
var ColumnB;
var ColumnC;
var currentRow;

//

onCellSelect: function (rowid, iCol, aData) {

                currentRow = rowid;
                var ColumnA = $('#grid').getCell(rowid, 'MyCol');
                var ColumnB = $('#grid').getCell(rowid, 'MyCol');

                 $("#grid").jqGrid('editRow', rowid, true );
                $("#myMenu").hide();
                if (rowid && rowid !== lastsel) {
                    if (lastsel) jQuery('#grid').jqGrid('restoreRow', lastsel);
$("#grid").jqGrid('editRow', rowid, true );
                    lastsel = rowid;
                }
                else if (rowid && rowid === lastsel)
                { $("#grid").jqGrid('editRow', rowid, true ); }

               //if it is in editable mode 
               // when you view the html using firebug it will be like the cell id change to                       
               //format like "rowid_ColumnName"
               $('#' + currentRow + '_ColumnC').val(ColumnA*ColumnB);

               // or you can use achieve this using following jqgrid method at 
               //appropriate  place
               $("#myGrid").jqGrid('setCell', rowid, 'ColumnC', ColumnA*ColumnB);



}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM