more than one checkbox column, need select all option on header, then edit the boolean checked value in toolbar update button

i have this grid, and i need to insert 3 column of checkboxes (ex: Verify,Approval,Signed) which value is stored in database as boolean,
example if the row is verified then the checkbox automatically checked when loaded.. and the point of my question is i need those 3 columns have checkbox in their header (checkALL) then when we hit some button like "update" it will update the value as the checkboxed is true or false into the data base.. These are the codes Ive been trying to create the checkbox header but ejCheckBox is not a function on my mvc proje

@Html.EJS().Grid("UrlAdaptor").AllowSorting().AllowResizing().AllowPaging().AllowReordering().AllowGrouping().AllowFiltering().AllowExcelExport().ShowColumnChooser(true).Columns(col =>

{

  col.HeaderText("Check2").Template("<input type='checkbox' class='check'/>").HeaderTemplate("#headerTemplate1").TextAlign(Syncfusion.EJ2.Grids.TextAlign.Center).Width("150").Add();

    col.HeaderText("Check").Template("<input type='checkbox' class='check'/>").HeaderTemplate("#headerTemplate").Width(150).Add();


    col.Field("ControlNo").HeaderText("Control No").Add();

@*....another set of column *@

}).ActionComplete("onActionComplete").Created("onCreate").Height(300).AllowPaging().PageSettings(page => page.PageCount(4).PageSizes(true)).Toolbar(toolbarItems).FilterSettings(filter => { filter.Type(Syncfusion.EJ2.Grids.FilterType.Excel); }).ToolbarClick("toolbarclick").Render()


Script:

<script type="text/x-jsrender" id="headerTemplate">

    Checked

    <input type="checkbox" id="headerCheck" />

</script>

<script type="text/x-jsrender" id="headerTemplate1">

    Approve

    <input type="checkbox" id="headerCheck2" />

</script>

<script type="text/javascript">

    // Grid creates function.

    var checkbox = document.querySelector('[type="checkbox"]');

    function onCreate(args) {

        // Sets change event to the header checkbox.

        //$("#headerCheck").ejCheckBox({ "change": "headerChecked" });

        console.log(args);

        console.log("OnCreateMethod");

        $("#headerCheck").click(headerChecked());

    }

    // Header checkbox changes function.

    function headerChecked(args) {

        console.log("asaaa");

        console.log(args);

        //console.log($("#headerCheck").);

        //checkAll(args.isChecked);

        console.log($("#headerCheck").is(':checked'));

        checkAll($("#headerCheck").is(':checked'));

    }

    checkbox.addEventListener('change', function (event) {

        alert(event.target.checked);

    });

    $("#headerCheck").change(function () {

        console.log("change fn");

        headerChecked();

    })

    // Grid actionComplete function.

    function onActionComplete(args) {

        if (args.requestType == "paging") // Maintain the checked state while paging

           // checkAll($("#headerCheck").ejCheckBox("model.checked"));

            checkAll($("#headerCheck").is(':checked'));

            console.log(args);

    }

    $("#headerCheck").click(function () {

        if ($("#headerCheck").is(':checked') == true) {

            console.log("click function");

            headerChecked();

        }

    });

    function checkAll(flag) {

        if ($("#headerCheck").is(':checked') == true) {

            console.log("ischeckedbro");

            $(".checkc").prop("checked", flag);

        }

        else {

            console.log("not check bro");

            $(".checkc").prop("checked", flag);

        }


        $(".checkc").prop("checked", flag); // Check or uncheck all content check boxes

    }

these all are the codes ive been trying to use, but as u can see, I cannot use:  ejCheckBox({ "change": "headerChecked" }); ejCheckBox is not a function I also tried using click(function(){ but not working
thanks, kindly help


9 Replies 1 reply marked as answer

BR Bernadus Rangga Kresna Waskita July 4, 2022 07:43 AM UTC



BR Bernadus Rangga Kresna Waskita July 4, 2022 07:45 AM UTC

the class is .check not .checkc forgot to edit it in this question, so its not a class error, i just misstype it here ^_^



RS Rajapandiyan Settu Syncfusion Team July 5, 2022 01:14 PM UTC

Hi Bernadus,


Currently, we are validating your requirement with the provided code example at our end. We will provide further details on or before July 7th, 2022.


We appreciate your patience until then.


Regards,

Rajapandiyan S



BR Bernadus Rangga Kresna Waskita July 6, 2022 09:50 AM UTC

I got other solution for my problem, i decided to use the all checkbox option <the first column>,  then provide boolean action dropdown then click whether to edit it to true or false (green red btn)
and my client is ok with this.. maybe i still want to know how to provide checkbox on the header boolean value to select all.. and before the check box action even executed, the grid already loaded wheter the value is true or false by the check box.. thanks :)



RS Rajapandiyan Settu Syncfusion Team July 6, 2022 12:09 PM UTC

Hi Bernadus,


Based on your requirement, you want to render the CheckBox control in the Column header. You can achieve this by using the following code in the headerCellInfo event of Grid.


headerCellInfo: https://ej2.syncfusion.com/javascript/documentation/api/grid/#headercellinfo

queryCellInfo: https://ej2.syncfusion.com/javascript/documentation/api/grid/#querycellinfo


Checkbox: https://ej2.syncfusion.com/javascript/documentation/check-box/es5-getting-started/#change-the-checkbox-state


 

@Html.EJS().Grid("DataOperations").DataSource(dataManger => {

      dataManger.Url("/Home/UrlDatasource").BatchUrl("/Home/BatchUpdate").Adaptor("UrlAdaptor");

}).Columns(col =>

{

    col.Field("OrderID").HeaderText("Order ID").IsPrimaryKey(true).Width("120").Add();

    col.Field("CustomerID").HeaderText("Customer Name").Width("150").Add();

    col.Field("Verified").HeaderTemplate("#VerifiedHtemplate").Template("#VerifiedCtemplate").Width("120").Add();

 

}).HeaderCellInfo("headerCellInfo").QueryCellInfo("queryCellInfo").Render()

 

<script id="VerifiedHtemplate" type="text/x-template">

    <div>Verified <input class="verified_h_checkbox" type="checkbox"/></div>

</script>

 

<script id="VerifiedCtemplate" type="text/x-template">

    <div><input class="verified_c_checkbox" type="checkbox" /></div>

</script>

 

 

<script>

    function headerCellInfo(args) {

        if (args.cell.column.field == "Verified") {

            // render the EJ2 Checkbox component

            var checkbox = new ej.buttons.CheckBox({

                change: checkboxChange_Header,

            });

            // Render initialized CheckBox.

            checkbox.appendTo(args.node.querySelector('.verified_h_checkbox'));

        }

    }

    function checkboxChange_Header(args) {

        console.log(args);

    }

 

    function queryCellInfo(args) {

        if (args.column.field == 'Verified') {

           // render the EJ2 Checkbox component

            var checkbox = new ej.buttons.CheckBox({

                checked: args.data[args.column.field], // bind the cell value

                change: checkboxChange_Cell,

            });

            // Render initialized CheckBox.

            checkbox.appendTo(args.cell.querySelector('.verified_c_checkbox'));

        }

    };

    function checkboxChange_Cell(args) {

        console.log(args);

    }

 

</script>

 


Find the below sample for your reference,


Sample: https://www.syncfusion.com/downloads/support/directtrac/general/ze/mvc_grid_headerTemplate_checkbox1163551966.zip


Please get back to us if you need further assistance.


Regards,

Rajapandiyan S



BR Bernadus Rangga Kresna Waskita July 7, 2022 03:50 AM UTC

thanks for the respond, the data is rendered now thanks for that, but the header checkbox still doesnt work..
I want to when the user change the value of the check box, all the record is checked when the header is checked, and change all to uncheck when header is unchecked,
I already know how to execute it to db when the row is checked using another button as update its value.. so yeah i need to check all or uncheck all from its header, thanks.. I tried this from ur code below:

but its still doesnt work ^_^ maybe my approach is wrong



RS Rajapandiyan Settu Syncfusion Team July 8, 2022 11:11 AM UTC

Hi Bernadus,


In your requirement, you want to edit all the cell values (current page) through the header checkbox click. We suggest you to use the Batch Edit feature to edit multiple cells at a time in the Grid.


Batch Edit: https://ej2.syncfusion.com/aspnetmvc/documentation/grid/editing/batch-editing

Demo: https://ej2.syncfusion.com/aspnetmvc/Grid/BatchEditing#/fluent


By using updateCell method inside the header checkbox change event, you can programmatically update all the cell values.


updateCell: https://ej2.syncfusion.com/javascript/documentation/api/grid/#updatecell


 

    function checkboxChange_Header(args) {

        console.log(args);       

        var grid= document.getElementById('Grid').ej2_instances[0];

        grid.getCurrentViewRecords().map((x, index) => {

            grid.updateCell(index, "Verified", args.checked)

        })

    }

 


Using the code example below, you can update a particular cell value in the checkbox change event.


 

    function checkboxChange_Cell(args) {

        console.log(args);

        var grid= document.getElementById('Grid').ej2_instances[0];

        var cell = args.event.target.closest('td');

        var rowInfo = grid.getRowInfo(cell);

        grid.updateCell(rowInfo.rowIndex, rowInfo.column.field, args.checked); // update the cell

    }

 


Sample: https://www.syncfusion.com/downloads/support/directtrac/general/ze/mvc_grid_updatecell_header_checkbox1068496786.zip


Please get back to us if you need further assistance.


Regards,

Rajapandiyan S


Marked as answer

BR Bernadus Rangga Kresna Waskita July 12, 2022 02:15 AM UTC

Perfectt, thanks for the answer. It's just what I needed



RS Rajapandiyan Settu Syncfusion Team July 12, 2022 03:49 AM UTC

Hi Bernadus,


We are glad to hear that you have achieved your requirement with the solution provided.


Please get back to us if you need further assistance.


Regards,

Rajapandiyan S


Loader.
Up arrow icon