admin管理员组文章数量:1388805
Problem Statement:
I'm using the grid provided by codeplex(Codeplex grid.mvc) to implement the grid in my application.Everything was working fine with all the operations.Recently I added checkbox in the grid for selecting multiple rows for performing some operation based on multi-selection.Even i'm able to perform various operation on multiple selection.
Problems what i'm facing:
- I'm not able to retain the state of checkbox selected when the user switches from one page to the other.
- Not able to add Select all checkbox in the header section for selecting all the checkbox in the grid.
How to do this?After doing some research i came to know that checkbox state in the grid can be retained in 2 ways:
- We can store on client side in cookies, and remain it after page loads.
- We can store it on server side, when user change checkbox state you can make an ajax call to change the checkbox state. Remain state on server side, when building page layout.
Can anyone please elaborate on this..???or else can anyone suggest me some ways of solving both of my problems???
Razor View Code:
@model IEnumerable<Web.Areas.Smart.Models.OrderModel>
@using GridMvc.Html
@{
ViewBag.Title = "Index";
}
<h2>Details</h2>
<hr />
<div>
@Html.Grid(Model).Columns(columns =>
{
columns.Add()
.Encoded(false)
.Sanitized(false)
.SetWidth(30)
.RenderValueAs(itm => @Html.CheckBox("checked", itm.InputModel.ChkStatus, new { @class = "check-item", ID= itm.InputModel.AssetID}));
columns.Add(itm => itm.OrderNumber).Titled("Order #");
columns.Add(itm => itm.OrderDate).Format("{0:MM/dd/yyyy}").Titled("Order Date");
columns.Add(itm => itm.InvoiceNumber).Titled("Invoice #");
columns.Add(itm => itm.InvoiceDate).Format("{0:MM/dd/yyyy}").Titled("Invoice Date");
columns.Add(itm => itm.ID).Titled("ID");
}).WithPaging(5).Sortable(true).Filterable(true)
<br />
<input type="button" class="btn btn-primary" value="Create" onclick="@("location.href='"
+ Url.Action("Index", "Plan")
+ "'")" />
</div>
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
<script type='text/javascript'>
$(function () {
$('.datepicker').datepicker({
format: "mm/dd/yyyy",
}).on('changeDate', function (e) {
$(this).datepicker('hide');
});
})
$(function () {
$(".check-item").click(function () {
//alert("item clicked, id = " + $(this).attr("ID"));
var assetID = $(this).attr("ID")
var Url = "@Url.Content("~/Plan/GetCount")";
$.ajax({
url: Url,
dataType: 'json',
data: { ID: id},
success: function (data) {
}
});
});
});
</script>
}
Image for your refernce:
Problem Statement:
I'm using the grid provided by codeplex(Codeplex grid.mvc) to implement the grid in my application.Everything was working fine with all the operations.Recently I added checkbox in the grid for selecting multiple rows for performing some operation based on multi-selection.Even i'm able to perform various operation on multiple selection.
Problems what i'm facing:
- I'm not able to retain the state of checkbox selected when the user switches from one page to the other.
- Not able to add Select all checkbox in the header section for selecting all the checkbox in the grid.
How to do this?After doing some research i came to know that checkbox state in the grid can be retained in 2 ways:
- We can store on client side in cookies, and remain it after page loads.
- We can store it on server side, when user change checkbox state you can make an ajax call to change the checkbox state. Remain state on server side, when building page layout.
Can anyone please elaborate on this..???or else can anyone suggest me some ways of solving both of my problems???
Razor View Code:
@model IEnumerable<Web.Areas.Smart.Models.OrderModel>
@using GridMvc.Html
@{
ViewBag.Title = "Index";
}
<h2>Details</h2>
<hr />
<div>
@Html.Grid(Model).Columns(columns =>
{
columns.Add()
.Encoded(false)
.Sanitized(false)
.SetWidth(30)
.RenderValueAs(itm => @Html.CheckBox("checked", itm.InputModel.ChkStatus, new { @class = "check-item", ID= itm.InputModel.AssetID}));
columns.Add(itm => itm.OrderNumber).Titled("Order #");
columns.Add(itm => itm.OrderDate).Format("{0:MM/dd/yyyy}").Titled("Order Date");
columns.Add(itm => itm.InvoiceNumber).Titled("Invoice #");
columns.Add(itm => itm.InvoiceDate).Format("{0:MM/dd/yyyy}").Titled("Invoice Date");
columns.Add(itm => itm.ID).Titled("ID");
}).WithPaging(5).Sortable(true).Filterable(true)
<br />
<input type="button" class="btn btn-primary" value="Create" onclick="@("location.href='"
+ Url.Action("Index", "Plan")
+ "'")" />
</div>
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
<script type='text/javascript'>
$(function () {
$('.datepicker').datepicker({
format: "mm/dd/yyyy",
}).on('changeDate', function (e) {
$(this).datepicker('hide');
});
})
$(function () {
$(".check-item").click(function () {
//alert("item clicked, id = " + $(this).attr("ID"));
var assetID = $(this).attr("ID")
var Url = "@Url.Content("~/Plan/GetCount")";
$.ajax({
url: Url,
dataType: 'json',
data: { ID: id},
success: function (data) {
}
});
});
});
</script>
}
Image for your refernce:
Share Improve this question edited Oct 16, 2014 at 11:59 Vishal asked Oct 16, 2014 at 11:53 VishalVishal 2,0174 gold badges25 silver badges42 bronze badges3 Answers
Reset to default 2Try to use Localstorage
localStorage.setItem("Idofcheckbox",true)
localStorage.getItem("Idofcheckbox")
Note : Localstorage will store value as string.
Why not saving them client side ? Where selectedCheckbox is a global variable.
var selectedCheckbox = [];
$("input[type='checkbox']").click(function () {
if($(this)[0].checked)
{
selectedCheckbox.push($(this).attr("id"));
}
else
{
selectedCheckbox = $.grep(selectedCheckbox , function (item) {
return item != $(this).attr("id");
});
}
});
And after rendering the table :
$("input[type='checkbox']").each(function () {
if($.inArray($(this).attr("id"), selectedCheckbox) > -1)
{
$(this).attr("checked","checked");
}
});
Below Solution works:
Grid MVC: Add a column at the start of the grid
columns.Add()
.Titled("<input class='checkAll' type='checkbox' name='checkAll' value='.checkAll'/>")
.Encoded(false)
.Sanitized(false)
.SetWidth(20)
.RenderValueAs(item => @Html.CheckBox("checked", false, new { @class = "check-item", ID = item.Id}));
JavaScript:
$('.checkAll:checkbox').change(function () {
if ($(this).prop("checked")) {
$('input:checkbox').prop('checked', true);
}
else {
$('input:checkbox').prop('checked', false);
}
});
本文标签: javascriptMaintain Grid MVC checkbox state from page to page in MVC RazorStack Overflow
版权声明:本文标题:javascript - Maintain Grid MVC checkbox state from page to page in MVC Razor - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744585158a2614151.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论