开发者

How to compute a column value while editing in jqgrid

开发者 https://www.devze.com 2023-04-02 17:36 出处:网络
I have a supposedly common problem to solve (done easily with other grid controls I\'m familiar with).

I have a supposedly common problem to solve (done easily with other grid controls I'm familiar with). In jqgrid, i'm quite stumped. I have a jqgrid with inline editing enabled. 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. 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 -

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. The only way to get a cell value seems to be to us开发者_开发知识库e a custom regex user function that strips out that value. 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? 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.

            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. There is a .each() loop for find the matching row node. 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.


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



}
0

精彩评论

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