writing a script/Function for checkboxes and Custom Delete button in Telerik UI for ASP.NET MVC - json

I need help writing a script/Function for my Telerik UI for asp.net MVC program. I have a delete button in my tool bar and I think my script is correct for it deleting. Now, I'm told that my check boxes haft to have a script also to be deleted when checked. As a C# coder, I'm not entirely knowledgeable about Json coding. So, any help would be appreciated! Here is my code below.
#(Html.Kendo().Grid<MVCSQLDatabase.Models>()
.Name("Grid")
.DataSource(dataSource => dataSource
.Ajax()
.PageSize(10)
.Batch(true)
.Model(model => model.Id(p => p.Proposal_Uid))
.Read(read => read.Action("Proposals_Read", "Grid"))
.Create(create => create.Action("Proposals_Create", "Grid"))
.Update(update => update.Action("Proposals_Update", "Grid"))
.Destroy(destroy => destroy.Action("Proposals_Destroy", "Grid"))
)
.Resizable(resize => resize.Columns(true))
.Columns(columns =>
{
columns.Select().Width(100); //<-- my check boxes code.
columns.Bound(c => c.Prime).Width(215);
columns.Bound(c => c.Proposal).Width(200);
columns.Bound(c => c.C).Width(190);
columns.Bound(c => c.Cl).Width(185);
columns.Bound(c => c.T).Width(290);
columns.Bound(c => c.M).Width(220);
columns.Bound(c => c.S).Format("{0: dd/MM/yyyy}").Width(170);
columns.Bound(c => c.E).Format("{0: dd/MM/yyyy}").Width(170);
columns.Bound(c => c.P).Width(235);
columns.Bound(c => c.Con).Width(215);
columns.Command(command => { command.Destroy(); }).Width(180);// <--- My delete button in my column
})
.ToolBar(toolbar =>
{
toolbar.Create();
toolbar.Save();
toolbar.Excel();
toolbar.Custom().Text("Delete").Name("batchDestroy").IconClass("k-icon k-i-close"); //<-- my custom made delete button in my toolbar.
})
.ColumnMenu()
.Editable(editable => editable.Mode(GridEditMode.InCell))
.Pageable()
.Selectable(selectable =>
{
selectable.Mode(GridSelectionMode.Multiple);
selectable.Type(GridSelectionType.Row);
})
.PersistSelection()
.Filterable(filterable => filterable.Mode(GridFilterMode.Row))
.Scrollable()
.HtmlAttributes(new { style = "height:835px;" })
)
<script>
$("#grid").on("click", "batchDestroy", function() {
var $tr = $(this).closest("tr"),
grid = $("#grid").data("kendoGrid"),
dataItem = grid.dataItem($tr);
grid.dataSource.remove(dataItem);
});
</script>

Here is the code for anybody else stuck in this situation. The thing that got me the most, was the .done function to end the button click function and then saving the grid AFTER the delete was made. Hopefully, this helps others!
<script>
$(document).ready(function ()
{
$(".k-grid-Destroy").on("click", function (e)
{
e.preventDefault();
var grid = $("#Grid").data("kendoGrid");
var selectedRows = grid.select();
kendo.confirm(kendo.format("Are you sure you wish to delete {0} records?", selectedRows.length))
.done(function ()
{
$.each(selectedRows, function (i, row)
{
grid.removeRow(row);
})
grid.saveChanges();
});
});
});
</script>

Related

Show 2 child grids at the same level in a single parent grid in ASP.NET Kendo MVC

Can anyone please provide me the similar solution using kendo MVC as in the below link?
Creating 2 child kendo grids at the same level
Thanks!
Use the client template features. So on your grid:
#(Html.Kendo().Grid<Kendo.Mvc.Examples.Models.EmployeeViewModel>()
.Name("grid")
.Columns(columns =>
{
columns.Bound(e => e.FirstName).Width(130);
columns.Bound(e => e.LastName).Width(130);
columns.Bound(e => e.Country).Width(130);
columns.Bound(e => e.City).Width(110);
columns.Bound(e => e.Title);
})
.Sortable()
.Pageable()
.Scrollable()
>> refer to the template
.ClientDetailTemplateId("template")
... etc
Then make the template with the 2 grids:
<script id="template" type="text/kendo-tmpl">
#(Html.Kendo().Grid<Kendo.Mvc.Examples.Models.OrderViewModel>()
.Name("grid_#=EmployeeID#") // template expression, to be evaluated in the master context
.Columns(columns =>
{
columns.Bound(o => o.OrderID).Width(110);
columns.Bound(o => o.ShipCountry).Width(150);
columns.Bound(o => o.ShipAddress).ClientTemplate("\\#= ShipAddress \\#"); // escaped template expression, to be evaluated in the child/detail context
columns.Bound(o => o.ShipName).Width(300);
})
.DataSource(dataSource => dataSource
.Ajax()
.PageSize(10)
.Read(read => read.Action("HierarchyBinding_Orders", "Grid", new { employeeID = "#=EmployeeID#" }))
)
.Pageable()
.Sortable()
.ToClientTemplate()
)
#(Html.Kendo().Grid<Kendo.Mvc.Examples.Models.OrderViewModel>()
.Name("grid2_#=EmployeeID#") // template expression, to be evaluated in the master context
.Columns(columns =>
{
columns.Bound(o => o.OrderID).Width(110);
columns.Bound(o => o.ShipCountry).Width(150);
columns.Bound(o => o.ShipAddress).ClientTemplate("\\#= ShipAddress \\#"); // escaped template expression, to be evaluated in the child/detail context
columns.Bound(o => o.ShipName).Width(300);
})
.DataSource(dataSource => dataSource
.Ajax()
.PageSize(10)
.Read(read => read.Action("HierarchyBinding_Orders", "Grid", new { employeeID = "#=EmployeeID#" }))
)
.Pageable()
.Sortable()
.ToClientTemplate()
)
</script>
The .ToClientTemplate() is important. Also need to have different grid names for each row. You can mix in other HTML for organization if desired. See here.

Newly added row gets disappear in kendo grid

I have a kendo grid and a AddRow button.On AddRow Button click I am adding a new row to kendo grid.When I click on AddRow button, new row gets added sucessfully but clicking anywhere else in the page except first column of newly added row,row disappears.Here is my code on button click:
$('#AddRow').click(function () {
grid = $('#grdclaimantTypeTips').data("kendoGrid");
grid.addRow();
row = grid.tbody.find(".k-grid-edit-row");
grid.select(row);
}
Here is my code for kendo grid:
#(Html.Kendo().Grid<ClaimPro.Data.ClientAttributeDO>()
.Name("grdclaimantTypeTips")
.Columns(columns =>
{
columns.Bound(Type => Type.claim_type_name).Title(Resource.ClaimType).Width(80).EditorTemplateName("ClientAttributesDDL").EditorViewData(new { columnName = "claim_type_cd" });
columns.Bound(Type => Type.claimant_name).Title(Resource.Claimant).Width(120).ClientTemplate("#=claimant_name_changed#");
columns.Bound(Type => Type.tip).Title(Resource.Tip).Width(200).HtmlAttributes(new { #class = "Wrap" });
columns.Bound(Type => Type.tip).Hidden();
columns.Bound(Type => Type.tip).Hidden();
columns.Bound(Type => Type.id).Hidden();
})
.Scrollable()
.Sortable()
.Filterable()
.Selectable(selectable =>
{
selectable.Mode(GridSelectionMode.Single);
selectable.Type(GridSelectionType.Row);
}
)
.Pageable(pageable => pageable.Input(true).PageSizes((int[])ViewData["DefaultDropdownSize"]))
.Resizable(resize => resize.Columns(true))
.Editable(editable => editable.Mode(GridEditMode.InCell))
.Events(e => e.Edit("onEditGrid"))
.DataSource(dataSource => dataSource
.Ajax()
.ServerOperation(false)
.Batch(true)
.Model(model => model.Id(c => c.id))
.Model(model => model.Field(p => p.claimant_name).Editable(false))
.Read(read => read.Action("GetClaimantTypeDetails", "ClientAttribute"))
)
)

Column with menu in each cell with menu items

I want to create a menu item in each cell using Kendo Grid MVC I have managed to get the grid to display with the menu item in each cell but when I click on the menu I don't see the menu items.
Need the menu items to show in their own column this is why I have not bound it to a specific property.
Any ideas on what I'm doing wrong?
#(Html.Kendo().Grid(Model)
.Name("gridDropDownMenu")
.Columns(columns =>
{
columns.Template(#<text></text>).Title("").Width(120).HtmlAttributes(new { #class = "templateCell" }).ClientTemplate(
Html.Kendo().Menu()
.Name("menu_#=ProductID#")
.Items(its =>
{
its.Add().Text("Actions").Items(nested =>
{
nested.Add().Text("Test 1");
nested.Add().Text("Test 2");
});
})
.ToClientTemplate().ToHtmlString()
);
columns.Bound(p => p.ProductName);
})
.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(true);
model.Field(p => p.CategoryID).DefaultValue(1);
})
.Read(read => read.Action("ForeignKeyColumn_Read", "Home"))
.Update(update => update.Action("ForeignKeyColumn_Update", "Home"))
.Create(create => create.Action("ForeignKeyColumn_Create", "Home"))
.Destroy(destroy => destroy.Action("ForeignKeyColumn_Destroy", "Home"))
)
)
Resolved by adding the following: .Events(ev => ev.DataBound("initMenus"))
function initMenus(e) {
$(".templateCell").each(function(){
eval($(this).children("script").last().html());
});
}

How to specify partial view of Edit Popup

I have a Kendo Grid with Popup editing. However, I need to have my own PartialView displayed for editing.
Is there any way to tell the grid what PartialView to display for editing?
If that can't be done, is there a way to wire up the grid so it calls a JavaScript function which would then pop up a custom window that I will populate with a partial view? It would have to be able to pass the ID of Row that was selected so that the view would edit the correct row. I know how to create the Window, I just don't know how to call it from the grid and get the Id of the row.
This is the grid:
#(Html.Kendo().Grid<OrderSummaryLineItem>()
.Name("grid")
.Columns(columns =>
{
columns.Bound(c => c.ReportType).Width(75);
columns.Bound(c => c.ReferenceId).Width(75);
columns.Bound(c => c.BorrowerName).Width(75);
columns.Bound(c => c.PropertyAddress).Width(100);
columns.Bound(c => c.EstimatedCompletionDate).Width(100);
columns.Bound(c => c.ReportPrice).Width(75);
columns.Bound(c => c.ExpediteFee).Width(75);
columns.Bound(c => c.Discount).Width(75);
columns.Bound(c => c.TotalPrice).Width(75);
columns.Command(command => { command.Edit(); command.Destroy(); }).Width(160);
})
.HtmlAttributes(new { style = "height: 600px;" })
.Editable(editable => editable.Mode(GridEditMode.PopUp))
.Scrollable()
.Groupable()
.Sortable()
.Pageable(pageable => pageable
.Refresh(true)
.PageSizes(true)
.ButtonCount(5))
.DataSource(dataSource => dataSource
.Ajax()
.Model(model=> model.Id(c=> c.ReferenceId))
.Read(read => read.Action("Orders_Read", "OrderSummary"))
.Destroy(update => update.Action("EditingPopup_Destroy", "OrderSummary"))
)
)
Update:
I have this partially resolved: I added a custom Command like this:
columns.Command(command => { command.Custom("ViewDetails").Click("showDetails");
This wires up the "showDetails" JavaScript function. But I am still not able to pass the current row Id.
This is what I came up with:
columns.Template(#<text></text>).ClientTemplate("<a href='"+Url.Action("EditOrderLineItem","OrderSummary")+"/#=Id#'>Edit</a>");

How to Refresh single column in Kendo UI Grid asp.net mvc?

Here is my code for loading data into Kendo Grid:
#(Html.Kendo().Grid(Model)
.Name("Grid")
.Columns(columns =>
{
columns.Bound(p => p.CountryName);
columns.Bound(p => p.CountryCode);
columns.Bound(p => p.CountryCodeLength);
columns.Bound(p => p.CurrencyString);
columns.Bound(p => p.CountryISOName);
})
.Pageable(p =>
{
p.Enabled(true);
p.PageSizes(new int[] { 10, 20, 30, 40, 50 });
})
.Sortable()
.Selectable()
.Groupable()
.Scrollable()
.Filterable()
.DataSource(dataSource => dataSource
.Ajax()
.Model(model => model.Id(p => p.ID))
.Read(read => read.Action("Country_Read", "Country")).PageSize(10)
)
.Events(events => events.Change("GridChange"))
.ToolBar(toolbar =>
{
toolbar.Template(
#<text>
<div id="Toolbar">
<a id="Add" href="#Url.Action("Create", "Country")" title="Add New Record"></a>
<a id="Edit" title="Edit Selected Record"></a>
<a id="Delete" title="Delete Selected Record"></a>
</div>
</text>);
})
)
How do I refresh the column CountryCodeLength on 10-15 seconds regularly? I tried refreshing the complete Grid, but I have some popup to be open inside the same grid. So while refreshing complete grid on working with popup, the popup will be lost. Please tell me how I can refresh a single column?
Here is the code that I used to refresh the whole grid:
$(document).ready(function()
{
var refreshId = setInterval( function() {
//GET YOUR GRID REFERENCE
var grid = $("#Grid").data("kendoGrid");
grid.dataSource.read();
}, 5000);
});
var gridData = $("#Grid").data("kendoGrid").dataSource;
var data = gridData.at(0);//data at 0 Location
data.set("CountryCodeLength", 20);//set the value here
you can use a polling script to refresh the value
var MyGrid = $("#MyGrid").data("kendoGrid");
var selectedItem = MyGrid.dataItem(MyGrid.select());
selectedItem.set("CountryCodeLength", 10);
MyGrid.tbody.find("tr[data-uid=\"" + selectedItem.uid + "\"]").addClass("k-state-selected");