I have a model with an enum as a data type on a property. In the grid I want to show an image representation for this enum. Whats the best way to do this?
I've tried using the ClientTemplate to do this. I'm not sure what I need. I have tried the following without success:
@(Html.Kendo().Grid<BatchEditModel>()
.Name(
"mygrid"
)
.Columns(columns =>
{
columns.Bound(bem => bem.MyInfo.Number).ClientTemplate(
"# if (MyStatus == "
+ MyStatusConstants.Complete + ") { #
<span
class
=
"icon-oo-my-img-32"
style=
"font-size: 1.2em; color: green"
></span>
"# } #"
"# else if (MyStatus == "
+ MyStatusConstants.PWHTRequired +
") { #"
<span
class
=
"icon-oo-my-img-32"
style=
"font-size: 1.2em; color: darkcyan"
></span>
"# } #"
"# else if (MyStatus == "
+ MyStatusConstants.Myed +
") { #"
<span
class
=
"icon-oo-my-img-32"
style=
"font-size: 1.2em; color: blue"
></span>
"# } #"
)
This has errors in the razor page. I had the all the text wrapped in quotes and used + to concatenate them.
The question is, I have an enum as a value, but I want to display an image - which is a custom glyph icon on the span. What is the best way to implement this?
Thanks
Andez
Hi. I have 2 screen with a Telerik MVC grid. First one is for Orders and second one for OrderDetails. I use server binding.
The first one works fine. It shows Orders data in the grid. Each row has a Details button and when I click on the button, the second screen shows up with the OrderDetails data of the Order data you selected in the grid. Here each row has a Edit button that is supposed to perform inline editing.
At this point, the URL is http://myweb.com/Order/Details/123. 123 is the OrderId in the Orders table.
The problem happens when I click on Edit button for the row data. It does a GET request with OrderDetailId automatically. The page expects OrderId not OrderDetailId.
So, at this point, the page expects http://myweb.com/Order/Details/##OrderId## but the grid does http://myweb.com/Order/Details/##OrderDetailId##.
How should I handle this case? Could I manage what URL format the grid uses for the Edit button?
Hi,
I am trying to create a grid with one column to return a result similar to your example here: http://demos.telerik.com/kendo-ui/grid/rowtemplate. I am using the server-side RowTemplate code such as...
@(Html.Kendo().Grid(Model.Results)
.Name("Components")
.RowTemplate(grid =>
@<text>
<tr>
<td>
ID: @item.ComponentID<br>
Name: @item.Name
... etc
</td>
</tr>
</text>
)
.DataSource(d => d.Ajax().ServerOperation(false))
.Pageable(pageable => pageable
.Refresh(true)
.PageSizes(true)
.ButtonCount(5))
)
I am basing my code on this example:
@model IEnumerable<Product>@(Html.Kendo().Grid(Model).Name("grid")
.RowTemplate(grid => @<text><div>Product Name: @product.ProductName</div><div>Units In Stock: @product.UnitsInStock</div></text>))
The output completely ignores the rowFormat and prints every field in the model object as a separate column (see attached photo).
I have tried almost everything - client templates, etc. Nothing stops every column from being displayed.
Any ideas would be appreciated.
I have two classes Category and Product and I want to expend products when users click a category node on the treeview.
My View is as follows:
<h2>Index</h2>
<p>
@Html.ActionLink("Create New", "Create")
</p>
<div class="row">
<div class="col-md-3">
<label class="k-label-top">TreeView</label>
@(Html.Kendo().TreeView().Name("treeview")
.DataSource(datasource => datasource
.Read(read => read.Action("Categories", "Categories"))
).DataTextField("Name")
)
</div>
</div>
Controller
public ActionResult Categories(int? id)
{
var model = db.Categories
.Where(p => p.Id == id.Value || p.Id == null)
.Select(p => new {
id = p.Id,
Name = p.Name,
hasChildren = p.Products.Any()
});
return this.Json(model, JsonRequestBehavior.AllowGet);
}
Categories Class
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace EmployeeTreeView.Models
{
public class Category
{
public int Id { get; set; }
public string Name { get; set; }
public virtual ICollection<Product> Products { get; set; }
}
}
Products Class
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace EmployeeTreeView.Models
{
public class Product
{
public int Id { get; set; }
public string Name { get; set; }
public virtual Category Category { get; set; }
public int CategoryId { get; set; }
}
}
When I remove the where clause in the controller the parent node just loops over and over again, but no child nodes are displayed.
I have a checkbox (chkRecurring) in the custom editor of the scheduler. I need to show/hide a div (divRecurring) clicking the checkbox. I wrote jquery click event but it doesn't work. And it doesn't throw any error in firebug or any other browser console.
@(Html.Kendo().Scheduler<
TaskViewModel
>()
.Name("schedulerJob")
.Date(DateTime.Today)
.StartTime(7, 0, 0)
.Height(700)
.Views(views =>
{
views.DayView();
views.WeekView();
views.TimelineView(v => v.Selected(true));
})
.Events(e =>
{
e.Edit("schedulerJob_edit");
e.Navigate("schedulerJob_navigate");
e.Remove("schedulerJob_remove");
e.Save("schedulerJob_save");
e.DataBinding("schedulerJob_dataBinding");
e.DataBound("schedulerJob_dataBound");
})
.Group(group => group.Resources("Techs").Orientation(SchedulerGroupOrientation.Vertical))
.Resources(resource =>
{
resource.Add(m => m.TechName)
.Title("Techs")
.Name("Techs")
.DataTextField("TechName")
.DataValueField("emm_code")
.DataSource(d => d.Read("Techs", "JOBS"));
})
.DataSource(d => d
.Model(m =>
{
m.Id(r => r.emm_code);
})
.ServerOperation(true)
.Read(r => r.Action("JobSchedule_Read", "JOBS").Data("passFilter"))
.Create("JobSchedule_Create", "JOBS")
.Update("JobSchedule_Update", "JOBS")
.Destroy("JobSchedule_Delete", "JOBS")
)
.Editable(e => {
e.TemplateId("schedulerJobEditor").Window(w => w.Title("Time Slot Reservation").Name("schedulerJobEditor"));
})
)
<
script
id
=
"schedulerJobEditor"
type
=
"text/x-kendo-template"
>
<
div
>
<
table
style
=
"border-collapse: initial;"
>
<
tr
>
<
td
align
=
"right"
>
<
label
for
=
"Start"
>Job Date:</
label
>
</
td
>
<
td
>
<
input
type
=
"text"
id
=
"jobDateTextbox"
class
=
"k-input k-textbox"
name
=
"start"
data-bind
=
"value: StartViewDate"
style
=
"border-width: 0px;"
disabled
=
"disabled"
>
</
td
>
<
td
align
=
"right"
>
<
label
for
=
"ReservedDate"
>Reserved On:</
label
>
</
td
>
<
td
>
<
input
type
=
"text"
id
=
"reservedDateTextbox"
class
=
"k-input k-textbox"
name
=
"reservedDate"
data-bind
=
"value: ReservationViewDate"
style
=
"border-width: 0px;"
disabled
=
"disabled"
>
</
td
>
</
tr
>
<
tr
>
<
td
align
=
"right"
>
<
label
for
=
"Start"
>Job Time:</
label
>
</
td
>
<
td
>
<
input
type
=
"text"
id
=
"jobTimeTextbox"
class
=
"k-input k-textbox"
name
=
"start"
data-bind
=
"value: StartViewTime"
>
</
td
>
<
td
align
=
"right"
>
<
label
for
=
"ReservedTime"
>Time:</
label
>
</
td
>
<
td
>
<
input
type
=
"text"
id
=
"reservedTimeTextbox"
class
=
"k-input k-textbox"
name
=
"reservedTime"
data-bind
=
"value: ReservationViewTime"
style
=
"border-width: 0px;"
disabled
=
"disabled"
>
</
td
>
</
tr
>
<
tr
>
<
td
align
=
"right"
>
<
label
for
=
"Duration"
>Duration:</
label
>
</
td
>
<
td
>
<
select
name
=
"duration"
id
=
"durationDropDownList"
data-bind
=
"value: Duration"
>
@foreach (var dur in (List<
ServicePROWeb.ServiceProWCFService.TypeMstr
>)HttpContext.Current.Session["DurationList"])
{
<
option
value
=
'@dur.ty_code_desc'
>@dur.ty_code_desc</
option
>
}
</
select
>
</
td
>
<
td
align
=
"right"
>
<
label
for
=
"ReservationID"
>Reservation ID:</
label
>
</
td
>
<
td
>
<
input
type
=
"text"
id
=
"reservationIDTextbox"
class
=
"k-input k-textbox"
name
=
"reservationID"
data-bind
=
"value: ReservationID"
style
=
"border-width: 0px;width:100%"
disabled
=
"disabled"
>
</
td
>
</
tr
>
<
tr
>
<
td
align
=
"right"
>
<
label
for
=
"TechName"
>Tech:</
label
>
</
td
>
<
td
colspan
=
"3"
>
<
input
type
=
"text"
class
=
"k-input k-textbox"
name
=
"techName"
data-bind
=
"value: TechName"
disabled
=
"disabled"
>
</
td
>
</
tr
>
<
tr
>
<
td
align
=
"right"
>
<
label
for
=
"Title"
>Reason for Reservation:</
label
>
</
td
>
<
td
colspan
=
"3"
>
<
input
type
=
"text"
class
=
"k-input k-textbox"
name
=
"title"
data-bind
=
"value: title"
style
=
"width: 60%;"
>
</
td
>
</
tr
>
<
tr
>
<
td
>
</
td
>
<
td
colspan
=
"3"
>
<
input
type
=
"checkbox"
id
=
"chkRecurring"
>Recurring Reservation
</
td
>
</
tr
>
</
table
>
<
br
/>
<
div
id
=
"divRecurring"
style
=
"width: 95%; margin-left: 5px; padding: 10px;"
>
<
input
type
=
"radio"
name
=
"rdoTech"
value
=
"c"
checked
=
"checked"
>Current Tech
<
input
type
=
"radio"
name
=
"rdoTech"
value
=
"a"
style
=
"margin-left: 15px;"
>All Techs
<
span
style
=
"margin-left: 24px;"
>Every: </
span
><
input
type
=
"text"
id
=
"everyTextbox"
class
=
"k-input k-textbox"
name
=
"recurrenceNumber"
data-bind
=
"value: RecurrenceNumber"
>
<
input
type
=
"radio"
name
=
"rdoRecurrenceFrequency"
value
=
"d"
checked
=
"checked"
style
=
"margin-left: 15px;"
>Day(s)
<
input
type
=
"radio"
name
=
"rdoRecurrenceFrequency"
value
=
"w"
style
=
"margin-left: 15px;"
>Weeks(s)
<
input
type
=
"radio"
name
=
"rdoRecurrenceFrequency"
value
=
"m"
style
=
"margin-left: 15px;"
>Month(s)
<
br
/><
br
/>
<
input
type
=
"checkbox"
id
=
"chkIncludeBusinessDays"
>Include Non-Business Days
<
span
style
=
"margin-left: 30px;"
>Until: </
span
><
input
type
=
"text"
id
=
"untilDateTextbox"
class
=
"k-input k-textbox"
name
=
"until"
>
<
span
style
=
"margin-left: 15px;"
>(max. 3 months)</
span
>
</
div
>
</
div
>
</
script
>
$(
'#chkRecurring'
).click(
function
() {
$(
"#divRecurring1"
).hide();
});
Thanks
I am having a play around with the grid. I have implemented the following example where batch update/create/delete from this post kendo-grid-batch-incell-save-changes-with-single-trigger-for-create-update-destroy. This works nicely where I get the rows being created/updated/deleted.
However, if I need to do some custom server side validation in a save event before the changes are committed - if there are any errors, is it possible to return the errors and set them back in the grid supposing my save send handler is:
function
sendSaveAllData() {
var
grid = $(
'#mygrid'
).data(
"kendoGrid"
);
var
finalData = ...;
// send the data with one request to the server
$.ajax({
url:
"@Url.Action("
SaveAllBatchChanges
", "
MyController
")"
,
data: finalData,
type:
"POST"
,
error:
function
() {
//Handle the server errors
},
success:
function
(data) {
//alert("update on server is completed");
showMessage(
'success'
,
'My Batch'
,
'Successfully saved batch changes.'
)
//refresh the grid
grid.dataSource._destroyed = [];
grid.dataSource.read();
}
})
}
With controller method:
public
ActionResult SaveAllBatchChanges(
[Bind(Prefix =
"updated"
)]List<BatchEditModel> updated,
[Bind(Prefix =
"new"
)]List<BatchEditModel> created,
[Bind(Prefix =
"deleted"
)]List<BatchEditModel> destroyed)
{
// do some validation before notifying server
// save if valid
return
Json(
"Success!"
);
}
At the minute I show a popup message to the user which reads success - but I haven't got any code in place currently to do the validate until I find something concrete to put in place.
Ideally I want to set cell style of errors in the grid if possible back in the browser - ie red background colour if the cell contains an error.
Is there a way to manually set error messages in the grid (client side) and highlight the error cells from an external ajax post?
Thanks
Andez
Hello,
I need to format some cell in export excell with kendo mvc grid. I use the export in this way :
.Index.cshtml :
.ToolBar(tools => tools.Excel())
.Excel(excel => excel
.FileName("Export.xlsx")
.ForceProxy(true)
.Filterable(true)
.AllPages(true)
.ProxyURL(Url.Action("ExcelSave", "CieAssistant"))
)
Controller :
public ActionResult ExcelSave(string contentType, string base64, string fileName)
{
var fileContents = Convert.FromBase64String(base64);
return File(fileContents, contentType, fileName);
}
How i can update format of cell in the controller function "ExcelSave" ?!
Thanks,
I am new to Telerik MVC and JavaScript and jQuery.
Here is my issue:
I have two ListViews on my page:
@(Html.Kendo().ListView(Model.ProfessionalsNotInGroup.Lines).Name("staffNotInGroupList").TagName("div").HtmlAttributes(new { style = "height: 580px; overflow: auto;" }).ClientTemplateId("groupTemplate").Selectable(selectable => selectable.Mode(ListViewSelectionMode.Multiple)).Navigatable().DataSource(ds => ds .Sort(sort =>{ sort.Add(f => f.Proname);}).Model(m =>{ m.Id(f => f.Userguid); m.Field(f => f.Proname);})).Deferred())
AND
@(Html.Kendo().ListView(Model.ProfessionalsInGroup.Lines).Name("staffInGroupList").TagName("div").HtmlAttributes(new { style = "height: 580px; overflow: auto;" }).ClientTemplateId("groupTemplate").Selectable(selectable => selectable.Mode(ListViewSelectionMode.Multiple)).Navigatable().DataSource(ds => ds .Sort(sort =>{ sort.Add(f => f.Proname);}).Model(m =>{ m.Id(f => f.Userguid); m.Field(f => f.Proname);})).Deferred())
The template for the ListViews:
<script type="text/x-kendo-tmpl" id="groupTemplate"><div class="staff"><span>#: Proname#</span></div>
I have a button that moves items from one ListView to Another:
function addStaffToGroup(e) {var lvStaffNotInGroupList = $("#staffNotInGroupList").data("kendoListView");var StaffSelections = lvStaffNotInGroupList.select();var lvStaffInGroupList = $("#staffInGroupList").data("kendoListView");for (i = 0; i < StaffSelections.length; i++){ lvStaffInGroupList.dataSource.add(lvStaffNotInGroupList.dataItem(StaffSelections[i])); lvStaffInGroupList.refresh(); lvStaffNotInGroupList.dataSource.remove(lvStaffNotInGroupList.dataItem(StaffSelections[i])); lvStaffNotInGroupList.refresh();}}
The problem being whenever I remove or add items to the ListViews and call refresh() the ListViews are not being sorted alphabetically.
How do I achieve this please?
Thank you.
Hello,
I think I may have a found a bug with how a Grid is displaying data after I've added a record to the table it's bound to. The meat of the problem is in the javascript I'm using to call dataSource.read() after I've added the record. My first attempt looked as follows:
<script>
function
NewDefectFeatureSubmitButtonClick(e)
{
var
defectFeatureGrid = $(
"#DefectFeatureGrid"
).data(
"kendoGrid"
);
var
newDefectFeatureTextBox = $(
"#NewDefectFeatureNameTextBox"
);
var
newDefectFeatureName = newDefectFeatureTextBox.val();
var
dummyUrl =
'@Url.Action("AddDefectFeature", "DefectFeature", new { name = "--V1--" })'
;
var
url = dummyUrl.replace(
"--V1--"
, newDefectFeatureName);
$.ajax(
{
dataType:
"json"
,
type:
"POST"
,
url: url
});
Sleep(1000);
// needed to allow the ajax call above to complete before re-reading the datasource
defectFeatureGrid.dataSource.read();
newDefectFeatureTextBox.val(
""
);
}
</script>
Notice how I'm passing the parameter via the URL...then calling dataSource.read(). This works just fine and the grid updates with the new record...however I felt this looked "hacky" so I wanted to update it to use the data object in the ajax call....which looks like the following:
<script>
function
NewDefectFeatureSubmitButtonClick(e)
{
var
defectFeatureGrid = $(
"#DefectFeatureGrid"
).data(
"kendoGrid"
);
var
newDefectFeatureTextBox = $(
"#NewDefectFeatureNameTextBox"
);
var
newDefectFeatureName = newDefectFeatureTextBox.val();
var
url =
'@Url.Action("AddDefectFeature", "DefectFeature")'
;
$.ajax(
{
data: {
name: newDefectFeatureName
},
dataType:
"json"
,
type:
"POST"
,
url: url
});
Sleep(1000);
// needed to allow the ajax call above to complete before re-reading the datasource
defectFeatureGrid.dataSource.read();
newDefectFeatureTextBox.val(
""
);
}
</script>
notice here how I'm passing the parameter into the data object for the ajax call, then the same as before calling dataSource.read(). The difference being is in this case, the grid does NOT add the new row.
When I look at the database, the record gets added regardless to how the parameter is passed into the ajax call.
When I look at the fiddler traces from the read calls:
I've no idea why this is happening, but it repro's 100% of the time. Is this a bug? If not can someone explain to me why it's not? I have another view where I don't have a choice and I have to pass the parameters via the data object...so I need to get it solved eventually or I'm going to be blocked.
Here's the rest of my source files for context:
Controller:
namespace
TPOReports.Controllers
{
using
System.Web.Configuration;
using
System.Web.Mvc;
using
Kendo.Mvc.UI;
using
Kendo.Mvc.Extensions;
using
TPOReporting;
/// <summary>
/// controller class for dealing with DefectFeature related actions
/// </summary>
public
class
DefectFeatureController : Controller
{
/// <summary>
/// returns the default view for the this controller
/// </summary>
/// <returns>DefectFeatureView</returns>
[HttpGet]
public
ActionResult DefectFeatureView()
{
return
View();
}
/// <summary>
/// adds a new DefectFeature into the database
/// </summary>
/// <param name="name">name of DefectFeature to add</param>
/// <returns>DefectFeatureView</returns>
[HttpPost]
public
ActionResult AddDefectFeature(
string
name)
{
DataMaintenance dataMaintenance =
new
DataMaintenance(WebConfigurationManager.ConnectionStrings[
"DefaultConnection"
].ConnectionString);
dataMaintenance.AddDefectFeature(name);
return
RedirectToAction(
"DefectFeatureView"
);
}
/// <summary>
/// retrieves all DefectFeature objects from the database
/// </summary>
/// <returns>a collection of DefectFeature objects as json</returns>
[HttpPost]
public
ActionResult GetDefectFeatures([DataSourceRequest] DataSourceRequest request)
{
DataMaintenance dataMaintenance =
new
DataMaintenance(WebConfigurationManager.ConnectionStrings[
"DefaultConnection"
].ConnectionString);
var defectFeaturesPayload = dataMaintenance.GetDefectFeatures().Payload;
DataSourceResult result = defectFeaturesPayload.ToDataSourceResult(request);
return
Json(result);
}
}
}
View with working javascript block:
@using TPOReporting
@{
ViewBag.Title = "Defect Feature Maintenance";
Layout = null;
}
<
script
>
function NewDefectFeatureSubmitButtonClick(e)
{
var defectFeatureGrid = $("#DefectFeatureGrid").data("kendoGrid");
var newDefectFeatureTextBox = $("#NewDefectFeatureNameTextBox");
var newDefectFeatureName = newDefectFeatureTextBox.val();
var dummyUrl = '@Url.Action("AddDefectFeature", "DefectFeature", new { name = "--V1--" })';
var url = dummyUrl.replace("--V1--", newDefectFeatureName);
$.ajax(
{
dataType: "json",
type: "POST",
url: url
});
Sleep(1000); // needed to allow the ajax call above to complete before re-reading the datasource
defectFeatureGrid.dataSource.read();
newDefectFeatureTextBox.val("");
}
</
script
>
<
div
>
<
h2
>Defect Features</
h2
>
<
hr
size
=
"3"
/>
</
div
>
<
div
>
<
table
>
<
tr
>
<
td
>
<
label
>New Defect Feature Name:</
label
>
</
td
>
<
td
>
@(Html.Kendo().TextBox()
.Name("NewDefectFeatureNameTextBox"))
</
td
>
<
td
>
@(Html.Kendo().Button()
.Name("NewDefectFeatureSubmitButton")
.Content("Submit")
.Events(e => e.Click("NewDefectFeatureSubmitButtonClick")))
</
td
>
</
tr
>
</
table
>
</
div
>
<
div
>
@(Html.Kendo().Grid<
DefectFeature
>()
.Columns(c =>
{
c.Bound(df => df.Name)
.Title("Name");
c.Bound(df => df.LastUpdate)
.Title("Last Update");
})
.DataSource(ds => ds
.Ajax()
.Read(r => r.Action("GetDefectFeatures", "DefectFeature")))
.HtmlAttributes(new { style = "height:300px;" })
.Name("DefectFeatureGrid")
.Scrollable()
.Selectable()
.Sortable())
</
div
>