datatables initizalize two or more times with respect to function call

问题: i have three select fields on last select onchange event call.(function clas). First time it runs and datatables initilize but when the second time event runs and function...

问题:

i have three select fields on last select onchange event call.(function clas). First time it runs and datatables initilize but when the second time event runs and function call and datatable again initilize it makes Error. i dont know how to set the format or how i can initialize datatable outside the function . i just want to initilize datatables for once but call ajax every time with respect to function here is my code And also ajax runs first time automatically with datatable initilization but second time ajax call when event run and function call

     function clas(src)
    {

    var dataTable = $('#user_data').DataTable({
  "processing":true,
  "serverSide":true,
  "order":[],
   "ajax":{
   url:"requests/add_students/fetch.php",
   type:"POST",
    data: {
          "class_name": 2,
          "class_section": 3,
          "class_session": 4
        }
  },
    dom: 'lBfrtip',
  buttons: [
            {
                extend: 'copyHtml5',
                exportOptions: {
                    columns: [ 0, ':visible' ]
                }
            },
            {
                extend: 'excelHtml5',
                exportOptions: {
                    columns: ':visible'
                }
            },
            {
                extend: 'pdfHtml5',
                exportOptions: {
                    columns: [ 1, 2 ]
                },

            },
            {
                extend: 'csvHtml5',
                exportOptions: {
                    columns: [ 1, 2 ]
                },


            }

        ],
   "lengthMenu": [ [10, 25, 50, -1], [10, 25, 50, "All"] ],
  "columnDefs":[
   {
    "targets":[0, 3, 4],
    "orderable":false,
   },
  ],

 });
    }

回答1:

You should check if datatable exist or not before initialing it. If it is already initialized then no need to to re initiate it. Check below code:

function clas(src) {    
            if (!$.fn.DataTable.isDataTable('#user_data')) {
                var dataTable = $('#user_data').DataTable({
                    "processing":true,
                    "serverSide":true,
                    "order":[],
                    "ajax":{
                        url:"requests/add_students/fetch.php",
                        type:"POST",
                        data: {
                            "class_name": 2,
                            "class_section": 3,
                            "class_session": 4
                        }
                    },
                    dom: 'lBfrtip',
                    buttons: [
                        {
                            extend: 'copyHtml5',
                            exportOptions: {
                                columns: [ 0, ':visible' ]
                            }
                        },
                        {
                            extend: 'excelHtml5',
                            exportOptions: {
                                columns: ':visible'
                            }
                        },
                        {
                            extend: 'pdfHtml5',
                            exportOptions: {
                                columns: [1, 2]
                            },
                        },
                        {
                            extend: 'csvHtml5',
                            exportOptions: {
                                columns: [1, 2]
                            },

                        }
                    ],
                    "lengthMenu": [[10, 25, 50, -1], [10, 25, 50, "All"]],
                    "columnDefs": [
                        {
                            "targets": [0, 3, 4],
                            "orderable": false
                        }
                    ]
                });
            }
        }
  • 发表于 2019-02-14 05:43
  • 阅读 ( 235 )
  • 分类:网络文章

条评论

请先 登录 后评论
不写代码的码农
小编

篇文章

作家榜 »

  1. 小编 文章
返回顶部
部分文章转自于网络,若有侵权请联系我们删除