0

Script:-

<script type="text/javascript">
        $(document).ready(function () {
            var x = document.getElementById("currentPage").value;
            if (x == null || x == "")
                GetPage(parseInt(1));
            else
                GetPage(parseInt(x));

            $('#pager').on('click', 'input', function () {
                GetPage(parseInt(this.id));
                document.getElementById("currentPage").value = parseInt(this.id);
            });
        });


        function GetPage(pageIndex) {
            //debugger;
            $.ajax({
                cache: false,
                //url: "/EmailScheduler/",
                url: '@(Url.RouteUrl("EmailScheduler"))',
                type: "POST",
                data: { "selectValue": pageIndex },
                traditional: true,
                dataType: "json",
                success: function (data) {
                    //debugger;
                    $('#GridRows').empty();
                    $('#pager').empty();
                    var trHTML = '';
                    var htmlPager = '';

                    $.each(data.Customers, function (i, item) {

                        trHTML += ' <tbody class="table-hover"><tr>'
                        + '<td class="text-left"><div class="checkbox" style="padding-left:50px;"><label><input type="checkbox" id=" ' + item.CustomerID + '" class="checkBoxClass"/></label></div></td>'
                        + '<td class="text-left" id="tblFirstName"> ' + item.FirstName + '</td>'
                        + '<td class="text-left" id="tblLastName"> ' + item.LastName + '</td>'
                        + '<td class="text-left" id="tblEmailID"> ' + item.EmailID + '</td>'
                        + '<td class="text-left" id="tblCustomerType"> ' + item.CustomerType + '</td>'
                        + '<td class="text-left" id="tblCustomerDesignation" style="padding-left:40px;padding-right:30px;"> ' + item.CustomerDesignation + '</td></tr></tbody>'
                    });

                    $('#GridRows').append(trHTML);

                    if (data.Pager.EndPage > 1) {
                        htmlPager += '<ul class="pagination">'
                        if (data.Pager.CurrentPage > 1) {
                            htmlPager += '<li><input class="myButton" style="width:25px;height:25px;" type="button" id="1" style="font-weight:bold;" value="<<" /></li><li><input type="button"  class="myButton" id="' + (data.Pager.CurrentPage - 1) + '"value="<"></li>'
                        }

                        for (var page = data.Pager.StartPage; page <= data.Pager.EndPage; page++) {
                            htmlPager += '<li class="' + (page == data.Pager.CurrentPage ? "active" : "") + '"><input type="button"  class="myButton" id="' + page + '" value="' + page + '"/></li>'
                        }

                        if (data.Pager.CurrentPage < data.Pager.TotalPages) {
                            htmlPager += '<li><input type="button"  class="myButton" id="' + (data.Pager.CurrentPage + 1) + '" value=">"/></li><li><input type="button" class="myButton" id="' + (data.Pager.TotalPages) + '"  value=">>"/></li>'
                        }

                        htmlPager += '</ul>'
                    }
                    $('#pager').append(htmlPager);

                },
                error: function (jqXHR, textStatus, errorThrown) {
                    debugger;
                }

            });
        }

        $(document).on('click', '#GridRows .checkBoxClass',
            function () {
                var cid = $(this).attr('id');
                debugger;
                var CustomerIDArray = [];
                var hidCID = document.getElementById("hfCustomerID");
                if (hidCID != null && hidCID != 'undefined') {
                    var CustID = hidCID.value;
                    CustomerIDArray = CustID.split("|");
                    var currentCheckboxValue = cid;
                    var index = CustomerIDArray.indexOf(currentCheckboxValue);
                    //debugger;
                    if (index >= 0) {
                        var a = CustomerIDArray.splice(index, 1);
                        //alert("a" + a);
                    }
                    else {
                        CustomerIDArray.push(currentCheckboxValue);
                        //alert('pushed value:' + CustomerIDArray);
                    }
                    hidCID.value = CustomerIDArray.join("|");
                    //alert('Final' + hidCID.value);
                }
                else {
                    alert('undefined');
                }

            });

        $(document).on('click', '#cbSelectAll', function () {
            if (this.checked) {
                //$(':checkbox').each(function () {
                $("#GridRows .checkBoxClass").each(function () {
                    //debugger;
                    this.checked = true;
                    var selectall = document.getElementsByClassName(".checkBoxClass");
                    var custid = $(this).attr('id');
                    console.log('custid' + custid);
                    var hidSelectAll = document.getElementById("hfSelectAll");
                    var hidCustomer = document.getElementById("hfCustomerID");
                    hidCustomer = '';
                    var str = 'Select All';
                    hidSelectAll.value = str;
                    console.log(hidSelectAll);
                });
                $("#GridRows .checkBoxClass").change(function () {
                    if (!$(this).prop("checked")) {
                        $("#cbSelectAll").prop("checked", false);
                        var cid = $(this).attr('id');
                        console.log('cid' + cid);
                        var hidSelectAll = document.getElementById("hfSelectAll");
                        var str = 'Select All + unselected values';
                        hidSelectAll.value = str;
                        console.log(hidSelectAll);
                    }
                });

            }
            else {
                $(':checkbox').each(function () {
                    this.checked = false;
                    var hidSelectAll = document.getElementById("hfSelectAll");
                    var str = 'UnSelect All';
                    hidSelectAll.value = str;
                    console.log(hidSelectAll);
                });
                $(".checkBoxClass").change(function () {
                    if (!$(this).prop("checked")) {
                        $("#cbSelectAll").prop("checked", false);
                        var hidSelectAll = document.getElementById("hfSelectAll");
                        var str = 'unSelect All + selected values';
                        hidSelectAll.value = str;
                        console.log(hidSelectAll);
                    }
                });
            }
        });

    </script>

HTML:-

  <div class="table-responsive" style="padding-left:20%;">
            <table id="tCustomer" class="table-fill" style="float:left;">
                <thead>
                    <tr>
                        <th class="text-left">                            
                             Select All
                            <div class="checkbox">
                                <input style="margin-left:15px;" type="checkbox" id="cbSelectAll" class="checkBoxClass"/>
                            </div>
                        </th>
                        <th class="text-left" style="padding-left:27px;">
                            First Name
                        </th>
                        <th class="text-left" style="padding-left:32px;">
                            Last Name
                        </th>
                        <th class="text-left" style="padding-left:40px;padding-right: 60px;">
                            Email-ID
                        </th>
                        <th class="text-left" style="padding-left:30px;padding-right: 40px;">
                            Customer Type
                        </th>
                        <th class="text-left" style="padding-left:15px;">
                            Customer Designation
                        </th>
                    </tr>
                </thead>
            </table>
            <div id="GridRows" style="width:65%;">
            </div>
        </div>

        <div id="pager"></div>
        <input type="hidden" id="currentPage">
        <input type="hidden" id="hfCustomerID"/>
        <input type="hidden" id="hfSelectAll" />

In this When click on Select all checkbox then all values from should be checked but when select all checked box is checked then only value from current pagination got selected. table contain is generated from ajax to avoid loss of checked value while page load.

2 Answers 2

1

In your case

$(".checkBoxClass").prop('checked', true);

should work too.

The .prop() method gets the property value for only the first element in the matched set. It returns undefined for the value of a property that has not been set, or if the matched set has no elements.

Read More: Here

Sign up to request clarification or add additional context in comments.

Comments

0

In jquery you can achieve this by:

$('input[type="checkbox"]').prop('checked', true);

It will make all checkbox checked.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.