示例#1
0
        private void PrepColumns(Models.GridContext gridContext, RenderingModel model)
        {
            foreach (var col in gridContext.GetVisibleColumns())
            {
                Column renderingColumn = new Column();
                model.Columns.Add(renderingColumn);
                renderingColumn.Name       = col.ColumnName;
                renderingColumn.HeaderText = col.HeaderText;

                if (gridContext.GridDefinition.Sorting && col.EnableSorting)
                {
                    SortDirection linkDirection = SortDirection.Asc;
                    SortDirection iconDirection = SortDirection.Unspecified;

                    if (gridContext.QueryOptions.SortColumnName == col.ColumnName && gridContext.QueryOptions.SortDirection == SortDirection.Asc)
                    {
                        iconDirection = SortDirection.Asc;
                        linkDirection = SortDirection.Dsc;
                    }
                    else if (gridContext.QueryOptions.SortColumnName == col.ColumnName && gridContext.QueryOptions.SortDirection == SortDirection.Dsc)
                    {
                        iconDirection = SortDirection.Dsc;
                        linkDirection = SortDirection.Asc;
                    }
                    else
                    {
                        iconDirection = SortDirection.Unspecified;
                        linkDirection = SortDirection.Asc;
                    }

                    renderingColumn.Onclick           = HtmlUtility.MakeSortLink(gridContext.GridName, col.ColumnName, linkDirection);
                    renderingColumn.SortIconDirection = iconDirection;
                }
            }
        }
示例#2
0
        private RenderingModel PrepModel(int?totalRecords, List <Row> rows, Models.GridContext gridContext)
        {
            RenderingModel model = new RenderingModel();

            model.HandlerPath = HtmlUtility.GetHandlerPath();
            model.TableHtmlId = HtmlUtility.GetTableHtmlId(gridContext.GridName);

            PrepColumns(gridContext, model);
            model.Rows = rows;

            if (model.Rows.Count == 0)
            {
                model.NoResultsMessage = gridContext.GridDefinition.NoResultsMessage;
            }

            model.GridName              = gridContext.GridName;
            model.NextButtonCaption     = gridContext.GridDefinition.NextButtonCaption;
            model.PreviousButtonCaption = gridContext.GridDefinition.PreviousButtonCaption;
            model.SummaryMessage        = gridContext.GridDefinition.SummaryMessage;
            model.ProcessingMessage     = gridContext.GridDefinition.ProcessingMessage;

            model.PagingModel = null;
            if (gridContext.QueryOptions.ItemsPerPage.HasValue)
            {
                model.PagingModel = new PagingModel();

                int currentPageIndex = gridContext.QueryOptions.PageIndex.Value;

                model.PagingModel.TotalRecords = totalRecords.Value;

                model.PagingModel.FirstRecord = (currentPageIndex * gridContext.QueryOptions.ItemsPerPage.Value) + 1;
                if (model.PagingModel.FirstRecord > model.PagingModel.TotalRecords)
                {
                    model.PagingModel.FirstRecord = model.PagingModel.TotalRecords;
                }
                model.PagingModel.LastRecord = (model.PagingModel.FirstRecord + gridContext.QueryOptions.ItemsPerPage.Value) - 1;
                if (model.PagingModel.LastRecord > model.PagingModel.TotalRecords)
                {
                    model.PagingModel.LastRecord = model.PagingModel.TotalRecords;
                }
                model.PagingModel.CurrentPage = currentPageIndex + 1;

                var numberOfPagesD = (model.PagingModel.TotalRecords + 0.0) / (gridContext.QueryOptions.ItemsPerPage.Value + 0.0);
                model.PagingModel.NumberOfPages = (int)Math.Ceiling(numberOfPagesD);

                for (int i = 1; i <= model.PagingModel.NumberOfPages; i++)
                {
                    model.PagingModel.PageLinks.Add(i, HtmlUtility.MakeGotoPageLink(gridContext.GridName, i));
                }
            }

            model.IsFilterable = gridContext.GridDefinition.Filtering;

            model.ClientDataTransferHtmlBlock = MVCGrid.Web.MVCGridHtmlGenerator.GenerateClientDataTransferHtml(gridContext);

            return(model);
        }
示例#3
0
        private static string RenderUsingRenderingEngine(GridEngine engine, Models.GridContext gridContext)
        {
            IMVCGridRenderingEngine renderingEngine = GetRenderingEngine(gridContext);

            using (MemoryStream ms = new MemoryStream())
            {
                using (TextWriter tw = new StreamWriter(ms))
                {
                    engine.Run(renderingEngine, gridContext, tw);
                }

                string result = LocalEncoding.GetString(ms.ToArray());
                return(result);
            }
        }
示例#4
0
        private static string RenderUsingController(GridEngine engine, Models.GridContext gridContext, HtmlHelper helper)
        {
            var model = engine.GenerateModel(gridContext);

            var controllerContext  = helper.ViewContext.Controller.ControllerContext;
            ViewDataDictionary vdd = new ViewDataDictionary(model);
            TempDataDictionary tdd = new TempDataDictionary();

            using (var sw = new StringWriter())
            {
                var viewResult = ViewEngines.Engines.FindPartialView(controllerContext,
                                                                     gridContext.GridDefinition.ViewPath);
                var viewContext = new ViewContext(controllerContext, viewResult.View, vdd, tdd, sw);
                viewResult.View.Render(viewContext, sw);
                viewResult.ViewEngine.ReleaseView(controllerContext, viewResult.View);
                return(sw.GetStringBuilder().ToString());
            }
        }
示例#5
0
        public void Render(Models.RenderingModel model, Models.GridContext gridContext, TextWriter outputStream)
        {
            var sw = outputStream;

            StringBuilder sbHeaderRow = new StringBuilder();

            foreach (var col in model.Columns)
            {
                if (col.VisibilityExport)
                {
                    if (sbHeaderRow.Length != 0)
                    {
                        sbHeaderRow.Append(",");
                    }
                    sbHeaderRow.Append(CsvEncode(col.HeaderText));
                }
            }
            sbHeaderRow.AppendLine();
            sw.Write(sbHeaderRow.ToString());

            foreach (var item in model.Rows)
            {
                StringBuilder sbRow = new StringBuilder();
                foreach (var col in model.Columns)
                {
                    if (col.VisibilityExport)
                    {
                        var cell = item.Cells[col.Name];

                        if (sbRow.Length != 0)
                        {
                            sbRow.Append(",");
                        }

                        string val = cell.PlainText;

                        sbRow.Append(CsvEncode(val));
                    }
                }
                sbRow.AppendLine();
                sw.Write(sbRow.ToString());
            }
        }
示例#6
0
        public void Render(Models.RenderingModel model, Models.GridContext gridContext, System.IO.TextWriter outputStream)
        {
            //model.
            string template = @"
@using MVCGrid.Models
@helper SortImage(Column col){
    
    if (col.SortIconDirection.HasValue)
    {
        switch (col.SortIconDirection)
        {
            case SortDirection.Asc:
                <img src='@(Model.HandlerPath)/sortup.png' class='pull-right' />
                break;
            case SortDirection.Dsc:
                <img src='@(Model.HandlerPath)/sortdown.png' class='pull-right' />
                break;
            case SortDirection.Unspecified:
                <img src='@(Model.HandlerPath)/sort.png' class='pull-right' />
                break;
        }
    }
}

@helper PageLink(int pageNum, string link, int currentPage){

    if (pageNum == currentPage){
        <li class='active'><a href='#' onclick='@Raw(link)'>@pageNum</a></li>
    }
    else{
        <li><a href='#' onclick='@Raw(link)'>@pageNum</a></li>
    }
}
@helper PageNextLink(int pageToEnd, PagingModel pagingModel){
    string attr="""";
    if (pageToEnd == pagingModel.CurrentPage){
        attr="" class='disabled'"";
    }
    string onclick = """";
    if (pageToEnd > pagingModel.CurrentPage){
        onclick = pagingModel.PageLinks[pagingModel.CurrentPage + 1] + ""; "";
    }

    <li@(Raw(attr))>
        <a href='#' aria-label='Next' onclick='@(Raw(onclick))return false;'><span aria-hidden='true'>Next &raquo;</span></a>
    </li>
}
@helper PagePreviousLink (int pageToStart, PagingModel pagingModel){
    string attr="""";
    if (pageToStart == pagingModel.CurrentPage){
        attr="" class='disabled'"";
    }
    string onclick = """";
    if (pageToStart < pagingModel.CurrentPage){
        onclick = pagingModel.PageLinks[pagingModel.CurrentPage - 1] + ""; "";
    }

    <li@(Raw(attr))>
        <a href='#' aria-label='Previous' onclick='@(Raw(onclick))return false;'><span aria-hidden='true'>&laquo; Previous</span></a>
    </li>
}

@functions {
    // Pass a user name to this method.
    string HeaderAttributes(Column col)
    {
        string str="""";
        if (col.Onclick != null){
            str="" style='cursor: pointer;'"";
            str += String.Format("" onclick='{0}'"", col.Onclick);
        }
        return str;
    }

    string AppendCssAttribute(string classString)
    {
        if (!String.IsNullOrWhiteSpace(classString))
        {
            return String.Format("" class='{0}'"", classString);
        }
        return """";
    }
    
}

<table id=""@Model.TableHtmlId"" class=""table table-striped table-bordered"">
    <thead>
        <tr>
            @foreach (var col in Model.Columns){
                <th@(Raw(HeaderAttributes(col)))>@col.HeaderText @(SortImage(col))</th>
            }
        </tr>
    </thead>
    <tbody>
        @foreach (var row in Model.Rows)
        {
            <tr@(Raw(AppendCssAttribute(row.CalculatedCssClass)))>
                @foreach (var col in Model.Columns)
                {
                    var cell = row.Cells[col.Name];
                    <td@(Raw(AppendCssAttribute(cell.CalculatedCssClass)))>@Raw(cell.HtmlText)</td>
                }
            </tr>
        }
    </tbody>
</table>

@if (Model.PagingModel != null){
    var pagingModel = Model.PagingModel;
    int pageToStart;
    int pageToEnd;
    pagingModel.CalculatePageStartAndEnd(5, out pageToStart, out pageToEnd);

    <div class='row'>
        <div class='col-xs-6'>
            Showing @pagingModel.FirstRecord to @pagingModel.LastRecord of @pagingModel.TotalRecords entries
        </div>
        <div class='col-xs-6'>
            <ul class='pagination pull-right' style='margin-top: 0;'>
                @PagePreviousLink(pageToStart, pagingModel)
                @for (int i = pageToStart; i <= pageToEnd; i++)
                {
                    <text>@PageLink(i, pagingModel.PageLinks[i], pagingModel.CurrentPage)</text>
                }
                @PageNextLink(pageToEnd, pagingModel)
            </ul>
        </div>
    </div>
}
";//

            string templateKey = "Output";

            var result = RazorEngine.Engine.Razor.RunCompile(template, templateKey, typeof(Models.RenderingModel), model);


            outputStream.Write(result);
        }