Here is my kendo grid code:
#(Html.Kendo().Grid(Model)
.Name("paymentGrid")
.Columns(columns =>
{
columns.Bound(p => p.AccountName).Title("Account Name");
columns.Bound(p => p.Active).Title("Active").ClientTemplate("<div>#=Active ? 'Active' : 'Inactive'#</div>");
columns.Command(command => command.Custom("DeActivate").Click("deActivatePaymentAccount").Text("DeActivate")).Title("DeActivate");
})
.Filterable()
.Sortable()
.Pageable(paging => paging.Enabled(true).PageSizes(true).Messages(messages => messages.Empty("No accounts found")))
.Editable(editable => editable.Mode(GridEditMode.InLine))
.DataSource(dataSource => dataSource
.Ajax()
.ServerOperation(false)
.Model(model =>
{
model.Id(p => p.AccountId);
})
.Update(update => update.Action("EditAccount", "Account"))
)
)
Question:
How do I add a client Template to my Custom Command (Deactivate) so I can toggle the text on the button based on whether the account is active or not?
I think that the best way to change the button text is to do this in the javascript function bound to the click event :
function deActivatePaymentAccount(e) {
var $btn = $(e.target);
$btn.text() === "DeActivate" ? $btn.text("Activate") : $btn.text("DeActivate");
// some other code here
}
Related
I have a Telerik Kendo Grid and I am adding a record when I click a button:
var dataSource = $("#GridArticulos").data("kendoGrid").dataSource;
dataSource.add({ ArticuloId: articuloId, Nombre: nombre, Precio:
precio, Cantidad: cantidad, Total: total });
dataSource.sync();
The record is added correctly to the grid, but when I click the save button on the grid, nothing happens, the action in the controller is not called, do I need some specific configuration on the grid?, this is what I have now:
#(Html.Kendo().Grid<App.Models.ArticulosListaViewModel>()
.Name("GridArticulos")
.AutoBind(false)
.Columns(columns =>
{
columns.Bound(c => c.ArticuloId).Width("15%");
columns.Bound(c => c.Nombre).Width("35%").ClientFooterTemplate("Total"); ;
columns.Bound(c => c.Cantidad).Width("10%").HtmlAttributes(new { style = "text-align:right" });
columns.Bound(c => c.Precio).Width("10%").Format("{0:C}").HtmlAttributes(new { style = "text-align:right" });
columns.Bound(c => c.Total).Width("10%").Format("{0:C}").HtmlAttributes(new { style = "text-align:right" })
.ClientFooterTemplate("<div align=\"right\">#=kendo.format('{0:C}', sum)#</div>");
columns.Command(command => command.Destroy()).Width("20%");
})
.Editable(editable => editable.Mode(GridEditMode.InCell))
.ToolBar(toolbar =>
{
toolbar.Save().SaveText("Crear pedido").CancelText("Cancelar");
})
.Selectable(s => s.Mode(GridSelectionMode.Single).Type(GridSelectionType.Cell))
.Scrollable()
.Resizable(resize => resize.Columns(true))
.HtmlAttributes(new { style = "height:550px", #class = "ra-section" })
.DataSource(dataSource => dataSource
.Ajax()
.Aggregates(aggregates =>
{
aggregates.Add(p => p.Total).Sum();
})
.Batch(true)
.ServerOperation(false)
.Events(events => events.Error("GridArticulos_Error").RequestEnd("GridArticulos_RequestEnd"))
.Model(model =>
{
model.Id(a => a.ArticuloId);
model.Field(a => a.Nombre).Editable(false);
model.Field(a => a.Cantidad).Editable(false);
model.Field(a => a.Precio).Editable(false);
model.Field(a => a.Total).Editable(false);
})
//.Create(create => create.Action("GuardarPedido", "Pedidos").Data("ObtenerDatos"))
.Update(update => update.Action("GuardarPedido", "Pedidos").Data("ObtenerDatos"))
.Destroy(d => d.Action("EliminarArticulo", "Pedidos"))
)
)
you have your .Create() method commented out. uncomment it and point it to the appropriate controller method and you should be fine
uncomment this line--> //.Create(create => create.Action("GuardarPedido", "Pedidos").Data("ObtenerDatos"))
.Update(update => update.Action("GuardarPedido", "Pedidos").Data("ObtenerDatos"))
.Destroy(d => d.Action("EliminarArticulo", "Pedidos"))
also, your create method is pointed to the same method as your update method on the line below it so you would need to edit the method to point to another method. I can't help there since I don't know what methods you have in your controller
I have one grid like this :
#(Html.Kendo().Grid<ProductViewModel>()
.Name("Grid")
.Columns(columns =>
{
**columns.Bound(c => c.Logo).ClientTemplate();**
columns.Bound(p => p.Title);
columns.Bound(p => p.Category);
columns.Bound(p => p.SupplierName);
columns.Bound(p => p.SupplierContactName);
columns.Bound(p => p.IsDeleted);
columns.Bound(p => p.TimeStamp).Format("{0:yyyy/MM/dd HH:mm}").EditorTemplateName("DateTime"); ;
//columns.Command(command => { command.Edit(); command.Destroy(); }).Width(220).Title("Command");
//columns.Command(command => { command.Edit(); command.Destroy(); }).Width(250);
})
.Pageable()
.Scrollable()
.DataSource(dataSource => dataSource
.Ajax()
.PageSize(20)
.Events(events => events.Error("error_handler"))
.Model(model => model.Id(p => p.ID))
.Read(read => read.Action("EditingCustomValidation_Read", "Product"))
)
And I have one action like this for Showing image :
public FileContentResult Photo(int id)
{
return new FileContentResult(db.Products.Find(id).Logo, "image");
}
What should I write in my ClientTemplate for calling this action sending productid an showing products logo?
Please you can try like this...
columns.Bound(c => c.Logo).ClientTemplate("<img src='" + Url.Content("~/ImagePath/") + "#=ImageUrl#' alt='#=Name #' Title='#=Name #' height='62' width='62'/>");
One of my colleague ran into same issue and we posted question on telerik forum Bind image path to a controller function in Kendo Grid and they responded as below.
I think instead of your controller returning a string with the URL of the image it might be easier for it to just return the image itself. Some thing like this post on stackoverlow
That way you can just have your template point the src property of the img html element to this controller action that would presumably take as parameter something to pinpoint the corresponding image file to each row element.
I have a kendo grid with datasource. Each time it is refreshing the old data with new data. But my requirement is quite different I also want display old data as well.
Another data source is in anotherKENDO GRID.
My Kendo Grid as below:
#(Html.Kendo().Grid<ABC>()
.Name("grdABC")
.HtmlAttributes(new { style = "height:450px" })
.Columns(columns =>
{
columns.Bound(d => d.ID).Hidden(true);
columns.Bound(d => d.Group).Hidden(true);
columns.Bound(d => d.Name).Title("Name").Width(350);
})
.Selectable(selectable => selectable
.Mode(GridSelectionMode.Multiple))
.Scrollable()
.Filterable()
.DataSource(dataSource => dataSource
.Ajax()
.Sort(sort => sort.Add("Name").Ascending())
.Model(model => model.Id(p => p.ID))
.Read(read => read.Action("GetSelectedNames", "EligCriteria", new { Id = Model.ID != null ? Model.ID : (decimal?)null }).Data("getSelectedEligibilityNames"))
)
.AutoBind(false)
)
We can maintain using below code:
var grid = $("#GridID").data("kendoGrid");
grid.one("dataBinding", function(e){
e.preventDefault()
});
I have a Kendo Grid which has a popup editable template,
If possible i would like to pass the model (the model of the row, or at least its Id) to the editable template
Grid
#(Html.Kendo().Grid<Client>()
.Name("grid")
.Columns(columns =>
{
columns.Bound(c => c.Name).Width(140);
columns.Bound(c => c.Status);
columns.Bound(c => c.ProcesingStyle);
columns.Bound(c => c.ArchiveDays);
columns.Command(command =>
{
command.Edit().Text(" ");
command.Destroy().Text(" "); ;
}).Width(90);
})
.ToolBar(toolbar => toolbar.Create().Text("New"))
.Editable(editable => editable
.Mode(GridEditMode.PopUp)
.TemplateName("Client").AdditionalViewData(new { Client = Model })
.Window(w => w.Title("Site")))
.HtmlAttributes(new { style = "height: 380px;" })
.Scrollable()
.Sortable()
.Selectable()
.Resizable(resize => resize.Columns(true))
.Reorderable(reorder => reorder.Columns(true))
.Events(events => events.Change("onChange"))
.Pageable(pageable => pageable
.Refresh(true)
.PageSizes(true)
.ButtonCount(5))
.DataSource(dataSource => dataSource
.Ajax()
.Read(read => read.Action("Get", "Clients"))
.Model(model => model.Id(p => p.Id))
.Create(update => update.Action("Create", "Clients"))
.Update(update => update.Action("Update", "Clients"))
.Destroy(update => update.Action("Destroy", "Clients"))
)
)
Template
#model Client
#(Html.Kendo().ComboBoxFor(m => m.Plan)
.DataTextField("Name")
.DataValueField("Id")
.Placeholder("Select Plan...")
.HtmlAttributes(new { style = "width:300px" })
.Filter(FilterType.Contains)
.MinLength(3)
.DataSource(source =>
source.Read(read =>
read.Action("GetPlans", "Plans",new {ClientId = Model.Id}))))
Everything works fine except i need to use the Id of the row/model inside the template, in particular , i need the to pass the model.Id (which is the id of the model of the row), to the action on the Combobox in the template, so i can filter the data correctly
This is the offending line in the grid,
.TemplateName("Client").AdditionalViewData(new { Client = Model })
The result is the Model inside the template is always null, im not sure how to pass the data i need to the template
Is there anyway i can do this, or should i be looking at a different approach?
The way I got around this was to put a JavaScript function in the original view as seen below:
function getClientId() {
var row = $(event.srcElement).closest("tr");
var grid = $(event.srcElement).closest("[data-role=grid]").data("kendoGrid");
var dataItem = grid.dataItem(row);
if (dataItem)
return { clientId: dataItem.Id }
else
return { clientId: null }
}
And reference it from my editor template:
.DataSource(source => source.Read(read => read.Action("GetPlans", "Plans").Data("getClientId"))))
Note : I'm pretty sure you cant run JavaScript from a EditorTemplate so it needed to be housed in the original view.
I know this is a really old question, but for those who are wondering why this doesn't work:
.TemplateName("Client").AdditionalViewData(new { Client = Model })
This code doesn't work because the only data you can pass through this method is static data. You can pass specific strings or numbers, like "Hello World", and that would work fine. For dynamic data with kendo, I've learned that it really depends on the situation, and your solution here works well.
Ive seen lots of traffic regarding this issue so I thought I would add my own spin. Everything works as expected on my dev machine. But when I deploy to Azure, the read action on the grid is no longer posted. instead its posting to the url of the page.
Here is my grid
#(Html.Kendo().Grid<HondaPERebates.Model.Models.ClaimModel>()
.Name("grid")
.Groupable()
.Columns(columns =>
{
columns.Bound(p => p.RebateProgramId).Hidden();
columns.Bound(p => p.Status).ClientTemplate("<span class='#=GetClass(Status)#'>#=Status#</span>").Width(100);
columns.Bound(p => p.SellingDealerNo).Width(100);
columns.Bound(p => p.SerialNumberSuffix).ClientTemplate("<span>#=SerialNumberPrefix#-#=SerialNumberSuffix#</span>").Width(150).Title("Serial Number").Filterable(false);
columns.Bound(p => p.SubmittedDate).Format("{0:MM/dd/yyyy}").Width(75);
})
.Pageable()
.Sortable()
.Resizable(resize => resize.Columns(true))
.Selectable()
.Filterable()
.Scrollable(scrollable => scrollable.Virtual(true).Height(630))
.DataSource(dataSource => dataSource
.Ajax()
.Model(model =>
{
model.Id(p => p.RebateProgramId);
})
.PageSize(65)
.Read(read => read.Action("_GetClaims", "Rebates").Data("grid_Parameters"))
)
.Events(events => events.DataBound("onDataBound")
)
)
and the controller
public ActionResult _GetClaims(int rebateProgramId, [DataSourceRequest] DataSourceRequest request)
{
var email = this.HttpContext.User.Identity.Name;
var list = _manager.GetClaimsByEmail(rebateProgramId, email);
return Json(list.ToDataSourceResult(request), JsonRequestBehavior.AllowGet);
}
and the js that gets invoked to get extra parms
function grid_Parameters() {
var RebateProgramId = $('#RebateProgramId').val();
return { RebateProgramId: RebateProgramId };
}
I my case the Action method was not getting called (example : HierarchyBinding_Employees).
I changed the Name to GetHierarchyDataForEmployees and it worked for me :).