【问题标题】:updating a cell value based on another cell value in kendo grid asp.net mvc根据剑道网格asp.net mvc中的另一个单元格值更新单元格值
【发布时间】:2017-04-17 16:14:45
【问题描述】:

我已经为这个问题苦苦挣扎了两天,非常感谢任何帮助。我有一个剑道网格,在其中我给了网格 excel 类似的功能,即在点击输入时,可编辑的列被突出显示,我可以输入值并在选项卡上移动到下一个单元格。我有一个称为外部金额的列,它是可编辑的,即用户在单元格中输入值,下一列是差值,每当用户在外部金额列中输入值并点击输入时,都应该计算该差值。

差异-InternalLocalAmt-ExternallocalAmt。 InternalLocalAmt 已填充且不可编辑。

代码sn-p:

@(Html.Kendo().Grid(Model)
    .Name("OutputCashGrid")
    .Columns(columns =>
    {

        columns.Bound(p => p.InternalLocalAmt).Width(130);
        columns.Bound(p => p.ExternalLocalAmt).Width(130);
  columns.Bound(p => p.LocalDifference).Title("Difference").Width(130).Format("{0:N}").HtmlAttributes(new{id="DifferenceVal"});
 })
  .Sortable()
     .ColumnMenu()
     .Scrollable(scr => scr.Height(430))
     .Filterable()
     .Navigatable()
    .DataSource(dataSource => dataSource
        .Ajax()
        .PageSize(50)
        .ServerOperation(false)
             .Batch(true) // Enable batch updates.
                       .Model(model =>
                            {
                                model.Id(p => p.OutputcashID); // Specify the property which is the unique identifier of the model.
                                //model.Field(p => p.OutputcashID).Editable(false); // Make the ProductID property not editable.
                                model.Field(p => p.OutputcashID).Editable(false);
                                model.Field(p => p.Level1).Editable(false);
                                model.Field(p => p.TotalRecitems).Editable(false);
                                model.Field(p => p.TotalReconcilingItems).Editable(false);
                                model.Field(p => p.AsOfDate).Editable(false);
                                model.Field(p => p.InternalLocalAmt).Editable(false);

                            })


           .Update("Editing_Update", "SaveRec")
           )


            .Pageable(pageable => pageable
                 .Refresh(true)
                .Input(true)
                .Numeric(false)
             )
                         .Resizable(resize => resize.Columns(true))
                         .Selectable()
                         .Events(ev => ev.Change("differenceValue"))



        )


<script>
 $(document).ready(function () {

        var gridOutput = $("#OutputCashGrid").data("kendoGrid");

        gridOutput.table.bind("keypress", function (e) {
            if (e.which !== 0 && e.charCode !== 0 && !e.ctrlKey && !e.metaKey && !e.altKey) {

                //get currently navigated cell, this id follows user's navigation
                var activeCell = $("#OutputCashGrid_active_cell");

                //don't do anything if already editing cell        
                if (activeCell.hasClass("k-edit-cell")) return;

                gridOutput.editCell(activeCell);
                var input = activeCell.find("input");



                //number datatype editor loses key press character when entering edit
                if (input.last().attr('data-type') === 'number') {
                   var a= input.val(String.fromCharCode(e.keyCode | e.charCode));
                   var selectedItemRow = gridOutput.dataItem($(e.currentTarget).closest("tr"));
                } else {
                    input.val("");
                }
            }
        });


        $("#OutputCashGrid table").on("keydown", "tr", function (e) {
            var code = (e.keyCode ? e.keyCode : e.which);
            if (code == 13) { //If key is ENTER
                //find index of the td element
                var activeCell = $("#OutputCashGrid_active_cell");
                var tdIndex = $(e.target).closest('td').index();
                var tdvalue = $(e.target).closest('td').val();
                var cellvalue = activeCell.val();
                var row = $(e).closest("tr");
               // var model = $("#OutputCashGrid").dataItem(row);

                //var difference = selectedItemRow.LocalDifference
                //var TotalInternalAmt = selectedItemRow.InternalLocalAmt
                //var TotalExternalAmt = selectedItemRow.ExternalLocalAmt
                //var updatedDifference = Math.abs(TotalInternalAmt) - Math.abs(TotalExternalAmt)
                //selectedItemRow.set("Differnce", updatedDifference)
                //get the next row's cell
                var nextRow = $(e.target).closest('tr').next();
                var nextRowCell = $(nextRow).find('td:eq(' + tdIndex + ')');



                //focus the next cell on a different context
                setTimeout(function () {
                    var grid = $("#OutputCashGrid").data("kendoGrid");
                    grid.current(nextRowCell);


                }, 0);

            }
        });

</script>

我附上一个屏幕截图来显示网格。

【问题讨论】:

  • 在图片中,我有可编辑的外部本地金额,按回车键我想改变差异。差值=内部本地金额-外部本地金额

标签: jquery asp.net-mvc-4 kendo-grid kendo-asp.net-mvc inline-editing


【解决方案1】:

我看到您已经发布了对您的问题的答案,但是我最近自己也在为类似的事情而苦苦挣扎,并想出了我认为更有效的解决方案。

您可以先将 change 事件附加到数据源,然后访问行模型中已更改的项目,并根据需要进行更新,而不是更新网格数据源中的所有项目(使用 Calculations 函数) .

例如:

var gridOutput = $("#OutputCashGrid").data("kendoGrid");
gridOutput.dataSource.bind("change", function(e) {
   // checks to see if the action is a change and the column being changed is what is expected
   if (e.action === "itemchange" && e.field === "ExternalLocalAmount") {           
       // here you can access model items using e.items[0].modelName;
       e.items[0].Difference = e.items[0].InternalLocalAmount - e.items[0].ExternalLocalAmount;
       // finally, refresh the grid to show the changes
       gridOutput.refresh();
   }
});

【讨论】:

  • 这绝对是一种更好的实现方式。谢谢 :)。我对 js 和剑道还很陌生,所以试图找到自己的方式。
  • 没问题很乐意提供帮助,我想我们都曾一度成为“新手”;)如果这个答案或任何其他答案已经解决了您的问题/帮助了您,请不要忘记接受它。
【解决方案2】:

经过多次尝试,我终于得到了解决方案,在这里发布代码以防有人需要它以供将来参考

function Calculations() {

        var grid = $("#StaggingCashExceptionsGridTest").data("kendoGrid");
        var TotalExternal = 0;

        var gridData = grid.dataSource.view();
        for (var i = 0; i < gridData.length; i++) {

            TotalExternal+=gridData[i].ExternalLocalAmount;//gridDaga[0].ExternalLocalAmount , takes the first row external local amount, till the number of rows in he grid. 
            TotalInternal += gridData[i].InternalLocalAmount;
        difference=TotalExternal-TotalInternal;
 $("#SubDifference").html(difference)//to set the value of difference cell
    }

【讨论】:

    【解决方案3】:

    看起来可能是以下行阻止了其余部分的运行:

    var nextRowCell = $(nextRow).find('td:eq(' + tdIndex + ')');
    

    试试这个:

    var nextRowCell = nextRow.find('td:eq(' + tdIndex + ')');
    

    【讨论】:

    • 这只是移动到下一行。我想要用户正在编辑的单元格的值。编辑的值。例如 - 在网格加载时,外部本地数量是 200,用户将其更改为 50。我想要值 50
    • var nextRowCell 为我提供下一行的数据,而不是正在编辑的当前行。
    • 好的,没关系,但是根据代码中的 cmets,changedValues() 没有被调用,所以看起来可能是因为上一行看起来你有一个阻止代码执行后续行的错误。
    • 对不起,我想在回车时调用该函数,在该函数中我想获取更新单元格的值并计算差异。但我没有得到任何与这种逻辑。所以我正在寻找其他逻辑。我删除了那条线。
    • 哦,好的。因此,为了清楚起见,您需要获取外部本地 Amt 的更新值然后更新差异单元格的逻辑帮助?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-27
    • 1970-01-01
    • 1970-01-01
    • 2018-01-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多