如何使用 javascript 引用 kendo 网格中的特定单元格?

How do I reference a specific cell in kendo grid with javascript?

现在我有一个 2 行 6 列的 kendo 网格。我需要一些逻辑来突出显示特定的单元格,但我不知道如何引用单元格。我使用了这个例子,但我不知道传递什么作为 id。

myHub.client.highlightRow = function (id) {
    var data = $("#MyGrid").data("kendoGrid").dataSource.data();
    for (var i = 0; i < data.length; i++) {
        var dataItem = data[i];
        if (dataItem.id == id) {
            //alert(dataItem.uid);
            $("#MyGrid").data("kendoGrid").tbody.find("tr[data-uid=" + dataItem.uid + "]").effect("highlight", { color: "#f35800" }, 3000);
        }
    }
};

这是我的网格示例。

function loadGaugeTable(siteId, dashboardId, endDate, planType) {
    var today = new Date();
    var metricTitle = "Metric, as of " + monthNames[today.getMonth()] + " " + today.getDate();
    var containerSize = $("#gaugeMetricTableContainer").width();
    var apiPath = "/" + getAppPath() + "/Analytics/api/DashboardApi/getAllMetricTDData" + "?siteId=" + siteId +
                                                                                    "&dashboardId=" + dashboardId +
                                                                                    "&endDate=" + escape(endDate) +
                                                                                    "&planType=" + planType

    $("#gaugeMetricTable").kendoGrid({

        attributes: {
            "class": "table-cell",
            style: "font-size: 10px"
        },
        height: 250,
        selectable: "row",
        scrollable: true,
        sortable: true,
        filterable: true,
        columns: [
            { field: "MetricName", title: metricTitle, width: containerSize / 4 + "px" },
            { field: "DailyActual", title: "Daily Actual", format: decimalPrecisionFormat },
            { field: "DailyTarget", title: "Daily Target", format: decimalPrecisionFormat },
            { field: "MTDActual", title: "MTD Actual", format: decimalPrecisionFormat },
            { field: "MTDTarget", title: "MTD Target", format: decimalPrecisionFormat },
            { field: "YTDActual", title: "YTD Actual", format: decimalPrecisionFormat },
            { field: "YTDTarget", title: "YTD Target", format: decimalPrecisionFormat }

        ],
        dataSource: {
            transport: {
                read: {
                    dataType: "json", url: apiPath
                }
            }
        },

    });
}

我将如何引用第 1 行第 2 列。

var data = $("#gaugeMetricTable").data("kendoGrid").dataSource.data();
data[0];

Returns 该行的数据,但我无法使用 data[0].columns[1].

引用该列

在kendoGrid 中,每个数据都由对象数组表示,其中一个数组元素是一行。 Kendo 将 uid 属性 添加到数组中的所有数据对象。所以一个数据对象看起来像:

var dataItem = {
    MetricName: "some-val",
    DailyActual: "some-val",
    DailyTarget: "some-val",
    MTDActual: "some-val",
    MTDTarget: "some-val",
    YTDActual: "some-val",
    YTDTarget: "some-val",
    uid: "uid-val"
};

现在要获取此数据行,您只需使用:

var grid = $("#gaugeMetricTable").data("kendoGrid");
var row = grid.find("tr[data-uid=" + dataItem.uid + "]");

接下来要按索引获取此单元格之一,您可以这样写:

var cellIndex_1 = 5;
var cell_1 = row.find("td:eq(" + cellIndex_1 + ")");

要通过 属性 名称获取一个单元格,您必须首先知道它的索引,例如如果你想获得对应于 MTDActual 属性:

的单元格
var cellName = "MTDActual";
var cellIndex_2 = grid.element.find("th[data-field = '" + cellName + "']").index();
var cell_2 = row.find("td:eq(" + cellIndex_2 + ")");

编辑:

此代码可用于常规网格和锁定列的网格:

var cellName = "MTDActual";
var grid = $("#gaugeMetricTable").data("kendoGrid");

var headerCells = grid.element.find("th");
var cellIndex = headerCells.index(grid.element.find("th[data-field = '" + cellName + "']"));

var rowCells = grid.element.find("tr[data-uid=" + dataItem.uid + "] td");
var cell = $(rowCells[cellIndex]);

Kendo 道场示例:https://dojo.telerik.com/oDUpuTAw

这对我有用:

function onChange(arg) {
       var cell = this.select();
       var cellIndex = cell[0].cellIndex;
       var column = this.columns[0];
       ...

例如,所选行中第 0 列单元格的值位于:

var mydata = dataItem[column.field];

快乐编码 KendoGrid。

您可以通过以下步骤申请试用:

首先,获取您要从中按名称将 CSS 应用到特定列的特定行。在我的要求中找到了使用具有特定列 class 名称的行的 UID 的行。

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

var row = grid.dataSource.getByUid("your-row-uid");

最后,通过 class 名称获取所选行的特定单元格值。

$(row).find("td.className").css("background-color", "lightblue");