Exemplo n.º 1
0
        public static GridBuilder <T> GridControl <T>(this HtmlHelper helper, GridInfomation grid) where T : class
        {
            grid.ObjectType = grid.ObjectType != null ? grid.ObjectType : typeof(T);//Kiểu dữ liệu của đối tượng đang xét trên lưới.
            Action <GridSelectionSettingsBuilder> selection = new Action <GridSelectionSettingsBuilder>(d => d.Mode(GridSelectionMode.Single));
            Action <PageableBuilder> pageable = new Action <PageableBuilder>(d => d.PageSizes(true).Refresh(true).ButtonCount(5));

            Action <GridColumnFactory <T> > columns = new Action <GridColumnFactory <T> >(column =>
            {
                if (grid.ValueFields != null)
                {
                    column.AutoGenerate(false);


                    foreach (var valueField in grid.ValueFields)
                    {
                        GridBoundColumnBuilder <T> bound = null;

                        Type fieldValueType = null;

                        if (grid.TypeFields != null && grid.TypeFields.Any(d => d.Key == valueField))
                        {
                            fieldValueType = grid.TypeFields.Where(d => d.Key == valueField).Select(d => d.Value).FirstOrDefault();
                        }
                        else if (grid.ObjectType != null && grid.ObjectType.HasProperty(valueField))
                        {
                            fieldValueType = grid.ObjectType.GetPropertyType(valueField);
                        }

                        if (fieldValueType != null)
                        {
                            bound = column.Bound(fieldValueType, valueField);
                        }
                        else
                        {
                            bound = column.Bound(valueField);
                        }

                        if (grid.HasDisplayFields())
                        {
                            string displayField = grid.DisplayFields.Where(d => d.Key
                                                                           == valueField).Select(d => d.Value).FirstOrDefault();

                            if (!string.IsNullOrWhiteSpace(displayField))
                            {
                                bound.Title(displayField);
                            }
                        }

                        if (grid.InvisibleFields != null)
                        {
                            bound.Visible(!grid.InvisibleFields.Contains(valueField));
                        }

                        if (grid.HasFormatFields())
                        {
                            string formatField = grid.FormatFields.Where(d => d.Key
                                                                         == valueField).Select(d => d.Value).FirstOrDefault();

                            if (!string.IsNullOrWhiteSpace(formatField))
                            {
                                bound.Format(formatField);
                            }
                        }
                    }
                    //1. Tạo command muốn hiển thị
                    //column.Command(cmd =>
                    //{
                    //    cmd.Edit();

                    ////    //cmd.Custom("Change").Click("showpage");
                    ////    //cmd.Custom("Edit").Click("ShowInfoCatBankEdit");
                    //    cmd.Destroy();
                    //}).Width(300);
                }
            });

            Action <DataSourceBuilder <T> > dataSource = new Action <DataSourceBuilder <T> >(d =>
            {
                AjaxDataSourceBuilder <T> ajaxDataSource = d.Ajax();
                //2. Xác định được id tương ứng khi click vào một record trên lưới

                ajaxDataSource.Model(m => m.Id(grid.ValueFields[0]));
                if (grid.PageSize > 0)
                {
                    ajaxDataSource.PageSize(grid.PageSize);
                }

                if (!string.IsNullOrWhiteSpace(grid.IdPropertyName))
                {
                    ajaxDataSource.Model(model => model.Id(grid.IdPropertyName));
                }

                if (!string.IsNullOrWhiteSpace(grid.Url))
                {
                    ajaxDataSource.Read(read => read.Url(grid.Url).Type(HttpVerbs.Post));
                    //ajaxDataSource.Update(read => read.Url(grid.EditUrl).Type(HttpVerbs.Post));
                    ////ajaxDataSource.Create(read => read.Url(grid.InsertUrl).Type(HttpVerbs.Post));
                }
                if (!string.IsNullOrWhiteSpace(grid.InsertActionName))
                {
                    ajaxDataSource.Update(grid.EditActionName, grid.EditControllerName);
                    //ajaxDataSource.Read(grid.DataActionName, grid.ControllerName);
                    ajaxDataSource.Create(grid.InsertActionName, grid.InsertControllerName);

                    //3. Phải xác định dược controller và action áp dụng cho nút xóa
                    ajaxDataSource.Destroy(grid.DeleteActionName, grid.DeleteControllerName);
                }
            });
            var gride = new Action <GridEventBuilder>(e =>
            {
                if (!string.IsNullOrWhiteSpace(grid.Change))
                {
                    e.Change(grid.Change);
                }
            });

            return(helper.Kendo().Grid <T>()
                   .Name(grid.GridName)
                   .Sortable()
                   .Scrollable()
                   //.ToolBar(t =>
                   //{
                   //    t.Create();
                   //    //t.Custom().Text("Add").Url("#").HtmlAttributes(new { onclick = "NewPageInsert()" });
                   //    //t.Custom().Text("Add new").Url("#").HtmlAttributes(new { onclick = "ShowDivInsert()" });

                   //})
                   .Groupable()
                   .Editable(editable => editable.Mode(GridEditMode.PopUp).TemplateName(grid.TemplateName))
                   .Selectable(selection)
                   .Pageable(pageable)
                   .Columns(columns)
                   .DataSource(dataSource)
                   .Events(gride));
        }