简体   繁体   English

使用JavaScript更新Kendo Grid时保持行焦点

[英]Keeping Row Focus when updating Kendo Grid using JavaScript

In an MVC program, I am using a Kendo Grid to display a list of data items (Facilities in this case). 在MVC程序中,我使用Kendo网格显示数据项列表(在这种情况下为Facility)。 The model for the grid contains many more columns than are being displayed and I have a separate area beside the grid where those values are displayed for the user to edit. 网格的模型包含的列比正在显示的要多得多,并且我在网格旁边有一个单独的区域,这些区域将显示这些值供用户编辑。 As the user changes the values, they are reflected back in the Grid's datasource. 当用户更改值时,它们会反映回Grid的数据源中。 If the user moves to another row without saving, they are prompted to save the changes. 如果用户移动到另一行而不保存,则会提示他们保存更改。

The code for the grid: 网格的代码:

    @(Html.Kendo().Grid(Model)    
    .Name("FacilityGrid")       
    .Columns(columns => {
        columns.Bound(p => p.Name).Width(100);
        columns.Bound(p => p.ShortDescription).Width(150);
        })
    .ToolBar(toolbar => toolbar.Create().Text("Add New Facility"))
    .Editable(editable => editable.Mode(GridEditMode.PopUp).TemplateName("FacilityPopup"))
    .Pageable()
    .Resizable(resize => resize.Columns(true)) 
    .Sortable()
    .Selectable()
    .Scrollable()
    .Filterable()
    .DataSource(dataSource => dataSource
        .Ajax()
        .PageSize(10)
        .Model(model => 
            {
                model.Id(p => p.FacilityID); 
                model.Field(p=>p.FacilityID).DefaultValue(new Guid("00000000-0000-0000-0000-000000000000"));
            })
        .Read(read => read.Action("Facility_Read", "Setup"))
        .Create(update => update.Action("Facility_Create", "Setup"))
        .Update(update => update.Action("Facility_Update", "Setup"))
        .Destroy(update => update.Action("Facility_Destroy", "Setup"))
        )
        .Events(events => events
        // Subscribe to the "change" event.
        .Change("rowChange")
        )

For now I just have some inputs to edit the additional data: 现在,我只需要输入一些内容即可编辑其他数据:

    <p id="FacilityID"> </p>
    <p> Facility Name: <input ID="Name" class="GridLinked" type="text"></p>
    <p> Description: <input ID="Description" class="GridLinked" type="text"></p>
    <p> Address 1: <input ID="Address1" class="GridLinked" type="text"></p>
    <p> Address 2: <input ID="Address2" class="GridLinked" type="text"></p>
    <p> City: <input ID="City" class="GridLinked" type="text"></p>
    <p> State: <input ID="State" class="GridLinked" type="text"></p>
    <p> Country: <input ID="Country" class="GridLinked" type="text"></p>

The values are set on the change event for the grid 在网格的更改事件上设置值

function rowChange(e) { 
    // handle the "change" event
    // Get the values from the Grid to display in the detail area
    $("#Name").val(this.dataItem(this.select()).Name);
    $("#FacilityID").text(this.dataItem(this.select()).FacilityID);
    $("#Description").val(this.dataItem(this.select()).Description);
    $("#Address1").val(this.dataItem(this.select()).Address1);
    $("#Address2").val(this.dataItem(this.select()).Address2);
    $("#City").val(this.dataItem(this.select()).City);
    $("#State").val(this.dataItem(this.select()).State);
    $("#Country").val(this.dataItem(this.select()).Country);

    if (this.dataSource.hasChanges()) {
        if (confirm("You have unsaved changes. Do you want to save them?")) {
            $('#FacilityGrid').data('kendoGrid').saveChanges();
        }
        else {
            $('#FacilityGrid').data('kendoGrid').cancelChanges();
        }
    };
}    

and the datasource for the grid is updated with change to the input fields. 并通过更改输入字段来更新网格的数据源。

 $(".GridLinked").change(function (event) {
     var grid = $('#FacilityGrid').data('kendoGrid');
// See if there are any pending changes
   debugger;

     var cell = event.target;
     var selectedRow = grid.select();
     var selectedRowIndex = selectedRow.index();
     grid.dataSource.data()[selectedRowIndex].set(cell.id, cell.value);
     grid.select(selectedRowIndex);
    // Set the focus back to the Grid
});

The problem I am facing is that when cell values are set in the grid, the focus indicator (highlighted row) goes away and when I attempt to set it back again (using Grid.select(selectedRowIndex)), it causes the rowChange event to fire on the grid and my handler throws an error 我面临的问题是,当在网格中设置单元格值时,焦点指示器(突出显示的行)消失了,而当我尝试再次将其设置回(使用Grid.select(selectedRowIndex))时,它将导致rowChange事件发生在网格上触发并且我的处理程序抛出错误

JavaScript runtime error: Unable to get property 'Name' of undefined or null reference

Does anyone have any suggestions on how to have some sort of indicator as to the selected row in the grid? 有没有人对如何对网格中的所选行具有某种指示符有任何建议?

Is this a good design practice, to manipulate the grid datasource directly? 直接操作网格数据源是一个好的设计实践吗?

You can highlight a selected row by painting the the selected grid using the dataSource.view(). 您可以使用dataSource.view()绘制选定的网格来突出显示选定的行。 I will advice you to do the following in the rowChange event 我将建议您在rowChange事件中执行以下操作

var cell = event.target;  var selectedRow = grid.select();   
var selectedRowIndex = selectedRow.index() 
var dataView = this.dataSource.view();  
$("#FacilityGrid tbody").find("tr[data-uid=" + selectedRowIndex + "]").css("backgroundcolor", "grey");

Hope this helps ...happy Coding 希望这可以帮助...快乐编码

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

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