c# - kendoui网格全选

标签 c# jquery asp.net-mvc kendo-ui

我使用 MVC + kendoui 网格,这是我的代码:


模型


      public class User
         {
        [StringLength(9)]       
        public int UserId { get; set; }

        [StringLength(50)]        
        public string UserName { get; set; }

        public bool IsSelected { get; set; }
}

cshtml

@model IEnumerable<PoliciesHaglasha.Models.User>

@{    
    Layout = null;
    var serializer = new System.Web.Script.Serialization.JavaScriptSerializer();
    var users = MvcHtmlString.Create(serializer.Serialize(Model));
}

         JsonUsers = @users;

        var  UsersSource = new kendo.data.DataSource({
            pageSize: 5,
            data: JsonUsers,            
            autoSync: true,
            schema: {
                model: {
                    id: "UserId",
                    fields: {
                        UserId: { type: "number" ,editable: false, nullable: false },                      
                        UserName: { type: "string" , editable: false, nullable: false }                     
                    }
                }
            }
        });     
           $("#gridPolisot").kendoGrid({
            dataSource: PolisotSource,                        
            editable: true,
            scrollable: false,
            selectable: "row",
            sortable: true,            
            reorderable: true,           
            toolbar: [{ name: "save", text: "save" }],
            columns: [ 
                    { field:"IsSelected", title: "<input type='checkbox' id='chkSelectAll'>", width: "34px" ,template: "<input  type='checkbox' #= IsSelected ? checked='checked' : '' #/>"},                                    
                    { field:"UserId", title: "User Id", width: "20px", attributes: {"class": "KendoUITD"}},
                    { field:"UserName",title:"User Name",  width: "50px", attributes: {"class": "KendoUITD"}},              
                ],         
            },                         
        });

我的问题是:

1) 如何使选择所有标题复选框起作用?

2) 当我选中/取消选中一个复选框时,它将返回到服务器

谢谢,

最佳答案

1) 您应该使用 headerTemplate 网格的配置选项:

headerTemplate: "<input type='checkbox' id='chkSelectAll' onclick='checkAll(this)'/>"

然后在页面中添加“checkAll”函数:

function checkAll(ele) {
    var state = $(ele).is(':checked');
    var grid = $('#gridPolisot').data('kendoGrid');
    $.each(grid.dataSource.view(), function () {
        if (this['IsSelected'] != state) 
            this.dirty=true;
        this['IsSelected'] = state;
    });
    grid.refresh();
}

2) 我不确定我是否正确理解了您想要实现的目标,但是如果您需要将上述功能的更改与服务器同步,那么您也可以调用网格 saveChanges method .

关于c# - kendoui网格全选,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20781918/

相关文章:

c# - 身份验证中间件

c# - ProtectedData 可以在多台计算机上工作吗?

javascript - 无法在不同的 div 中填充列表元素

javascript - 无法将表格内容渲染到 Twitter Bootstrap 中的模式?

c# - 如何在存储库中正确处理 Linq to SQL DataContext?

jquery - 如何在 jQuery 中从 MVC View Foreach 使用 OnClick 函数

c# - 使用父对象引用的 XML 反序列化

c# - 限制 2 个单独列表的组合计数 - C#

c# - 来自 ARM 的访问 token 在收到时已过期

javascript - 谁能告诉我如何在centos中打开扩展名为.dat的文件?