Created
November 21, 2017 02:51
-
-
Save albertbaldi/562eeb16b1574792883e9248047b4a82 to your computer and use it in GitHub Desktop.
MVCGrid
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@model MVCGrid.Web.Models.MVCGridToolbarModel | |
@{ | |
string gridName = Model.MVCGridName; | |
} | |
<div class="row"> | |
<div class="col-md-3"> | |
<div class="form-inline"> | |
@if (Model.PageSize) | |
{ | |
<div class="form-group"> | |
<label> | |
Registros: <select class="form-control" data-mvcgrid-type="pageSize" data-mvcgrid-name="@(gridName)"> | |
<option value="10">10</option> | |
<option value="25">25</option> | |
<option value="50">50</option> | |
<option value="100">100</option> | |
</select> | |
</label> | |
</div> | |
} | |
</div> | |
</div> | |
<div class="col-md-9"> | |
<div class="form-inline pull-right"> | |
<div class="btn-group" style="padding-bottom:5px;"> | |
@if (Model.AddNew) | |
{ | |
<button class="btn btn-default btn-novo" title="Incluir novo registro" data-id="@Model.CustomId.Value" data-id-field="@Model.CustomId.Key" data-controller="@(string.IsNullOrEmpty(Model.Controller) ? Html.CurrentController() : Model.Controller)"> | |
<span class="glyphicon glyphicon-plus" aria-hidden="true"></span> | |
</button> | |
} | |
@if (Model.ColumnVisibility) | |
{ | |
<div class="btn-group"> | |
<button type="button" title="Selecionar campos" class="btn btn-default dropdown-toggle" data-toggle="dropdown" aria-expanded="false"> | |
<span class="glyphicon glyphicon-list" aria-hidden="true"></span> | |
</button> | |
<ul class="dropdown-menu" role="menu" data-mvcgrid-type="columnVisibilityList" data-mvcgrid-name="@(gridName)"></ul> | |
</div> | |
} | |
@if (Model.Export) | |
{ | |
<button class="btn btn-default" title="Exportar registros (.cvs)" data-mvcgrid-type="export" data-mvcgrid-name="@(gridName)"> | |
<span class="glyphicon glyphicon-download-alt" aria-hidden="true"></span> | |
</button> | |
} | |
</div> | |
@if (Model.GlobalSearch) | |
{ | |
<span> </span> | |
<div class="form-group"> | |
<label> | |
Filtro: <input type="search" placeholder="" class="form-control" | |
data-mvcgrid-type="additionalQueryOption" data-mvcgrid-option="search" data-mvcgrid-name="@(gridName)" | |
data-mvcgrid-apply-additional="keyup"> | |
</label> | |
</div> | |
} | |
</div> | |
</div> | |
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Interface; | |
using Models; | |
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Web; | |
namespace Repository | |
{ | |
public class DisciplinaRepository : IRepository<Disciplina> | |
{ | |
public IEnumerable<Disciplina> GetData(out int totalRecords, int? limitOffset, int? limitRowCount, string orderBy, bool desc) | |
{ | |
return GetData(out totalRecords, null, limitOffset, limitRowCount, orderBy, desc); | |
} | |
public IEnumerable<Disciplina> GetData(out int totalRecords, string globalSearch, int? limitOffset, int? limitRowCount, string orderBy, bool desc) | |
{ | |
using (var db = new DataEntities()) | |
{ | |
var query = db.Disciplina.AsQueryable(); | |
if (!string.IsNullOrWhiteSpace(globalSearch)) | |
{ | |
query = query.Where(p => (p.Descricao + " " | |
).Contains(globalSearch)); | |
} | |
totalRecords = query.Count(); | |
if (!string.IsNullOrWhiteSpace(orderBy)) | |
{ | |
query = query.OrderBy(orderBy, desc); | |
} | |
if (limitOffset.HasValue) | |
{ | |
query = query.Skip(limitOffset.Value).Take(limitRowCount.Value); | |
} | |
return query.ToList(); | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
namespace Interface | |
{ | |
public interface IRepository<T> | |
{ | |
IEnumerable<T> GetData(out int totalRecords, string globalSearch, int? limitOffset, int? limitRowCount, string orderBy, bool desc); | |
IEnumerable<T> GetData(out int totalRecords, int? limitOffset, int? limitRowCount, string orderBy, bool desc); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class MVCGridConfig | |
{ | |
private static Regex digitsOnly = new Regex(@"[^\d]"); | |
public static void RegisterGrids() | |
{ | |
GridDefaults gridDefaults = new GridDefaults() | |
{ | |
NoResultsMessage = "Não existem registros para exibir", | |
ProcessingMessage = "Carregando", | |
NextButtonCaption = "Próximo", | |
PreviousButtonCaption = "Anterior", | |
SummaryMessage = "Exibindo {0} até {1} de {2} registros", | |
}; | |
ColumnDefaults colDefaults = new ColumnDefaults() | |
{ | |
EnableSorting = true, | |
AllowChangeVisibility = true, | |
}; | |
// add your Grid definitions here, using the MVCGridDefinitionTable.Add method | |
#region Disciplina | |
MVCGridDefinitionTable.Add("DisciplinaGrid", new MVCGridBuilder<Disciplina>(gridDefaults, colDefaults) | |
.WithAuthorizationType(AuthorizationType.AllowAnonymous) | |
.AddColumns(cols => | |
{ | |
cols.Add("Descricao").WithHeaderText("Descrição") | |
.WithHtmlEncoding(false) | |
.WithValueExpression((p, c) => c.UrlHelper.Action("Edit", "Disciplina", new { id = p.Id })) | |
.WithValueTemplate("<a href='{Value}'>{Model.Descricao}</a>") | |
.WithPlainTextValueExpression(p => p.Descricao.ToString()); | |
}) | |
.WithAdditionalQueryOptionNames("Search") | |
.WithAdditionalSetting("RenderLoadingDiv", false) | |
.WithSorting(true, "Descricao") | |
.WithPaging(true, 10, true, 100) | |
.WithRetrieveDataMethod((context) => | |
{ | |
var options = context.QueryOptions; | |
int totalRecords; | |
var repo = DependencyResolver.Current.GetService<DisciplinaRepository>(); | |
string globalSearch = options.GetAdditionalQueryOptionString("Search"); | |
var items = repo.GetData(out totalRecords, globalSearch, options.GetLimitOffset(), options.GetLimitRowcount(), | |
options.SortColumnName, options.SortDirection == SortDirection.Dsc); | |
return new QueryResult<Disciplina>() | |
{ | |
Items = items, | |
TotalRecords = totalRecords | |
}; | |
})); | |
#endregion Disciplina | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment