Click here to Skip to main content
15,887,175 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi
am using angular js and ajax datatable using but am declare the datatable inside script i got error in "Datatable not a function" error came

i declare all supporting script files. my code is

What I have tried:

$scope.getalldata = function () {
        
        $http({
            method: "get",
            url: "https://localhost:44336/admin/unittype/getalldata"
        }).then(function (response) {
            $scope.record = response.data;

            angular.element(document).ready(function () {
                
                dTable = $('#tbl_unittypelist').DataTable({
                    "processing": false,
                    "sorting": true,
                    "serverSide": false,
                    "order": [[0, 'desc']],
                    "paging": true,
                    "bDestroy": true,
                    "fnStateSave": function (oSettings, oData) {
                        localStorage.setItem('DataTables', JSON.stringify(oData));
                    },
                    "fnStateLoad": function (oSettings) {
                        return JSON.parse(localStorage.getItem('DataTables'));
                    },
                    //header checkbox
                    headerCallback: function (thead, data, start, end, display) {
                        thead.getElementsByTagName('th')[0].innerHTML = `
                    <label class="">
                        <input name="select_all" type="checkbox" value="1" class="select_all" id="select_all" />
                        <span></span>
                    </label>`;
                    },
                    'rowCallback': function (row, data, dataIndex) {
                        var rowId = data.supid;//check this id

                        if ($.inArray(rowId, rows_selected) !== -1) {
                            $(row).find('input[type="checkbox"]').prop('checked', true);
                        }
                        else {
                            $(row).find('input[type="checkbox"]').prop('checked', false);
                        }
                    },

                    "footerCallback": function (row, data, start, end, display) {
                    },

                    "columnDefs": [
                        {
                            targets: 0,
                            width: '30px',
                            className: 'dt-left dtr-control',
                            orderable: false,
                            render: function (data, type, full, meta) {
                                console.log(data);
                                return '<label class="checkboxs"> ' +
                                    '<input type="checkbox" id="chk_item" class="checkable" name="chk_item" value=' + data + ' />' +
                                    '<span></span>' +

                                    '</label>';
                            },
                        }
                    ],
                    select: {//this section add class name for selected checkbox
                        style: 'os',
                        className: 'selecteds'
                    },


                });
            });

            //for each page selectable checkbox while click on heaser checkbox --start
            $('#tbl_unittypelist tbody').on('click', 'input[type="checkbox"]', function (e) {
                var $row = $(this).closest('tr');
                var data = dTable.row($row).data();
                var rowId = data.unittypeid;
                var index = $.inArray(rowId, rows_selected);


                if (this.checked && index === -1) {
                    rows_selected.push(rowId);
                } else if (!this.checked && index !== -1) {
                    rows_selected.splice(index, 1);
                }
                updateDataTableSelectAllCtrl(dTable);
                e.stopPropagation();
            });

            $('#tbl_unittypelist thead input[name="select_all"]', dTable.table().container()).on('click', function (e) {

                if (this.checked) {

                    $('#tbl_unittypelist tbody input[type="checkbox"]:not(:checked)').trigger('click');
                } else {

                    $('#tbl_unittypelist tbody input[type="checkbox"]:checked').trigger('click');
                }
                e.stopPropagation();
            });
            //for each page selectable checkbox while click on heaser checkbox --end
            dTable.on('draw', function () {
                updateDataTableSelectAllCtrl(dTable);
            });


            //header select all checkbox click action --start
            dTable.on('change', '.select_all', function () {
                var set = $(this).closest('table').find('td:first-child .checkable');
                var checked = $(this).is(':checked');

                $(set).each(function () {
                    if (checked) {
                        $(this).prop('checked', true);
                        //$(this, allPages).closest('tr').addClass('selecteds');

                        dTable.rows().select();
                        dTable.rows().every(function () {
                            var d = this.data();

                            var rowId = d.unittypeid;//this rowid changed for every table default id
                            var index = $.inArray(rowId, rows_selected);
                            if (index === -1) {
                                rows_selected.push(rowId);
                            }
                        });
                        $('#btn_deleteall').show();
                    }
                    else {
                        $(this).prop('checked', false);
                        //$(this).closest('tr').removeClass('selecteds');
                        dTable.rows().deselect();
                        rows_selected = [];
                        $('#btn_deleteall').hide();
                    }
                });
            });
        //header select all checkbox click action --end
            //for each row checkbox click action --start
            dTable.on('change', 'tbody tr .checkboxs', function () {
                var checked = $(this).find('.checkable').is(':checked');
                if (checked) {
                    $(this).parents('tr').addClass('selecteds');

                    $('#btn_deleteall').show();
                }
                else {
                    $(this).parents('tr').removeClass('selecteds');
                    var count = dTable.rows('.selecteds').count();
                    if (count < 1) {
                        $('#btn_deleteall').hide();
                    };

                }

            });
            //for each row checkbox click action --end
            $('.dataTables_wrapper').children('div').removeClass("btn-group");



        }, function () {
            alert("Error Occur");
        })
    };
Posted
Updated 31-Jul-23 3:45am

1 solution

It looks like your 'DataTables' library is not properly loaded or initialized before you make a call to it. Make sure that you have included the jQuery library before loading the 'DataTables' library.

You should have the following declared first, you must load jQuery before 'DataTables' -
HTML
<!--Include jQuery first -->
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>

<!--Include DataTables CSS file second -->
<link rel="stylesheet" href="https://cdn.datatables.net/1.11.5/css/jquery.dataTables.min.css">

<!--Include DataTables JS last -->
<script src="https://cdn.datatables.net/1.11.5/js/jquery.dataTables.min.js"></script>


Check your browser's developer console (F12) to see if there are any errors in to loading jQuery. If there are, solve those errors first.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900