简体   繁体   English

Kendo UI Grid可编辑的手动dataItem.set()缓慢/延迟

[英]Kendo UI Grid editable manual dataItem.set() slow/delay

I have an editable Kendo Grid that may have a column with a checkbox to change a boolean value. 我有一个可编辑的Kendo网格,其中可能有一个带有复选框的列,用于更改布尔值。 I have used this solution proposed by OnaBai that is working perfectly! 我使用了OnaBai提出的完美解决方案

The only problem is that the checkbox value change is too slow. 唯一的问题是复选框值更改太慢。 When user clicks it, it takes about 1 second to change. 当用户单击它时,大约需要1秒钟才能更改。 I realize that the dataItem.set() method is responsible by this delay. 我意识到dataItem.set()方法是造成此延迟的原因。

My grid has a considerable amount of data. 我的网格中有大量数据。 About 30-40 columns and 300+ lines. 大约30-40列和300+行。 It is defined as follows: 定义如下:

$("#mainGrid").kendoGrid({
    dataSource: dataSource,

    pageable: false,
    sortable: true,

    scrollable: true,
    editable: true,
    autoBind: false,
    columnMenu: true, // Cria o menu de exibição de colunas
    height: getGridHeight(),

    toolbar: [/* hide for brevity */],
    columns: [/* hide for brevity */],
    dataBound: function() { /* hide for brevity. */},
    edit: function() { /* hide for brevity. */}
});

Another detail is that, when dataItem.set() is called, it calls dataBound() event but that is not causing the delay. 另一个细节是,当调用dataItem.set()时,它将调用dataBound()事件,但不会引起延迟。 Grid's edit() method is not being called on this process. 在此过程中未调用Grid的edit()方法。 I don't know if worths to post dataSource code. 我不知道是否值得发布dataSource代码。

I would suggest using the approach from this code library article when it comes to use checkboxes. 在使用复选框时,我建议使用代码库文章中的方法。 It does not use the set methods of the model and still works the same way. 它不使用模型的set方法,并且仍以相同的方式工作。 Even with 2000 records on a single page CheckAll will work flawlessly. 即使在一个页面上有2000条记录,CheckAll也可以正常工作。

I have found an alternative way for doing what OnaBai proposed and it's working better. 我已经找到了替代OnaBai的建议的方法,并且效果更好。

// This is the grid
var grid = $("#mainGrid").data("kendoGrid");

// .flag is a class that is used on the checkboxes
grid.tbody.on("change", ".flag", function (e) 
{
    // Get the record uid
    var uid = grid.dataItem($(e.target).closest("tr")).uid;

    // Find the current cell
    var td = $(e.target).parent().parent();

    // This opens the cell to edit(edit mode)
    grid.editCell(td);

    // This ones changes the value of the Kendo's checkbox, that is quickly shown, 
    // changed and then hidden again. This marks the cell as 'dirty' too.
    $(td.find("input")[0]).prop("checked", $(e.target).is(":checked") ? "checked" : null).trigger("change").trigger("blur");
}

Should try something like this: 应该尝试这样的事情:

I'll set the column with the Edit button to look like this: 我将使用“编辑”按钮将列设置为如下所示:

columns.Command(command => {command.Edit().HtmlAttributes(new { id = "btnEdit_" + "${Id}" }); }).Width(100).Hidden(true);

And have it where clicking into the first column (I have an image with a hyperlink) uses an onclick function to programmatically click the Edit button, click the checkbox, then click the Update button. 并将其放置在第一列(我有一个带有超链接的图像)中,该位置使用onclick函数以编程方式单击“编辑”按钮,单击复选框,然后单击“更新”按钮。 Probably more "old school", but I like knowing it is following the order I would be doing if I were updating it, myself. 可能是更多的“老派”,但我想知道它遵循我自己进行更新时要执行的顺序。

I pass in the object ("this"), so I can get the row and checkbox when it appears, the new status as 0 or 1 (I have some code that uses it, not really necessary for this demo, though, so I'm leaving that part out of my function for simplicity), and the ID of that item: 我传入了对象(“ this”),所以我可以在行和复选框出现时将其获取,新状态为0或1(虽然我有一些使用它的代码,但是对于本演示来说并不是必须的,所以我为了简化起见,我将该部分保留在我的函数之外),以及该项目的ID:

columns.Bound(p => p.IsActive).Title("Active").Width(100).ClientTemplate("# if (IsActive == true ) {# <a href=javascript:void(0) id=btnActive_${Id} onclick=changeCheckbox(this, '0', ${Id}) class='k-button k-button-icontext k-grid-update'><img style='border:1px solid black' id=imgActive src=../../Images/active_1.png /></a> #} else {# <a href=javascript:void(0) id=btnActive_${Id} onclick=changeCheckbox(this, '1', ${Id}) class='k-button k-button-icontext k-grid-update'><img style='border:1px solid black' id=imgActive src=../../Images/active_0.png /></a> #}#");

function changeCheckbox(obj, status, id) {
    var parentTr = obj.parentNode.parentNode;
    $('[id="btnEdit_' + id + '"]').click();

    parentTr.childNodes[5].childNodes[0].setAttribute("id", "btnUpdate_" + id); // my Update button is in the 6th column over
    parentTr.childNodes[0].childNodes[0].setAttribute("id", "chkbox_" + id);
    $('[id=chkbox_' + id + ']').click().trigger("change");
    $('[id=chkbox_' + id + ']').blur();

    var btnUpdate = $('[id="btnUpdate_' + id + '"]');
    $('[id="btnUpdate_' + id + '"]').click();

}

Code above assumes, of course, the checkbox is in the first column. 上面的代码当然假定该复选框位于第一列中。 Otherwise, adjust the first childNodes[0] on that chkbox setAttribute line to the column it sits in, minus one because it starts counting from zero. 否则,将chkbox setAttribute行上的第一个childNodes[0]调整为它所在的列,减去1,因为它从零开始计数。

I did a solution much like @DontVoteMeDown. 我做了一个类似于@DontVoteMeDown的解决方案。 But I have a nested grid (master / detail) so I get the parent grid from the event parameter. 但是我有一个嵌套的网格(主/明细),所以我从事件参数中获取了父网格。 Also I just trigger a click-event on the checkbox. 另外,我只是在复选框上触发了一个点击事件。

$("#grid .k-grid-content").on("change", "input.chkbx", function (e) {
    // Get the parent grid of the checkbox. This can either be the master grid or the detail grid.
    var parentGrid = $(e.target).closest('div[data-role="grid"]').data("kendoGrid");
    // Get the clicked cell.
    var td = $(e.target).closest("td");
    // Enter the cell's edit mode.
    parentGrid.editCell(td);
    // Find the checkbox in the cell (which now is in "edit-mode").
    var checkbox = td.children("input[type=checkbox]");
    // Trigger a click (which will toggle check/uncheck).
    checkbox.trigger("click");
});

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

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