0

I have similar case to a question bellow

Datatable unable to export updated checkbox values in excel as 0 or 1, alway showing the initially checked values only

I have a jQuery datatable and every thing is going alright from binding on the server side to showing data, but I need to submit the datatable from my razor page to another controller. So, I need the checked rows and non checked rows as well with their ids, so when I submit to controller all data in datatable are submitted well except the checkboxes values are not being updated ,they have been sent with their initial values ,even if I checked or unchecked them , I got only the initial value they got from the server on first bind

@section scripts {
    <script>
        var obj = @Html.Raw(Json.Serialize(Model));

        var storeId = document.getElementById('idStore').value;
        
        var tableParam;
        $(document).ready(function () {
         $('#myTable').DataTable({
            "proccessing": true,
            "serverSide": true,
            "ajax": {
                url: "/item/loaddata",
                type: 'POST',
                headers: { 'RequestVerificationToken': $('@Html.AntiForgeryToken()').val() },
                data: { "StoreId": storeId }
             },
             "columnDefs": [
                 {
                     "targets": 0,
                     "checkboxes": {
                         "selectRow": true
                     },
                 }
             ],
             "select": {
                 "style": "multi"
             },
            "columns": [


                {
                      "data": "selected", "searchable": false, "render": function (data, type, row) {
                         
                            return '<input type="checkbox" class="chk"' + (data ? ' checked' : '') + '/>';
                    }
                },

                {"name":"SNumber", "data": "tname"},
                { "name": "MName", "data": "lname" },
                { "name": "SGuid", "data": "itemguid", "visible": false, "searchable": false },



            ],
            "order": [[0, "desc"]]
        });


        });


       
        
            
    </script>

    <script type="text/javascript">


        $(document).on("click", "#btnSave", function (e) {
              var table = $('#myTable').DataTable();
 
            var datac = table.rows(function (idx, data, node) {
                return $(node);
            }).data().toArray();

             
                 
             
            $.ajax({
                url: "/inventory/saveselecteditems",
                type: 'post'
               , data: { "testmodel": datac }

            });



        });

       
    </script>



}
react_or_angluar
  • 1,568
  • 2
  • 13
  • 20
Khalid
  • 343
  • 3
  • 16
  • i can handle checked or uncheck rows by manual way like ,var data = table.rows( function ( idx, data, node ) { return $(node).find('.chk:input[type="checkbox"]').prop('checked'); but i want them to be binded to the submitted data ,i dont want to process the selected indexes and re-inject them in the array again – Khalid Nov 15 '20 at 06:04

1 Answers1

1

Datatable has a built-in function to get the selected rows.

table.rows({ selected: true }).data().toArray();

Change it like below:

$(document).on("click", "#btnSave", function (e) {
        var table = $('#myTable').DataTable();
        var datac = table.rows({ selected: true }).data().toArray();  
        $.ajax({
            url: "/inventory/saveselecteditems",
            type: 'post', 
            data: { "testmodel": datac }
        });
});

Update:

<script type="text/javascript">
    $(document).on("click", "#btnSave", function (e) {
        var table = $('#myTable').DataTable();
        var datac = table.rows(function (idx, data, node) {
            var chk = $(node).find("input:checkbox");
            if ($(chk).is(':checked')) {
                data.selected = true;
            } else {
                data.selected = false;
            }
            return $(node);
        }).data().toArray();
        $.ajax({
            url: "/inventory/saveselecteditems",
            type: 'post',
            data: { "testmodel": datac }

        });
    });
</script>
mj1313
  • 7,930
  • 2
  • 12
  • 32
  • thanks @mj1313 but i need the unselected rows also , not only the selected ones – Khalid Nov 16 '20 at 12:35
  • i did that already using the bellow code var arrayChecked = table.rows(function (idx, data, node) { return $(node).find('.chk:input[type="checkbox"]').prop('checked');; }).data().toArray(); – Khalid Nov 16 '20 at 12:36
  • i was thinking of a way to change the datatable value {selected} not only changing the html front , so when i submit the array to the server , the selected row shall have false val, some thread suggested that i must redraw the cell to take the change – Khalid Nov 16 '20 at 12:38
  • 1
    @Immortal I made some test, seems that {selected} value won't be changed whether check or uncheck it. After debug, I found you should change it value in the data property. See my update. – mj1313 Nov 17 '20 at 04:00