我正在使用带有ASP.NET MVC 4的Kendo UI网格
@Html.Label("Status: ")<select id="drStaus" style="width:250px">
<option value="Open">Open</option>
<option value="Pending">Pending</option>
<option value="Other">Closed</option>
</select>
<br /><br />
@(Html.Kendo().Grid((IEnumerable<FeedBackDashBord.Models.FeedBack>)Model)
.Name("grid")
.Columns(columns => {
columns.Bound(o => o.id).Visible(false);
columns.Template(o => Html.ActionLink("Edit", "Edit", new { o.id })).ClientTemplate("<a href=\"" + Url.Action("Edit", "DashBoard") + "/#= id#\">Edit</a>").Width(45);
columns.Bound(o => o.CurrentURL).Width("200px").Title("Reported URL");
columns.Bound(o => o.OS).Width("70px");
columns.Bound(o => o.Browser).Width("70px");
columns.Bound(o => o.UserAgent).Width("200px"); ;
columns.Bound(o => o.datetime).Title("Date Time").Width("100px");
columns.Bound(o => o.Description).Title("Description").Width("200px");
columns.Bound(o => o.Email).Width("150px");
columns.Bound(o => o.Status).Width("70px");
columns.Template(o => Html.ActionLink("Details", "Details", new { o.id })).ClientTemplate("<a href=\"" + Url.Action("Details", "DashBoard") + "/#= id#\">Details</a>").Width(65);
})
.Pageable()
.Sortable()
.Scrollable()
.Filterable()
.Groupable()
.HtmlAttributes(new { style = "height:900px;" })
.DataSource(dataSource => dataSource
.Ajax()
.PageSize(20)
.Read(read => read.Action("AjaxGrid", "DashBoard").Data("additionalInfo"))
)
)
<script>
function additionalInfo() {
return {
status: $("#drStaus").val()
}
}
$("#drStaus").kendoDropDownList();
</script>
在上面的代码中,我已成功将Kendo Grid绑定到数据源。现在我需要实现以下场景。
当从“drStaus”下拉列表中选择一个选项值时,我想将所选值传递给kendogrid并根据所选值重新加载网格。
答案 0 :(得分:5)
试试这个解决方案,我使用demo model属性绑定网格。
在视图中
@(Html.Kendo().DropDownList()
.Name("drStatus")
.DataTextField("Text")
.DataValueField("Value")
.Events(e => e.Change("Change"))
.BindTo(new List<SelectListItem>() {
new SelectListItem() {
Text = "Open",
Value = "Open"
},
new SelectListItem() {
Text = "Pending",
Value = "Pending"
},
new SelectListItem() {
Text = "Other",
Value = "Other"
}
})
.Value("Open")
)
@(Html.Kendo().Grid(Model)
.Name("grid")
.Columns(columns =>
{
columns.Bound(x => x.Name);
columns.Bound(x => x.Id);
})
.Pageable()
.Sortable()
.Scrollable()
.Filterable()
.Groupable()
.DataSource(dataSource => dataSource
.Ajax()
.Read(read => read.Action("AjaxGrid", "DashBoard").Data("additionalInfo"))
)
)
在javaScript中
var additionalDataObject;
function additionalInfo() {
var dropdownlist = $("#drStatus").data("kendoDropDownList");
additionalDataObject = {
status: dropdownlist.value()
};
return additionalDataObject;
}
function Change() {
var grid = $("#grid").data("kendoGrid");
var page = grid.dataSource._page;
reloadFilterParameters();
grid.dataSource.page(page);
}
function reloadFilterParameters() {
var dropdownlist = $("#drStatus").data("kendoDropDownList");
additionalDataObject = {
status: dropdownlist.value(),
};
}
答案 1 :(得分:3)
我使用Kendo Grid ToolBar Template实现了上述场景。
http://demos.kendoui.com/web/grid/toolbar-template.html
@(Html.Kendo().Grid((IEnumerable<FeedBackDashBord.Models.FeedBack>)Model)
.Name("grid")
.Columns(columns => {
columns.Bound(o => o.id).Visible(false);
columns.Template(o => Html.ActionLink("Edit", "Edit", new { o.id })).ClientTemplate("<a href=\"" + Url.Action("Edit", "DashBoard") + "/#= id#\">Edit</a>").Width(45);
columns.Bound(o => o.CurrentURL).Width("200px").Title("Reported URL");
columns.Bound(o => o.OS).Width("70px");
columns.Bound(o => o.Browser).Width("70px");
columns.Bound(o => o.UserAgent).Width("200px"); ;
columns.Bound(o => o.datetime).Title("Date Time").Width("100px");
columns.Bound(o => o.Description).Title("Description").Width("200px");
columns.Bound(o => o.Email).Width("150px");
columns.Bound(o => o.Status).Width("70px");
columns.Template(o => Html.ActionLink("Details", "Details", new { o.id })).ClientTemplate("<a href=\"" + Url.Action("Details", "DashBoard") + "/#= id#\">Details</a>").Width(65);
})
.ToolBar(toolbar =>
{
toolbar.Template(@<text>
<div class="toolbar">
<label class="category-label" for="category">Status:</label>
@(Html.Kendo().DropDownList()
.Name("drStaus")
.OptionLabel("All")
.DataTextField("Text")
.DataValueField("Value")
.AutoBind(false)
.Events(e => e.Change("categoriesChange"))
.DataSource(ds =>
{
ds.Read("ToolbarTemplate_Categories", "DashBoard");
})
)
</div>
</text>);
})
.Pageable()
.Sortable()
.Scrollable()
.Filterable()
.Groupable()
.HtmlAttributes(new { style = "height:900px;" })
.DataSource(dataSource => dataSource
.Ajax()
.PageSize(20)
.Read(read => read.Action("AjaxGrid", "DashBoard").Data("additionalInfo"))
)
)
<script>
function additionalInfo() {
return {
status: $("#drStaus").val()
}
}
$("#drStaus").kendoDropDownList();
</script>
<script>
function categoriesChange() {
var value = this.value(),
grid = $("#grid").data("kendoGrid");
if (value) {
grid.dataSource.filter({ field: "Status", operator: "eq", value: value });//parseInt(value)
} else {
grid.dataSource.filter({});
}
}
</script>