9

我希望能够调用一个将 Kendo 网格滚动到所选行的函数。我已经尝试了一些方法,但都没有奏效,

例如我试过这个:

var grid = $("#Grid").data("kendoGrid"),
    content = $(".k-grid-content");
content.scrollTop(grid.select());

我也试过这个:

var gr = $("#Grid").data("kendoGrid");
var dataItem = gr.dataSource.view()[gr.select().closest("tr").index()];
var material = dataItem.id;
var row = grid.tbody.find(">tr:not(.k-grouping-row)").filter(function (i) {
    return (this.dataset.id == material);
});
content.scrollTop(row);

谁能指出我正确的方向?:)

--- 已编辑 ---

由于其他原因,我无法绑定到更改事件,所以我必须能够调用一个函数,将列表滚动到选定的行。这就是我用@Antonis 为我提供的答案所尝试的。

var grid = $("#Grid").data("kendoGrid")
grid.element.find(".k-grid-content").animate({  
    scrollTop: this.select().offset().top  
 }, 400);

当我尝试这样做时,它会向下滚动列表,但不会滚动到选定的行。我是否通过调用 scrollTop 以错误的方式使用网格对象?

这个也是:

var grid = $("#ItemGrid").data("kendoGrid");
grid.scrollToSelectedRow = function () {
    var selectedRow = this.select();
    if (!selectedRow) {    
        return false;    
    }
    this.element.find(".k-grid-content").animate({
        scrollTop: selectedRow.offset().top  
    }, 400);
    return true;
    };

grid.scrollToSelectedRow();
4

5 回答 5

15

所以这里的大多数答案都犯了两个错误,一个只是效率问题,另一个是实际的错误。

  1. 使用element.find(".k-grid-content"). 这是非常不必要的。grid.content更容易(更快)为您提供完全相同的东西。

  2. 用于.offset()查找行的位置。这是不正确的:这将告诉您该行相对于文档本身的位置。如果您的页面允许您滚动整个页面(不仅仅是网格),则此数字将不正确。

    而是使用.position() - 这为您提供相对于偏移父级的位置。为了.position()给你正确的数字,你的表格grid.content必须有position: relative这最好通过 CSS 样式表应用:

    .k-grid-content 表{
      位置:相对;
    }

无论如何,假设您已经有了对网格本身的引用,我将其称为grid,并且您将内容窗格设置为relative位置,您所要做的就是:

grid.content.scrollTop(grid.select().position().top);

或者,对于动画,

grid.content.animate({ scrollTop: grid.select().position().top }, 400);

如前所述,grid.content为您提供内容窗格,即您要实际滚动的部分。它是一个 jQuery 对象。

jQuery 对象有一个.scrollTop方法,所以这部分非常简单。.animate当您使用其scrollTop属性时,该方法的工作方式类似。现在我们只需要知道滚动哪里。

为此,grid.select()返回与所选行对应的 jQuery 对象。这就是您要滚动的位置。为了获得它的位置,我们使用 jQuery.position()方法。返回值是一个带有topleft字段的对象;我们想滚动到它的垂直位置,所以我们使用top.

change当然,这在回调中最容易使用;grid很简单(因为this在网格本身上调用了回调),并且change在选择更改时自动调用。但是你可以在任何时候调用这个代码来滚动到选择;你可以grid通过使用:

grid = $('#theGridsId').data('kendoGrid');
于 2015-02-23T22:32:25.433 回答
6

您可以在选择一行时自动执行此操作。将函数绑定到 'change' 事件,在其中,您可以滚动到选定的行。(假设您只能选择一行,由 'this.select()' 给出)

JSFiddle 示例

“更改”处理程序

//    bind to 'change' event
function onChangeSelection(e) {

    //    animate our scroll
    this.element.find(".k-grid-content").animate({  // use $('html, body') if you want to scroll the body and not the k-grid-content div
        scrollTop: this.select().offset().top  //  scroll to the selected row given by 'this.select()'
     }, 400);
}
于 2013-07-03T17:31:50.837 回答
0

我的偏移量有问题,所以位置对我来说效果更好:

grid.element.find(".k-grid-content").animate({  // use $('html, body') if you want to scroll the body and not the k-grid-content div
                    scrollTop: this.select().position().top  //  scroll to the selected row given by 'this.select()'
                 }, 400);    
于 2014-11-02T14:20:22.050 回答
0

这是更新的代码 http://jsfiddle.net/27Phm/12/

//    bind to 'change' event
function onChangeSelection(e) {
    try {
        var $trSelect = this.select();
        var $kcontent = this.element.find(".k-grid-content");
        if ($trSelect && $trSelect.length == 1 && $kcontent) {
            var scrollContentOffset = this.element.find("tbody").offset().top;
            var selectContentOffset = $trSelect.offset().top;
            var IsMove = false;
            var distance = selectContentOffset - scrollContentOffset;
            distance += $kcontent.offset().top;
            if ($trSelect.prev().length == 1 && distance > $trSelect.prev().offset().top) {
                distance = (distance - $trSelect.prev().offset().top); //+ ($trSelect.height());
                //var toprows = $kcontent.scrollTop() / $trSelect.height(); //top rows above the scroll
                var selrowtotal = ($trSelect.offset().top - $kcontent.offset().top) + $trSelect.height();
                IsMove = selrowtotal > $kcontent.height() ? true : false;
                if (IsMove) $kcontent.scrollTop(distance);
            }
            if (!IsMove && $trSelect.offset().top < $kcontent.offset().top) {
                distance = selectContentOffset - scrollContentOffset;
                $kcontent.scrollTop(distance - 15);`enter code here`
            }
        }
    } catch (e) {

    }
}
于 2014-06-11T02:47:48.047 回答
0

我在 Kendo 移动 MVVM 中找到了一个功能

parent.set('onShow', function (e) {
   e.view.scroller.reset();
}

或者

app.xxx = kendo.observable({
   onShow: function() { e.view.scroller.reset(); }
});
于 2017-04-18T13:21:40.363 回答