Date Formatting not working - Telerik grid details - asp.net-mvc
I am hoping this is easy. I googled and looked through the forums and cannot find a solid answer. One answer was to add the calendar js file but that did not seem to work.
Look at the highlighted code below. The first 2, with formatting defined, are blank and should not be. The next 2 have real long date formats when all I need is “MM/dd/yyyy”. Yikes!
<%= Html.Telerik().Grid<ViewModelProcurementAction>(Model.ProcurementActions) //
.Name("ProcurementActionGrid")
.DetailView(details => details.ClientTemplate(
Html.Telerik()
.TabStrip()
.Name("TabStrip_<#= Id #>")
.SelectedIndex(0)
.Items(items =>
{
items.Add().Text("Additional Info").Content(
"<table>" +
"<tr>" +
"<td style='vertical-align: top'>" +
"<b>Summary Description:</b><br/><#= SummaryDescription #>" +
"</td>" +
"<td style='vertical-align: top'>" +
//"<ul>" +
"<b>Modification Number:</b> <#= ModificationNumber #><br/>" +
"<b>Funding Type:</b> <#= FundingTypeName #><br/>" +
"<b>Department Name:</b> <#= DepartmentName #><br/>" +
"<b>Solicitation Type:</b> <#= SolicitationTypeName #><br/>" +
"<b>Date Entered:</b> <#= $.telerik.formatString('{0:d}', DateEntered) #><br/>" +
//"</ul>" +
"</td>" +
"<td style='vertical-align: top'>" +
"<b>FY12:</b> <#= $.telerik.formatString('{0:c0}', FyByKey[\"FY12\"]==null ? 0 : FyByKey[\"FY12\"]) #><br/>" +
"<b>FY13: </b><#= $.telerik.formatString('{0:c0}', FyByKey[\"FY13\"]==null ? 0 : FyByKey[\"FY13\"]) #><br/>" +
"<b>FY14: </b><#= $.telerik.formatString('{0:c0}', FyByKey[\"FY14\"]==null ? 0 : FyByKey[\"FY14\"]) #><br/>" +
"<b>FY15: </b><#= $.telerik.formatString('{0:c0}', FyByKey[\"FY15\"]==null ? 0 : FyByKey[\"FY15\"]) #><br/>" +
"<b>FY16: </b><#= $.telerik.formatString('{0:c0}', FyByKey[\"FY16\"]==null ? 0 : FyByKey[\"FY16\"]) #><br/>" +
"<b>FY17: </b><#= $.telerik.formatString('{0:c0}', FyByKey[\"FY17\"]==null ? 0 : FyByKey[\"FY17\"]) #><br/>" +
"<b>Out Year Funding: </b><#= $.telerik.formatString('{0:c0}', OutYearFunding==null ? 0 : OutYearFunding ) #><br/>" +
"</td>" +
"<td style='vertical-align: top'>" +
"<b>DBE %: </b><#= $.telerik.formatString('{0:0}', DBE==null ? 0 : DBE) #><br/>" +
"<b>Amount Exceeds: </b><#= AmountExceeds #><br/>" +
//--------------------------------------------------------------------------------
"<b>Cost/Price Analysis Date: </b><#= $.telerik.formatString('{0:MM/dd/yyyy}', CostPriceAnalysisDate==null ? '' : CostPriceAnalysisDate) #><br/>" +
"<b>OIG Review Completion Date: </b><#= $.telerik.formatString('{0:shortDate}', OIGReviewCompletionDate==null ? '' : OIGReviewCompletionDate) #><br/>" +
"<b>Required Procurement Initiation Date: </b><#= RequiredProcurementInitiationDate #><br/>" +
"<b>Required Date of Award: </b><#= RequiredDateOfAward #><br/>" +
//--------------------------------------------------------------------------------
"<b>Number of Option Years: </b><#= NumberOfOptionYears #><br/>" +
"<b>Budget Approval: </b><#= BudgetApproval #><br/>" +
"<b>Point of Contact: </b><#= PointOfContact #><br/>" +
"</td>" +
"<td style='vertical-align: top'>" +
"<b>PRMT Group: </b><#= PRMTGroupName #><br/>" +
"<b>Planned CA Responsible: </b><#= PlannedCAResponsibleFullName #><br/>" +
"<b>Value of Option Years: </b><#= $.telerik.formatString('{0:0}', ValueOfOptionYears==null ? 0 : ValueOfOptionYears) #><br/>" +
"<b>Award Amount: </b><#= $.telerik.formatString('{0:0}', AwardAmount==null ? 0 : AwardAmount) #><br/>" +
"<b>Awarded Vendor: </b><#= AwardedVendor #><br/>" +
"<b>Multiple Year Funding: </b><#= MultipleYearFunding #><br/>" +
"<b>Period of Performance: </b><#= $.telerik.formatString('{0}{1}{2}', PeriodOfPerformanceStartDate==null ? '' : PeriodOfPerformanceStartDate, PeriodOfPerformanceEndDate==null ? '' : ' - ', PeriodOfPerformanceEndDate==null ? '' : PeriodOfPerformanceEndDate) #><br/>" +
"<b>Comments: </b><#= Comments #><br/>" +
"</td>" +
"</tr>" +
"</table>"
);
items.Add().Text("Purchase of Materials").Content(
"<br/><input type='checkbox' disabled='disabled' name='AwaitingProcurementPackEnabled' <#= AwaitingProcurementPackEnabled ? checked='checked' : '' #> /> <b>Awaiting Procurement pack from Point of Contact</b><br/>" +
Html.Telerik()
.Grid<ViewModelProcurementActivity>()
.Name("ActionGrid_<#= Id #>")
.Footer(false)
.Columns(c =>
{
c.Command(commands =>
{
commands.Edit().ButtonType(GridButtonType.ImageAndText);
}).Title("Actions").Width(120);
c.Bound(e => e.ActivityId).Visible(false);
c.Bound(e => e.Activity);
c.Bound(e => e.OriginalPlannedDate);
c.Bound(e => e.CurrentPlannedDate);
c.Bound(e => e.ActualDate);
c.Bound(e => e.ReasonForDateChange);
})
.DataBinding(dataBinding =>
dataBinding.Ajax()
.Select("AjaxGetActionDatesFor", "ProcurementActions", new { procurementActionId = "<#= Id #>" })
.Update("AjaxUpdateActionDatesFor", "ProcurementActions")
.Enabled(true)
)
.DataKeys(keys => keys.Add(c => c.ActivityId))
.Editable(editing => editing.Mode(GridEditMode.InLine)
)
.Selectable()
.ToHtmlString()
);
})
.ToHtmlString()
))
.Columns(c =>
{
c.Command(commands =>
{
commands.Edit().ButtonType(GridButtonType.ImageAndText);
commands.Delete().ButtonType(GridButtonType.ImageAndText);
commands.Custom("showHistory")
.ButtonType(GridButtonType.ImageAndText)
.Text("History")
.Action("Show", "ProcurementActions")
.DataRouteValues(route => { route.Add(o => o.Id).RouteKey("id"); });
}).Title("Actions").Width(100);
c.Bound(e => e.Id).Visible(false);
c.Bound(e => e.ActionId).Visible(false);
c.Bound(e => e.ContractNumber).HtmlAttributes(new {style = "vertical-align: top"});
c.Bound(e => e.ContractManager).Width(120).HtmlAttributes(new {style = "vertical-align: top"});
c.Bound(e => e.ActualCAResponsible).Width(150).HtmlAttributes(new {style = "vertical-align: top"});
c.Bound(e => e.TitleOfRequirement).HtmlAttributes(new {style = "vertical-align: top"});
c.Bound(e => e.CipOrName).Title("Project Id").HtmlAttributes(new {style = "vertical-align: top"});
c.Bound(e => e.RecordTypeName).Title("Record Type").HtmlAttributes(new {style = "vertical-align: top"});
c.Bound(e => e.FullContractType).Title("Contract Type").HtmlAttributes(new { style = "vertical-align: top" });
c.Bound(e => e.ProcurementActionYearDisplayName).Title("Plan FY").HtmlAttributes(new { style = "vertical-align: top" });
})
.DataKeys(keys => keys.Add(o => o.Id))
.DataBinding(dataBinding =>
dataBinding.Ajax()
.OperationMode(GridOperationMode.Client)
.Select("AjaxGetAll", "ProcurementActions") //, new { procurementActionId = "<#= CurrentAction #>" })
.Update("AjaxUpdate", "ProcurementActions")
.Delete("AjaxDelete", "ProcurementActions")
.Enabled(true)
)
.Editable(editing =>
editing.Mode(GridEditMode.PopUp)
.TemplateName("EditProcurementAction")
//? .AdditionalViewData(new { ListAllProcurementActionDateTypes = Model.AllProcurementActionDateTypes })
//? .AdditionalViewData(Model.AllUsers)
//? .AdditionalViewData(Model.ContractTypes)
//? .AdditionalViewData(Model.FundingTypes)
)
.Pageable(paging => paging.PageSize(15))
.Scrollable(scrolling => scrolling.Height(500))
.Filterable()
.Sortable()
%>
At the end of my file I changed this:
<%= Html.Telerik().ScriptRegistrar() %>
To this:
<%= Html.Telerik()
.ScriptRegistrar()
.DefaultGroup(group => group.Add("telerik.common.js")
.Add("telerik.calendar.js")
.Add("telerik.datepicker.js")
.Add("telerik.textbox.js"))
%>
Still nothing.
I am trying to deliver this today (for the final time) and am getting dinged for formatting.
Any thoughts?
-kb
So I finally tracked down how to do this correctly in the ClientTemplate:
"<b>Cost/Price Analysis Date: </b><#= $.telerik.formatString('{0:MM/dd/yyyy}', CostPriceAnalysisDate==null ? \"\" : CostPriceAnalysisDate) #><br/>" +
"<b>OIG Review Completion Date: </b><#= $.telerik.formatString('{0:MM/dd/yyyy}', OIGReviewCompletionDate==null ? \"\" : OIGReviewCompletionDate) #><br/>" +
"<b>Required Procurement Initiation Date: </b><#= $.telerik.formatString('{0:MM/dd/yyyy}', RequiredProcurementInitiationDate==null ? \"\" : RequiredProcurementInitiationDate) #><br/>" +
"<b>Required Date of Award: </b><#= $.telerik.formatString('{0:MM/dd/yyyy}', RequiredDateOfAward==null ? \"\" : RequiredDateOfAward) #><br/>" +
You need to make a call into $.telerik.formatString(...)
Related
Conditional Formating in Telerik Grid (MVC)
I want to change the cell background color based on a value. My solution If ound and which worked was this: #(Html.Kendo().Grid<Web.Models.Intern.CheckADGridModel>() .Name("ADGrid") .Filterable() .Sortable(s => s.SortMode(GridSortMode.MultipleColumn)) .Pageable(page => page.Enabled(true)) .Columns(col => { col.Bound(x => x.ProjectID).Filterable(true).Title("ID").Width(100); col.Bound(x => x.ProjectName).Filterable(true).Title(ResourcesLocal.Resources_Intern_CheckAD.Name).ClientTemplate( "<a href=\"" + #Url.Content("~/Home/Index/") + "#= ProjectID #\"" + "><span data-toggle=\"tooltip\" data-placement=\"bottom\" title=\"" + #ResourcesLocal.Resources_Intern_CheckAD.ChooseProject + "\">" + "#= ProjectName #" + "</span ></a>"); col.Bound(x => x.StartDate).Filterable(false).Title(ResourcesLocal.Resources_Intern_CheckAD.Start).Width(150).Format("{0:dd.MM.yyyy HH:mm:ss}"); col.Bound(x => x.EndDate).Filterable(false).Title(ResourcesLocal.Resources_Intern_CheckAD.Ende).Width(150).Format("{0:dd.MM.yyyy HH:mm:ss}"); col.Bound(x => x.ConnectionLength).Filterable(true).Title(ResourcesLocal.Resources_Intern_CheckAD.Length).Width(70) .ClientTemplate("#= formatADGridDauer(ConnectionLength) #"); .DataSource(ds => ds .Ajax() .PageSize(20) .Sort(x => x.Add("EndDate").Descending()) .Read(read => read.Action("DataSourceADGrid", "Intern").Data("GetADGridValues")) ) ) function formatADGridDauer(value) { var css = ""; if (parseFloat(value) >= 1800) css = "background-color:red; color:white;"; else if (parseFloat(value) > 600) css = "background-color:Orange; color:white;"; html = "<div style='" + css + "'>" + value + "</div>"; html = kendo.format(html); return html; } The problem is, I can only change the behaviour of the data in the cell, which means the padding of the grid-cell will be shown and it looks really ugly. So how to do it right an change the gridcell background color instead of the data inside?
Unfortunately, telerik is not the most user-friendly set of controls when it comes to customization. Javascript (jquery in this instance) has worked for me when I had to do something similar. Try this something like this: $(".k-grid tr td").filter(function () { //gets the generated table cells if ($(this).text() > 0) { $(this).css("background-color", "red"); } });
change group header dynamically in kendo ui mvc
I am using kendo ui with asp.net mvc. I want to change the group header coloumn dynamically. Please suggest solution.My code is shown below. <div id="dvPayrollReportGrid" class="kendo-responsive-grid-content"> #(Html.Kendo().Grid<CyberPayBO.Models.PayrollReportModel>() .Name("gridpayrollreport") .Columns(columns => { columns.Bound(payroll => payroll.CompanyCode).Title("").Hidden(true).Filterable(false).ClientTemplate("<input type='hidden' id='hdnCompanyCode' value='#=CompanyCode#'>"); columns.Bound(payroll => payroll.CompanyCode).Title("Action").Filterable(false).ClientTemplate("View || <span class='CompanyNameLink DeleteLink' onclick='DeletePayrollReport(this.id)' id='#=ID#'>Del</span> | <img src='../Images/pdf.gif' id='PayrollImage'/>").Sortable(false); columns.Bound(payroll => payroll.CheckDate).Format("{0:dd/MM/yyyy}"); columns.Bound(payroll => payroll.FromDate).Format("{0:dd/MM/yyyy}"); columns.Bound(payroll => payroll.ToDate).Format("{0:dd/MM/yyyy}"); columns.Bound(p => p.StoredFile).ClientTemplate("#if (StoredFile.toLowerCase().indexOf('.pdf')>-1) {#" + " " + "#} else { #" + "" + "# } #" + "#=StoredFile#"); columns.Bound(payroll => payroll.CheckDate).Title("Payroll Reports").Hidden(Convert.ToBoolean(PayrollReportColumn)).ClientTemplate("<span style='color:red'>Payroll Report</span>"); columns.Bound(payroll => payroll.CreatedDate).Title("Created At").Format("{0:dd/MM/yyyy HH:mm tt}"); }) .Pageable(pageable => pageable .Messages(messages => messages.Display("Payroll Reports {0} - {1} of {2}")) ) .Sortable() .DataSource(dataSource => dataSource .Ajax() .PageSize(PageSize) .Read(read => read.Action("PayrollReport_Read", "PayrollReport")) .Group(groups => { groups.Add(payroll => payroll.CompanyName); }) ) ) </div> i am grouping by the company name, and by default it is giving me company name as header in result. I want the header to be dynamic and display the company code instead.I am getting ouput as CompanyName: AAA TEST 1 in header, but i want ouput as 004:AAA Test 1., where 004 is company code.
I found the solution to it by using ClientGroupHeader Template columns.Bound(payroll => payroll.Company).ClientGroupHeaderTemplate("#=value#").Hidden(true); Here company is combination of CompanyCode and CompanyName and which i bind from repository. Finally grouping on Company gives me the desired result.
To Achieve this you need to return a new column in your backend With the class of the data you want to group by since Kendo doesn't let you group by anonymous type yet. the End the end of your Action will look something like this ... var query = db.PayrollReportModel.Select(payroll=> new PayrollReportModel() { payroll.CompanyCode ... grouper = new GrouperClass() { CompanyCode = payroll.CompanyCode, CompanyName = payroll.CompanyName } }); DataSourceResult result = await query.ToDataSourceResultAsync(request); } For your View <div id="dvPayrollReportGrid" class="kendo-responsive-grid-content"> #(Html.Kendo().Grid<CyberPayBO.Models.PayrollReportModel>() .Name("gridpayrollreport") .Columns(columns => { columns.Bound(payroll => payroll.grouper).Hidden(true).ClientGroupHeaderTemplate("#=value.CompanyCode#-#=value.CompanyName#"); columns.Bound(payroll => payroll.CompanyCode).Title("Action").Filterable(false).ClientTemplate("View || <span class='CompanyNameLink DeleteLink' onclick='DeletePayrollReport(this.id)' id='#=ID#'>Del</span> | <img src='../Images/pdf.gif' id='PayrollImage'/>").Sortable(false); columns.Bound(payroll => payroll.CheckDate).Format("{0:dd/MM/yyyy}"); columns.Bound(payroll => payroll.FromDate).Format("{0:dd/MM/yyyy}"); columns.Bound(payroll => payroll.ToDate).Format("{0:dd/MM/yyyy}"); columns.Bound(p => p.StoredFile).ClientTemplate("#if (StoredFile.toLowerCase().indexOf('.pdf')>-1) {#" + " " + "#} else { #" + "" + "# } #" + "#=StoredFile#"); columns.Bound(payroll => payroll.CheckDate).Title("Payroll Reports").Hidden(Convert.ToBoolean(PayrollReportColumn)).ClientTemplate("<span style='color:red'>Payroll Report</span>"); columns.Bound(payroll => payroll.CreatedDate).Title("Created At").Format("{0:dd/MM/yyyy HH:mm tt}"); }) .Pageable(pageable => pageable .Messages(messages => messages.Display("Payroll Reports {0} - {1} of{2}")) ) .Sortable() .DataSource(dataSource => dataSource .Ajax() .PageSize(PageSize) .Read(read => read.Action("PayrollReport_Read", "PayrollReport")) .Group(groups => { groups.Add(payroll => payroll.grouper); }) ) ) </div> Hope this helps
Telerik Client Template Conditions
I am using Telerik MVC Grid control to show a data grid. The detail of my grid is calling the following Client Detail template: <script id="client-template" type="text/x-kendo-template"> <%: Html.Kendo().Grid<ASML_Scheduler.ScheduleService.AgentViewData>() .Name("grid_#=WorkgroupName#") .DataSource(dataSource => dataSource.Ajax().Read(read => read.Action("Agents_Read", "Home", new {workgroupname= "#=WorkgroupName#", name = ViewData["LoggedInUser"] })) ) .Columns(columns => { columns.Bound(product => product.AgentName).ClientTemplate("<strong>\\#:AgentName\\#</strong>"); //columns.Bound(product => product.IsLoggedOn); //columns.Bound(product => product.IsLoggedOn).ClientTemplate("<div class='mystyle'>\\#:IsLoggedOn\\#</div>"); columns.Bound(product => product.IsLoggedOn).ClientTemplate( "# if (IsLoggedOn != false) { #" + "<div class='mystyle'>\\#:IsLoggedOn\\#</div>" + "# } else { #" + "<div>\\#:IsLoggedOn\\#</div>" + "# } #" ); columns.Bound(product => product.IsScheduled); }) .ToClientTemplate() %> </script> The problem I have is with the IsLoggedOn client template as it does not recognise the IsLoggedOn != false. Can anyone see what I havedone wrong here.
personally with this sort of thing I prefer doing this. change this: columns.Bound(product => product.IsLoggedOn).ClientTemplate( "# if (IsLoggedOn != false) { #" + "<div class='mystyle'>\\#:IsLoggedOn\\#</div>" + "# } else { #" + "<div>\\#:IsLoggedOn\\#</div>" + "# } #" ); to columns.Bound(product => product.IsLoggedOn) .ClientTemplate("#=StyleLogin(data.IsLoggedOn)#"); then create a javascript function function StyleLogin(IsLoggedOn) { var value = ''; if (IsLoggedOn != false) { value = '<div class="mystyle">' + IsLoggedOn + '</div>'; } else { value = '<div>' + IsLoggedOn + '</div>'; } return value; } This way you can easily debug the code and also reuse the function elsewhere if needed.
kendo grid column with conditionally chosen action
Here is what I got: columns.Bound(t => t.Id) .Title("") .Template(#<text></text>) .ClientTemplate("<a class=\"k-button\" href='" + Url.Action("Edit", "Controller") + "/#=Id#'>Edit</a>") .Width(110); What I need is to pick a specific controller action depending on type of object bound. (different form for e.g. CarEdit when lets say Type==1 and PlaneEdit when Type==2) I've done similar thing using JS recently (to produce ClientTemplate content) but would greatly appreciate solution without nasty JS.
As for now this is my best solution: columns.Bound(t => t.Id) .Title("") .Template(#<text></text>) .ClientTemplate("#= GetEditTemplate(data)#") .Width(110); function GetEditTemplate(data) { var html; if (data.Type === 1) { html = kendo.format("<a class=\"k-button\" href='" + '#Url.Action("Edit1", "Controller")' + "/{0}" + " '>Edit</a> ", data.Id ); } else { html = kendo.format("<a class=\"k-button\" href='" + '#Url.Action("Edit2", "Controller")' + "/{0}" + " '>Edit</a> ", data.Id ); } return html; }
Help with Jagged Array needed, getting IQueryable results into an array
Does anyone know how I can get my results currently in qry IQueryable object into a jagged array in the format: series: [{ name: '2', data: [0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0] }, { name: '3', data: [1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 3, 0] }] My problem is the code I have at the moment wraps double quotes around the 12 element data part, i.e. it looks like this in the debugger on client side: ?result[0] {...} [0]: "2" [1]: "[0,0,0,0,0,0,1,0,0,0,0,0]" ?result[1] {...} [0]: "3" [1]: "[1,0,0,0,0,0,0,0,0,0,0,0]" The problem is my array contains a single string element rather than an array of 12 numbers. Here is my controller code that at the moment returns the 12 number part as one big string : var qry = from i in _db.Complaints where i.Site.SiteDescription.Contains(searchTextSite) && (i.Raised >= startDate && i.Raised <= endDate) group i by i.ComplaintNatureTypeId.ToString() into grp select new { Type = grp.Key, Count = "[" + grp.Where(c => c.Raised.Month == 1).Count() + "," + grp.Where(c => c.Raised.Month == 2).Count() + "," + grp.Where(c => c.Raised.Month == 3).Count() + "," + grp.Where(c => c.Raised.Month == 4).Count() + "," + grp.Where(c => c.Raised.Month == 5).Count() + "," + grp.Where(c => c.Raised.Month == 6).Count() + "," + grp.Where(c => c.Raised.Month == 7).Count() + "," + grp.Where(c => c.Raised.Month == 8).Count() + "," + grp.Where(c => c.Raised.Month == 9).Count() + "," + grp.Where(c => c.Raised.Month == 10).Count() + "," + grp.Where(c => c.Raised.Month == 11).Count() + "," + grp.Where(c => c.Raised.Month == 12).Count() + "]" }; return Json(qry.ToArray(), JsonRequestBehavior.AllowGet);
You need to pass an object and Json will serialize it to a JSON string. Count = new int[] { grp.Where(c => c.Raised.Month == 1).Count(), grp.Where(c => c.Raised.Month == 2).Count(), grp.Where(c => c.Raised.Month == 3).Count(), grp.Where(c => c.Raised.Month == 4).Count(), grp.Where(c => c.Raised.Month == 5).Count(), grp.Where(c => c.Raised.Month == 6).Count(), grp.Where(c => c.Raised.Month == 7).Count(), grp.Where(c => c.Raised.Month == 8).Count(), grp.Where(c => c.Raised.Month == 9).Count(), grp.Where(c => c.Raised.Month == 10).Count(), grp.Where(c => c.Raised.Month == 11).Count(), grp.Where(c => c.Raised.Month == 12).Count() } Or you could do something like: Count = Enumerable.Range(1, 12).Select(x => grp.Where(c => c.Raised.Month == x).Count())
You are creating the count property as a string, should it not be an array: Count = new [] { grp.Where(c => c.Raised.Month == 1).Count(), grp.Where(c => c.Raised.Month == 2).Count(), ... grp.Where(c => c.Raised.Month == 12).Count() }