How to change Kendo MVC grid's edit command template? - kendo-ui

I am using Kendo UI for ASP.NET MVC. I have grid with edit command. The default look of the edit command is "button" and i wanted to change it to link. But there is no Template() method for command. So how do i change the edit command button to link?
Telerik has option to create own custom command as defined here. but my grid is configured to use GridEditMode.Popup which works great with inbuilt edit command. If i create custom command then i guess i have to wireup popup window & everything else.
I just wanted to change "button" to link?
#(Html.Kendo().Grid<UI.Models.GridVM>()
.Name("Grid")
.Columns(columns =>
{
columns.Bound(p => p.CampaignID)
columns.Bound(p => p.CampaignStatus);
columns.Command(command => command.Edit().Text("Edit Me")); // How do i change this to link??
})
.Editable(editable => editable
.Mode(GridEditMode.PopUp)
.TemplateName("CampaignEdit")
.Window(w =>
{
w.Width(400);
w.Title("Edit Details");
}))
.Filterable()
.Pageable()
.Navigatable()
.Sortable()
.DataSource(dataSource => dataSource
.Ajax()
.ServerOperation(false)
.PageSize(20)
.Model(model => model.Id(p => p.CampaignID))
.Read(read => read.Action("GetCampaigns", "Home"))
.Update(update => update.Action("UpdateCampaign", "Home"))
)
)
UPDATE1
#Steve Greene Thanks. Your approach did work on master grid. But i also have child detail grid which has edit link. The approach didnt work for detail grid. Kendo throws error.
I think we have to escaped template expression, to be evaluated in the child/detail context. But i am not sure the whats the syntax
#(Html.Kendo().Grid<UI.Models.GridVM>()
.Name("Grid")
.Columns(columns =>
{
columns.Bound(p => p.CampaignID)
columns.Bound(p => p.CampaignStatus);
columns.Template(#<text></text>)
.ClientTemplate(#"<a class=""k-grid-edit"" href=""\#"">Edit Master</a>");
//Worked in master grid
})
.Editable(editable => editable
.Mode(GridEditMode.PopUp)
.TemplateName("CampaignEdit")
.Window(w =>
{
w.Width(400);
w.Title("Edit Details");
}))
.Filterable()
.Pageable()
.Navigatable()
.Sortable()
.DataSource(dataSource => dataSource
.Ajax()
.ServerOperation(false)
.PageSize(20)
.Model(model => model.Id(p => p.CampaignID))
.Read(read => read.Action("GetCampaigns", "Home"))
.Update(update => update.Action("UpdateCampaign", "Home"))
)
.ClientDetailTemplateId("detailtemplate")
)
<script id="detailtemplate" type="text/kendo-tmpl">
#(Html.Kendo().Grid<UI.Models.DetailGridVM>()
.Name("detailgrid_#=CampaignID#")
.Columns(columns =>
{
columns.Bound(o => o.CampaignDetailID);
columns.Bound(o => o.Notes);
columns.Bound(o => o.CreatedBy);
columns.Template(#<text></text>)
.ClientTemplate(#"<a class=""k-grid-edit"" href=""\#"">Edit Detail</a>");
// Does not work in detail grid
})
.DataSource(dataSource => dataSource
.Ajax()
.PageSize(5)
.Read(read => read.Action("GetCampaignDetails", "Home", new { campaignID = "#=CampaignID#" }))
.Update(update => update.Action("UpdateCampaignDetails", "Home"))
.Model(model => model.Id(m => m.CampaignDetailID))
)
.Pageable()
.Sortable()
.ToClientTemplate())
</script>

Use a column template that has the k-grid-edit class on it (use k-grid-delete for destroy):
.Columns(columns =>
{
columns.Bound(p => p.CampaignID)
columns.Bound(p => p.CampaignStatus);
columns.Template(#<text></text>).ClientTemplate(#"<a class=""k-grid-edit"" href=""\#"">Edit</a>").Width(30);
columns.Template(#<text></text>).ClientTemplate(#"<a class=""k-grid-delete"" href=""\#"">Delete</a>").Width(30);
})
or for smaller buttons and bootstrap:
column.Template(#<text></text>).ClientTemplate(#"<a class=""btn btn-info btn-xs k-grid-edit"" href=""\#"">Edit</a>").Width(30);
column.Template(#<text></text>).ClientTemplate(#"<a class=""btn btn-danger btn-xs k-grid-delete"" href=""\#"">Delete</a>").Width(30);

For example you can do that with css only:
td[role=gridcell] > a.k-button.k-button-icontext.k-grid-edit :hover {
cursor: pointer;
}
td[role=gridcell] > a.k-button.k-button-icontext.k-grid-edit {
-webkit-box-shadow: none !important;
box-shadow: none !important;
background: transparent;
border: none;
}

Related

Kendo UI Custom Pop-up Editor in Hierarchical Grid

I have a Hierarchical grid and I'm trying to add a custom editor for pop-up editing. When I add the template to the child grid and click on the "edit" button, i get a invalid template error. If i add that same template to the parent, it works fine.
Here is the error in console:
Uncaught Error: Invalid template:'
Here is the code
#(Html.Kendo().Grid<ParentViewModel>()
.Name("GridAdjax")
.DataSource(dataSource => dataSource
.Ajax()
.PageSize(20)
.Events(events => events.Error("error_handler"))
.Read(read => read.Action("Read", "controller"))
.Model(model =>
{
model.Id(c => c.Id);
})
.ServerOperation(false)
.Events(events => events.Error("error_handler"))
.Events(events => events.RequestEnd("onRequestEnd"))
)
.Columns(columns =>
{
columns.Bound(p => p.CompanyName).Title("Company Name");
columns.Bound(p => p.CompanyDomain).Title("Company Domain");
columns.Bound(p => p.CompanySecurityRole).Title("Security Role");
columns.Bound(p => p.CompanySecurityGroup).Title("Security Group");
})
.ClientDetailTemplateId("template")
.Pageable()
.Sortable()
.Resizable(resize => resize.Columns(true))
.Events(e => e.DataBound("OnDataBound"))
.Deferred()
)
<CHILD>
<script id="template" type="text/kendo-tmpl">
#(Html.Kendo().Grid<ChildlViewModel>()
.Name("grid_#=CompanyId#")
.DataSource(dataSource => dataSource
.Ajax()
.PageSize(10)
.Update(update => update.Action("CompanyList_Update", "Administration"))
.Read(read => read.Action("Read", "Child", new { companyId = "#=CompanyId#" }))
.Model(model =>
{
model.Id(c => c.Id);
model.Field(c => c.CompanySoldTo).Editable(true);
model.Field(c => c.CompanyDistributionChannel).Editable(true);
model.Field(c => c.CompanyDivision).Editable(true);
model.Field(c => c.CompanyPlant).Editable(true);
model.Field(c => c.CompanySalesOrg).Editable(true);
})
)
.Columns(columns =>
{
columns.Bound(p => p.CompanySoldTo).Title("Sold To");
columns.Bound(p => p.CompanyDistributionChannel).Title("Dist. Chan.");
columns.Bound(p => p.CompanyPlant).Title("Plant");
columns.Bound(p => p.CompanySalesOrg).Title("Sales Org");
columns.Command(command => { command.Edit(); }).Title(("Edit SAP Info."));
})
.Editable(ed => ed.Mode(GridEditMode.PopUp).TemplateName("Detail"))
.Pageable()
.Sortable()
.ToClientTemplate()
)
</script>
<script>
function dataBound() {
this.expandRow(this.tbody.find("tr.k-master-row").first());
}
</script>
If anyone experiences this issue in the future, the problem was actually in my custom pop-up editor template. I had a data validation message on one of the input boxes set as "Please enter your account #". The "#" was being interpreted as a template by Kendo. I escaped the # and then was able to get it to successfully load.

Telerik grid column command , add to url # on click

I have this telerik grid in a asp.net mvc project
<div class="actualGrid" id="actualGrid">
#(Html.Kendo().Grid<AVNO_KPMG.Models.Bench>() //Bench Grid
.Name("grid")
.Columns(columns =>
{
columns.Bound(p => p.name).Title("Bench").Filterable(ftb => ftb.Cell(cell => cell.Operator("startswith"))).Width(100);
columns.Bound(p => p.freeSeats).Title("Free Seats").Width(200).Filterable(ftb => ftb.Cell(cell => cell.Operator("gte"))).HtmlAttributes(new { #class = "FreeSeats" })
.ClientTemplate("<div class='barthingy'><div class='bars_text'><div class='seatsText'><span class=\"bookNotfull\"></span> <b>#=bookedSeats#</b> USED SEATS</div><div class='seatsText'><span class=\"bookfull\"></span> <b>#=freeSeats#</b> TOTAL OFSEATS</div></div><div id='bigbar'><div class='bigbar' style='width:100%; float:left; background-color:rgb(142, 188, 0);'><div ' style='float:right; width:#=bookedSeats *100 / seatsCount#%; background-color:rgb(255, 99, 71); height:16px ' class='b_#=name#' id='temp-log'></div></div></div></div>");
//buttons
columns.Command(command => { command.Custom("checkBench1 ").Text(" AM ").Click("doCheckIn"); command.Custom("checkBench 2").Text(" PM ").Click("doCheckIn"); command.Custom("checkBench3").Text("All Day").Click("doCheckIn"); }).HtmlAttributes(new { #class = "comms#=freeSeats# freeAM#=seatsCount - (bookings_am + bookings_allday)# freePM#=seatsCount - (bookings_pm + bookings_allday)# freeALLDAY#=freeSeats#" }).Title("Check in").Width(200);
})
.Pageable()
.Sortable()
.Scrollable(scrolling => scrolling.Enabled(false))
.Filterable(ftb => ftb.Mode(GridFilterMode.Row))
//.HtmlAttributes(new { style = "height:530px;" })
.Events(events => events.DataBound("onDataBound"))
.DataSource(dataSource => dataSource
.Ajax()
//.Sort(sort => sort.Add("freeSeats").Ascending())
.PageSize(10)
.Events(events => events.Error("error_handler"))
.Model(model => model.Id(p => p.id))
.Read(read => read.Action("GetBenches", "Deskcheckin"))
)
)
</div>
Everytime i press on of the buttons in the command column, the url gets a # in front of it, even if i set the buttons to do nothing.
my url is something like
http://www.aaaaaa.com/stuff
When i press one of the buttons i get
http://www.aaaaaa.com/stuff#
How can i disable this?
Please try with the below code snippet. For custom command button grid generate anchor control for same. By default it set href='#' and I have replaced it with href='javascript:void(0)'.
<div>
#(Html.Kendo().Grid<MvcApplication1.Models.Student>()
.Name("grid")
.Columns(columns =>
{
..........
..........
columns.Command(command => { command.Custom("checkBench1").Text("AM").Click("doCheckIn"); }).Title("Check in").Width(200);
})
.Events(events => events.DataBound("onDataBound"))
..........
..........
)
</div>
<script>
function doCheckIn() {
alert('a');
}
function onDataBound(arg) {
$("#.k-grid-checkBench1").attr('href', 'javascript:void(0)');
}
</script>
Let me know if any concern.

MVVM pattern in Kendo Grid

I'm using the Kendo Grid control to display some data and it's working OK. This is the code:
#(Html.Kendo().Grid<MyModel>()
.Name("MyGrid")
.ToolBar(toolbar =>
{
toolbar.Create().Text("Add New Line").HtmlAttributes(new { #Class = "btn btn-primary" });
})
.Columns(columns =>
{
columns.Bound(p => p.Description).HtmlAttributes(new {style = "font-size: 9pt;"});
columns.Bound(p => p.Value);
columns.Command(cmd =>
{
cmd.Edit();
cmd.Destroy();
}).Width(200);
})
.Editable(editable => editable.Mode(GridEditMode.InLine))
.Selectable(s => s.Mode(GridSelectionMode.Multiple).Type(GridSelectionType.Row))
.DataSource(data => data
.Ajax()
.Events(events => events.Error("error_handler"))
.Model(model => model.Id(p => p.Id))
.ServerOperation(false)
.Read(read => read.Action("ReadAction", "MyController"))
.Create(create => create.Action("CreateAction", "MyController"))
.Update(update => update.Action("UpdateAction", "MyController"))
.Destroy(delete => delete.Action("DeleteAction", "MyController"))
))
The code above is working without any problem. Now, what I'm trying to implement is to display the same data using the MVVM pattern. I mean, I would like to have the same operations (create, update, delete) in my viewmodel without calling the server actions of my controller as the code above does.
I have google it for a while but without any luck.
Any help would be very appreciate

dropdown in grid background and text different color

I'm trying to add a dropdownlist in a telerik mvc grid using column.Bound I can get the dropdownlist to display but initially it displays as a textbox. Apparently if I use an editor template it should work but I get the error value cannot be null?
The aim is to display a dropdownlist in a grid, each item will have a different text color and background. This needs to be populated via model properties.
At the moment I'm using ViewData just to get things working but no joy. Was advised this can be done by Templates.
Any ideas why this isn't working?
#using System.Collections.Generic;
#model IEnumerable<TelerikChecklist.Models.ProductViewModel>
#(Html.Kendo().Grid(Model)
.Name("gridDropDown")
.Columns(columns =>
{
columns.Bound(p => p.ProductName);
//columns.ForeignKey(p => p.CategoryID, (System.Collections.IEnumerable)ViewData["categories"], "CategoryID", "CategoryName")
// .Title("Category")
// .Width(150);
columns.Bound(p => p.CategoryID).Title("Category Name").ClientTemplate((#Html.Kendo().DropDownList()
.Name("dropdown_#=CategoryID#")
.BindTo((System.Collections.IEnumerable)ViewData["categories"])
.DataTextField("CategoryName")
.DataValueField("CategoryID")
.ValueTemplate("")
.ToClientTemplate().ToString()
)).EditorTemplateName("GridForeignKey");
})
.Editable(editable => editable.Mode(GridEditMode.InCell))
.Pageable()
.Scrollable()
.HtmlAttributes(new { style = "height:250px;" })
.DataSource(dataSource => dataSource
.Ajax()
.Batch(true)
.PageSize(20)
.ServerOperation(false)
.Events(events => events.Error("errorHandler"))
.Model(model =>
{
model.Id(p => p.ProductID);
model.Field(p => p.ProductID).Editable(false);
model.Field(p => p.CategoryID).DefaultValue(1);
})
.Read(read => read.Action("ForeignKeyColumn_Read", "Home"))
.Update(update => update.Action("ForeignKeyColumn_Update", "Home")).Events(e => e.Change("Category"))
.Create(create => create.Action("ForeignKeyColumn_Create", "Home"))
.Destroy(destroy => destroy.Action("ForeignKeyColumn_Destroy", "Home"))
)
)
The GridForeignKey.cshtml
#model object
#(
Html.Kendo().DropDownListFor(m => m)
.BindTo((SelectList)ViewData[ViewData.TemplateInfo.GetFullHtmlFieldName("") + "_Data"])
)
resolved by creating another template:
columns.Bound(p => p.Category.CategoryName).Title("CategoryName").EditorTemplateName("GridForeignKey2");
#using System.Collections;
#(Html.Kendo().DropDownList()
.Name("NeatProperty")
.DataTextField("CategoryName")
.DataValueField("CategoryID")
.BindTo((System.Collections.IEnumerable)ViewData["categories"])
.Template("<div style='background-color:#: data.CategoryColor #'><span class=\"k-state-default\"></span>" + "<span class=\"k-state-default\"><p style='color:#: data.CategoryTextColor #;'>#: data.CategoryName #</p></span></div>")
.ValueTemplate("<div style='background-color:#: data.CategoryColor #;'><span>#:data.CategoryName#</span></div>")
)
Seen this in another post on stackoverflow but lost the link so couldn't post.

The best overloaded method match for Kendo.Mvc.UI.Fluent.GridEditingSettingsBuilder

The best overloaded method match for Kendo.Mvc.UI.Fluent.GridEditingSettingsBuilder Mode(Kendo.Mvc.UI.GridEditMode) has some invalid arguments .Filterable() and .Scrollable()
This is the error I get while I run the project which has the KendoUI Grid Control.
The code is similar to what is there in the KendoUI website , however on my project I do get this error. I am able to get the Filterable() option in the intellisense but am not sure why the error is thrown . I can see the error in Firebug and it is not thrown by the webpage as a yellow screen of death.
Any help would be appreciated.
My sample code is as below
CS HTML Code
#using Kendo.Mvc.UI;
#model IEnumerable<MvcApplication29.Models.CustomItem>
<div class="absolute-position">
<div class="absolute-position">
#(Html.Kendo().Grid(Model)
.Name("grid")
.Columns(columns =>
{
columns.Bound(o => o.No).Width("15%");
columns.Bound(o => o.ShortDesc).Width("15%");
columns.Bound(o => o.Category).Width("6%");
})
.Sortable()
.Pageable(p=>p.Refresh(true))
.Filterable()
.Scrollable()
.Editable(edit => edit.DisplayDeleteConfirmation("Are You Sure To Delete This ").Mode(GridEditMode.PopUp))
.ColumnMenu(col=>col.Sortable(false))
.Groupable()
.ToolBar(toolbar => toolbar.Create())
.Resizable(resize => resize.Columns(true))
.Reorderable(reorder => reorder.Columns(true))
//.ClientDetailTemplateId("template")
.HtmlAttributes(new { style = "height:430px;" })
.DataSource(dataSource => dataSource
.Ajax()
.PageSize(6)
.Read(read => read.Action("GetItemsHome", "det"))
.Model(model => {
model.Id(p => p.ID);
})
.Create(update => update.Action("EditingInline_Create", "det"))
// .Read(read => read.Action("EditingInline_Read", "Default1"))
.Update(update => update.Action("EditingInline_Update", "det"))
.Destroy(update => update.Action("EditingInline_Destroy", "det"))
)
)
</div>
</div>
///CS Code
public ActionResult GetItemsHome1([DataSourceRequest] DataSourceRequest request , int page)
{
List<CustomItem> lst = new List<CustomItem>();
return Json(lst.ToDataSourceResult(request));
}

Resources