I have page with DropDownList and Telerik's Kendo UI Grid Control. When first time page is opened DropDownList has no item selected in it. When user selects value in DropDownList only then Grid should make Ajax call to the server and load corresponding data.
My code works fine when user selects item in DropDownList, but the problem is that first time when page is opened I there is no value in DropDownList and Grid should not make a call to the server.
My question is how can I prevent grid not to make a call to the server if there is no item selected in DropDowList?
<div>
#Html.Kendo().DropDownList().Name("broker").DataTextField("GrandParentName").DataValueField("Id").BindTo(Model).SelectedIndex(#selectedIndex).Events(e => e.Change("brokerChanged"))
</div>
#(Html.Kendo().Grid<OrderViewModel>()
.Name("Orders")
.HtmlAttributes(new {style = "height: 500"})
.Columns(c =>
{
c.Bound(p => p.Id)
.Width(50)
.Title("")
.Sortable(false)
.IncludeInMenu(false)
.ClientTemplate((#Html.ActionLink("Edit", "Index", "Splits", new {Id = "OrderId"}, null).ToHtmlString().Replace("OrderId", "#=Id#")));
c.Bound(p => p.TradeDate)
.Title("Trd Dt")
.Format("{0:d}")
.Width(90)
.HtmlAttributes(new {style = "text-align: right"});
c.Bound(p => p.Price)
.Title("Price")
.Format("{0:n}")
.Width(100)
.HtmlAttributes(new {style = "text-align: right"});
c.Bound(p => p.Status)
.Title("Status");
c.Bound(p => p.Notional)
.Title("Notional")
.Format("{0:n}")
.HtmlAttributes(new {style = "text-align: right"});
})
.Sortable()
.Scrollable()
.ColumnMenu()
.Pageable(x =>
{
x.Enabled(true);
x.PreviousNext(false);
x.PageSizes(false);
x.Info(true);
x.Input(false);
x.Numeric(false);
x.Refresh(true);
x.Messages(y => y.Display("{2} Order(s)"));
})
.Resizable(resize => resize.Columns(true))
.Reorderable(reoder => reoder.Columns(true))
.DataSource(ds => ds.Ajax()
.ServerOperation(false)
.Read(read => read.Action("Action", "MyController").Data("selectedBrokerId")))
)
<script type="text/javascript">
function brokerChanged() {
var grid = $("#Orders").data("kendoGrid");
grid.dataSource.read();
}
function selectedBrokerId() {
var obj = { brokerId: $("#broker").data("kendoDropDownList").value() };
return obj;
}
</script>
Thanks a lot for your time and help.
There is an autobind function for the grid. You can use this to determine whether or not to read when the page first loads. This should work (assuming that selectedIndex determines if a dropdown value is selected):
#(Html.Kendo().Grid<OrderViewModel>()
.Name("Orders")
.HtmlAttributes(new {style = "height: 500"})
.AutoBind(selectedIndex > 0)
//rest of your grid declaration
Related
Does anyone know of a way to remove hyperlinks in the file generated from Kendo UI grid export to PDF and Excel functionality?
I have customised the export a fair amount and removed the pager bar etc, using CSS.
However I cannot work out how to stop the column headers from being hyperlinks.
I have tried setting the
pointer-events: none;
cursor: default;
but that didn't help and I am trying to avoid using javascript to remove it where possible.
UPDATE
Please see below an edited version of my grid code.
#(Html.Kendo().Grid<MvcProject.Domain.DTO.Reports.AccidentSummary>()
.Name("resultsGrid")
.Columns(columns =>
{
columns.Group(group => group
.Title("Accident Summary Report : Date run - " + DateTime.Now.ToShortDateString() )
.Columns(header => {
header.Bound(c => c.DocCount)
.HtmlAttributes(new { style = "text-align: center;" })
.Title(" ")
.ClientTemplate("<div><i rel='tooltip' title='Documents Attached' #= DocCount > 0 ? classHasFile : '' #></i></div>")
.Width(35).Filterable(false).Sortable(false).Groupable(false).IncludeInMenu(false);
header.Bound(c => c.RegionName)
.Title("Region")
.Width(100);
header.Bound(c => c.AreaName)
.Title("Area")
.Width(200);
header.Bound(c => c.Date_of_Accident)
.Title("Date")
.Width(120)
.Format("{0:dd/MM/yyyy}");
header.Bound(c => c.Days_Lost)
.Title("Days Lost")
.HtmlAttributes(new { style = "text-align: center;" })
.Width(120);
header.Bound(c => c.TypeOfAccidentName)
.Title("Nature ")
.Width(150);
header.Bound(c => c.Location_of_Accident)
.Title("Location Of Accident")
.Width(150).Hidden(true);
header.Bound(c => c.Comments)
.Title("Comments")
.Width(250).Hidden(true);
})
);
})
.HtmlAttributes(new { style = "height: 900px;" })
.Pageable(p => p
.ButtonCount(5)
.PageSizes(true)
.Refresh(true)
)
.Scrollable(s => s.Height("auto"))
.Sortable()
.Filterable()
.Groupable()
.ColumnMenu()
.Resizable(r => r
.Columns(true)
)
.Excel(excel => excel
.FileName("Accident Summary.xlsx")
.Filterable(true)
.ProxyURL(Url.Action("_GridExportSave", "Reports"))
.AllPages(true)
)
.DataSource(d => d
.Ajax()
.Read(read => read.Action("_AccidentSummaryResults_Read", "Reports").Data("Genesis.Reports.HandS.Search.getPaginationData"))
.ServerOperation(true)
.PageSize(20)
)
.ToolBar(tools =>
{
tools.Pdf();
tools.Excel();
})
//PDF removed for now until it is patched
.Pdf(pdf => pdf
.AllPages()
.FileName("AccidentSummary.pdf")
.ProxyURL(Url.Action("_GridExportSave", "Reports"))
)
.Events(events => events.DataBound("Genesis.Reports.HandS.Search.loadTT"))
)
Try
<style>
.k-pdf-export .k-grid-toolbar,
.k-pdf-export .k-pager-wrap
{
display: none;
}
</style>
or
<style>
.k-grid-toolbar,
.k-grid-pager > .k-link
{
display: none;
}
</style>
Use pdf.avoidLinks(true) to skip the actual hyperlinks.
pdf.avoidLinks indicates whether to produce actual hyperlinks in the exported PDF file.
pdf.avoidLinks default value is false.
.Pdf(pdf => pdf
.AllPages()
.FileName("AccidentSummary.pdf")
.avoidLinks(true)
.ProxyURL(Url.Action("_GridExportSave", "Reports"))
)
Note: Available in versions 2015.3.1020 and later
For reference
With following you can easily hide excel export button from MVC Kendo Grid
$(".k-grid-excel").hide();
I have a kendo Grid for a class, and for that class I've built an editor template to produce a radio button for one of the fields.
This radio button doesn't reflect propertie's value and is always false, although I've checked the value, by printing it on the form, and I'm sure it's true. If I set a default value for that field, the radio button will reflect that value, regardless of the real value of the field.
I should note that I'm using a client template to display a text for that field, and it works fine.
This is the Grid:
#(Html.Kendo().Grid<Class>()
.Name("Grid")
.Columns(columns =>
{
columns.Bound(x => x.BActive).ClientTemplate("#= BActive ? 'Open':'Close' #");
/// removed for brevity
})
.Editable(editable => editable.Mode(GridEditMode.PopUp))
.DataSource(ds => ds.Ajax()
.Model(model => {
model.Id(x => x.SWhCode);
model.Field(x => x.BActive).DefaultValue(true);
})
)
)
And this is the lines that produce the radio button inside the editorTemplate:
<label>
#Html.RadioButtonFor(x => x.BActive, true, new { #class = "radio-inline" }) Open
</label>
<label>
#Html.RadioButtonFor(x => x.BActive, false, new { #class = "radio-inline" }) Close
</label>
bool type will be render in client like this
true => True
false => False
Have you try to inspect your element rendered in HTML? You'll see that the bool value transform into capitalize string. To overcome this you should change true and false in radio button with type string too.
Your view code should be like this
<label>
#Html.RadioButtonFor(x => x.BActive, "true", new { #class = "radio-inline" }) Open
</label>
<label>
#Html.RadioButtonFor(x => x.BActive, "false", new { #class = "radio-inline" }) Close
</label>
My grid is
#(Html.Kendo().Grid<student.Models.SearchViewModel>()
.Name("Grid").HtmlAttributes(new { #class = "studentGrid" })
.Columns(
x =>
{
x.Bound(y => y.Id).Hidden(true);
x.Bound(y => y.Id).ClientTemplate(#"<input type='checkbox' name='checkedRecords' value='#= Id #' class='mainCheckbox' onclick='checkboxClicked(this, ""checkAllMain"")'/>")
.Title("")
.HeaderTemplate(#"<input type='checkbox' name='checkAllMain' onclick='selectAll(this, ""mainCheckbox"");' />")
.HeaderHtmlAttributes(new { style = "text-align:center" })
.Filterable(false)
.Sortable(false)
.HtmlAttributes(new { #class = "checboxClass", style = "text-align:center" });
x.Bound(y => y.abc1).Hidden(false);
x.Bound(y => y.abc2).Hidden(false);
x.Bound(y => y.abc3).Hidden(false);
}
)
.ToolBar(tb =>
{
tb.Custom()
.Text("Export To Excel")
.HtmlAttributes(new { id = "export" })
.Url(Url.Action("Export", Html.CurrentControllerName()));
tb.Custom()
.Text("Expand Selected Rows")
.HtmlAttributes(new { id = "expandSelectedRows" });
})
.Groupable()
.Reorderable(x => x.Columns(true))
.Pageable(x => x.PageSizes(new int[] { 20, 50, 100 }).Input(true).Numeric(true))
.Scrollable(x => x.Enabled(true).Height(Model.Height))
.Resizable(resize => resize.Columns(true))
.Reorderable(reorder => reorder.Columns(true))
.Sortable()
.Selectable()
.Navigatable()
.Filterable()
.ClientDetailTemplateId("subTemplate")
.AutoBind(!Model.NoAutoload)
.Events(ev => { ev.DataBound("DataBoundSearch"); })
.DataSource(dataSource => dataSource
.Ajax().PageSize(100)
.ServerOperation(false) // Paging, sorting, filtering and grouping will be done client-side
.Model(model => model.Id(c => c.Id))
.Events(events => events.Error("error").RequestStart("RequestStart").RequestEnd("RequestEnd").Change("Changed"))
.Read(x => x.Action("GetData", Html.CurrentControllerName()).Data("ABCPostData")))
)
with kendo grid when we select a row that row is highlighted with brown color by default.Am not able to get the default color when row is clicked. On the client side it rendered as
<tr class="k-master-row k-state-selected" data-uid="122bb914-87c2-4f0c-9351-52c1d9b84ae5" style="background-color: rgb(255, 255, 255);">
how it is set to background-color: rgb(255, 255, 255); ? how can i override this to brown like background-color: #f0713a, border-color: #f0713a?
There are a couple of ways to do this. The easiest way is through CSS
Modifying style for selected row
#grid tr.k-state-selected td {
background-color: #f0713a;
border-color: #f0713a
}
Modifying style for selected cell
#grid td.k-state-selected {
background-color: #f0713a;
border-color: #f0713a
}
and in your grid declaration ensure this is set:
selectable: "cell"
Here is a demo for single cell.
Another way is to override kendo styles with their themebuilder. But this is extremely bulky.
If you want to do it programatically, this is possible too by getting the selected element in the change event of the grid, and then setting the element's background in code. I will try to do this if you need this option, but the way I see it, leave UI stuff to css, and leave coding to javascript.
How to get row value from grid when toolbar is clicked in kendo ui mvc grid.
sample View code:
<div id="kendoGrid">
#(Html.Kendo().Grid(Model)
.Name("Grid")
.Columns(columns =>
{
columns.Template(#<input id='chkPayment' type='checkbox' class='check-box' />).Title("Select");
columns.Bound(m => m.PaymentDetailDto.InvoiceNumber);
columns.Bound(m => m.PaymentDetailDto.ModifiedDate).Title("Invoice Date");
})
.ToolBar(x => x.Custom().Text("Make Payment").HtmlAttributes(new { id = "btnMakePayment" }))
)
</div>
Sweeps the grid and search for the value it
$('input[type="checkbox"]').each(function () {
var chkPayment = $(this);
});
I would like to bind data to a kendoui multiselect at runtime.
for example suppose that I want to bind it as a cascade of a drobdownlist.
any idea?
<p>
<label for="categories">Catergories:</label>
#(Html.Kendo().DropDownList()
.Name("categories")
.HtmlAttributes(new { style = "width:300px" })
.OptionLabel("Select category...")
.DataTextField("CategoryName")
.DataValueField("CategoryId")
.DataSource(source =>
{
source.Read(read =>
{
read.Action("GetCascadeCategories", "CoreParam");
});
})
.Events(e =>e.Select("select"))
)
</p>
<p>
<label for="parameters">Parameters:</label>
#(Html.Kendo().MultiSelect()
.Name("parameters")
.HtmlAttributes(new { style = "width:400px" })
.DataTextField("ParamDesc")
.DataValueField("ParamCode")
.Placeholder("Select products...")
.DataSource(source =>
{
source.Read(read =>
{
read.Action("GetCascadeParams", "CoreParam")
.Data("filterParams");
})
.ServerFiltering(true);
})
.AutoBind(false)
)
</p>
<script type="text/javascript">
function filterParams() {
return {
categories: $("#categories").val()
};
}
function select(e) {
var dropdownlist = $("#categories").data("kendoDropDownList");
dropdownlist.select(e.item.index());
var multiselect = $("#parameters").data("kendoMultiSelect");
multiselect.dataSource.read();
};
</script>
You could create a custom MVVM binder which will get the text of the dropdownlist and will set a property of the ViewModel. This property can be bound to the hidden field. Check out the link below for more information.