【问题标题】:Check box uncheck while scroll up and down in kendo ui grid在 kendo ui 网格中上下滚动时取消选中复选框
【发布时间】:2014-07-30 07:50:01
【问题描述】:

我有一个剑道 UI 网格,我从 Javascript 绑定下面是相同的代码。

我的问题是,当我选中复选框并向下滚动网格并向上滚动时,即使我转到下一页,选中的复选框也未选中,然后也会遇到同样的问题。

$(gridName).html("");
    var fieldSchema = [];
    var columnSchema = [];

columnSchema.push({
    field: "",
    width: "30px",
    template: "<input id='chkDelete' type='checkbox' />",
});


var counter = 0;
$.each(data, function (index) {
    counter = counter + 1;
    var xmldoc = $.parseXML(data[index].CustomFields);
    var $xml = $(xmldoc);
    var jsonStr = '{';
    $xml.find("Fields").find("Field").each(function () {
        jsonStr = jsonStr + '"' + $(this).attr("Title").replace(/\s/g, '').replace(/[^\w\s]/gi, '') + '":{';
        jsonStr = jsonStr + '"Title":"' + $(this).attr("Title") + '",';
        jsonStr = jsonStr + '"Value":"' + $(this).attr("Value") + '",';
        jsonStr = jsonStr + '"Id":"' + $(this).attr("Id") + '"},';

        if (counter == 1) {
            columnSchema.push({
                field: $(this).attr("Title").replace(/\s/g, '').replace(/[^\w\s]/gi, '') + ".Value",
                title: $(this).attr("Title"),
                width: "128px",
                template: "#=" + $(this).attr("Title").replace(/\s/g, '').replace(/[^\w\s]/gi, '') + ".Value#",
            });

        }
    });
    jsonStr = jsonStr + '"CustomFields":"' + data[index].CustomFields.replace(/\"/g, "\'") + '",';
    jsonStr = jsonStr + '"ValidationPlanId":"' + data[index].ValidationPlanId + '",';
    jsonStr = jsonStr + '"IsTrCreated":"' + data[index].IsTrCreated + '",';
    jsonStr = jsonStr + '"Note":"' + data[index].Note + '",';
    jsonStr = jsonStr + '"IsUpdate":"' + data[index].IsUpdate + '",';
    jsonStr = jsonStr + '"TestRequestId":"' + data[index].TestRequestId + '"';
    jsonStr = jsonStr + '}';

    fieldSchema.push($.parseJSON(jsonStr));
});

var dtVpAdd = new kendo.data.DataSource({
    data: fieldSchema,
    schema: {
        model: {
            id: "ValidationPlanId"
        },
        total: function (result) {
            var totalCount = result.length;
            return totalCount;
        }

    }

});
dtVpAdd.pageSize(10);


$(gridName).kendoGrid({
    dataSource: new kendo.data.DataSource({
        data: fieldSchema,
        schema: {
            model: {
                id: "ValidationPlanId"
            }
        },
        pageSize: 10
    }),
    columns: columnSchema,
    filterable: true,
    sortable: {
        mode: "multiple",
        allowUnsort: true
    },
    scrollable: {
        virtual: true
    },
    resizable: true,
    reorderable: true,
    pageable: {
        input: true,
        numeric: false
    },

    dataBound: function () {
        $(gridName).on('click', '#chkDeleteAll', function () {
            var checked = $(this).is(':checked');
            $("input[id*='chkDelete']:checkbox").attr('checked', checked);
        });
    },

});

【问题讨论】:

  • 网格中的复选框有点棘手,因为您无法在不进入编辑模式的情况下更新它们(单击复选框)。你有没有记住这一点?如果没有,您会看到已标记的复选框,但模型实际上并未更新,因此当您加载新数据(页面、滚动、过滤器等)时,更改会丢失。

标签: javascript jquery asp.net-mvc-4 kendo-ui kendo-grid


【解决方案1】:

`Grid 中的复选框有点棘手,因为您无法在不进入编辑模式的情况下更新它们(单击复选框)。

如果您不考虑这一点,您会看到已标记的复选框,但模型实际上并未更新,因此当您加载新数据(页面、滚动、过滤器...)时,更改会丢失。

一种可能的解决方案是定义一个事件处理程序,当单击复选框时更新模型。

步骤:

将您的模板定义更改为:

template: "<input class='ob-checkbox' type='checkbox' #= Checkbox ? 'checked' : '' #/>",

我在其中指定如何呈现复选框以及哪个是当前值(在此示例中为 Checkbox 字段的值)。

然后,为这些input 定义一个处理程序。为此,我更喜欢在使用实时事件处理程序初始化 Grid 之后执行它,而不是在 dataBound 中执行此操作。比如:

grid.wrapper.on("click", ".ob-checkbox", function(e) {
    // Get the row containing the checkbox
    var row = $(e.currentTarget).closest("tr");
    // Get the item in the model corresponding to that row
    var item = grid.dataItem(row);
    // Get current value of the rendered checkbox (not the value in the model)
    var value = this.checked;
    // And update the model
    item.set("Checked", value);
});

其中grid 定义为:

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

在这里运行:http://jsfiddle.net/OnaBai/QubWN/

【讨论】:

  • 如果有人在下面的行中遇到问题 item.set("Checked",value);然后在行项下方写下。Checked = value 其中“Checked”是您的字段名称。
  • 您好 OnaBai,您的解决方案在一个网格中运行完美,但是当我应用到第二个网格时它无法正常工作,这给了我以下错误。项目未定义。这意味着“grid.dataItem(row)”是未定义的。请让我知道这是什么问题。
  • 你能在 JSFiddle 中重现它吗?把链接发给我,我看看。我试过了,它有效:jsfiddle.net/OnaBai/QubWN/4
  • var item = $("#grid1").data("kendoGrid").dataItem($(e.currentTarget).closest("tr"));上面的代码工作,谢谢
  • @OnaBai - 我现在也面临的同样问题是剑道树视图网格。我按照你的代码试过了。但就我而言,它不起作用。请帮我实现这里是道场链接dojo.telerik.com/uLalo
猜你喜欢
  • 2016-12-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-04-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-01-19
相关资源
最近更新 更多