简体   繁体   中英

Sending Parameters to Common JS file

Im wondering if it is possible to minimize my code by having a common JS file, As there will be many datatables around my application..

All the Datatables will have same theme only difference will be of data.

Like this script i have

    var oTable =  $('#ManageForms').dataTable({
         "aoColumns": [
             /* ID */   {
                 "bVisible":    false,
                 "bSortable":   false,
                 "bSearchable": false
             },
             /* Form Name */  null,
             /* Form Path */  null,
             /* Form CI Path */  null,
             /* Actions */  null
         ],

        "bServerSide":true,
        "bProcessing":true,
        "bJQueryUI": true,
        "sPaginationType": "full_numbers",
        //"bFilter":true,
        //"sServerMethod": "POST",
        "sAjaxSource": "{{base_url()}}admin/configurations/listForms_DT/",
        "iDisplayLength": 25,
        "aLengthMenu": [[2, 25, 50, -1], [2, 25, 50, "All"]],
        /*"sEcho": 1,
        "columns":[
            {data:"FormName"},
            {data:"FormPath"},
            {data:"FormCIPath"},
            { "data": null,
                "defaultContent": "<a href='#editBtnModal' class='editBtnFunc' ><i style='color: #666666' class='fa fa-pencil fa-fw fa-2x'></i></a><a href='#' id='deleteBtn'><i style='color: #ff0000' class='fa fa-times fa-fw fa-2x'></i></a>",
                "targets": -1
            }
        ],*/
        'fnServerData'   : function(sSource, aoData, fnCallback){
            $.ajax ({
                'dataType': 'json',
                'type'    : 'POST',
                'url'     : sSource,
                'data'    : aoData,
                'success' : fnCallback
            }); //end of ajax
        },
        'fnRowCallback': function(nRow, aData, iDisplayIndex, iDisplayIndexFull) {
            $(nRow).attr("data-id",aData[0]);
            return nRow;
        }
    });

i guess everything will be same except the sAjaxSource": "{{base_url()}}admin/configurations/listForms_DT/", and

         "aoColumns": [
             /* ID */   {
                 "bVisible":    false,
                 "bSortable":   false,
                 "bSearchable": false
             },
             /* Form Name */  null,
             /* Form Path */  null,
             /* Form CI Path */  null,
             /* Actions */  null
         ],

so instead of copy pasting the code again and again on every page of datatables. is there any way i put this code in single common.js file and send the parameters for specific datatable.

You should put your code in a function which could be call in each specific case with the url needed as param:

function getData(url){
   // ...
  "sAjaxSource": url
  // ...
}

this function can be in a common js and then use in any other code that is after you import this file.

The array in aoColumns can also be passed as param as the other field.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM