dimanche 25 septembre 2016

Check All Checkbox on SubCheckbox

I have WebGrid on MVC project with two WebGrid column. The first column collapses the sub-data and the other column is for checkbox.

This checkbox will check all checkbox on its subdata. The problem is I cannot select all the data on its sub-checkbox. This is my sample code:

//This colum will generate checkbox for the main data
   wbclCol.Add(new WebGridColumn
{
    ColumnName = "",
    Header = "",
    CanSort = false,
    Format = (objChildItem) =>
    {
        StringBuilder strbHtml = new StringBuilder();
        strbHtml.Append("<input class='obj-parmain'  name='lngID' value='" + objChildItem.lngPARID + "' data-pardata='" + objChildItem.lngID+ "' data-show='True' type='checkbox' ></input>");

        return new MvcHtmlString(strbHtml.ToString());
    }
});

//This column will generate another column for the sub-data:
 wbclCol.Add(new WebGridColumn
{
    ColumnName = "",
    Header = "",
    CanSort = false,
    Format = (objChildItem) =>
    {
        StringBuilder strbHtml = new StringBuilder();
        strbHtml.Append("<input class='obj-parsub'  name='lngID' value='" + objChildItem.lngPARID + "' data-pardata='" + objChildItem.lngID+ "' data-show='True' type='checkbox' ></input>");

        return new MvcHtmlString(strbHtml.ToString());
    }
});

This is my javascript to select all checkbox on class: obj-parsub when my checkbox with class: obj-parmain is check

 function fncParMainCheck() {
        $(document).off('click', '.obj-parmain');
        $(document).on('click', '.obj-parmain', function (e) {
            var blIsCheck = $(this).is(':checked');
            if (blIsCheck) {
                //var objNext = $('.obj-parsub').nextAll();
                //var objNextMain = $(this).siblings().nextAll().find('.obj-parsub');
                //var objNextMain = $(this).closest('.obj-parmain').find('.obj-parsub').prop('checked', this.checked);
                $(this).closest('.obj-parmain').find('.obj-parsub').parent().parent().nextAll().prop('checked', this.checked);
                //$(objNextMain).prop('checked', blIsCheck);
            }
        });
    }




Aucun commentaire:

Enregistrer un commentaire