javascript - jQuery DataTables - Refactoring Code to remove 'aoColumn' duplication -


i'm using jquery datatables render timeline , each column represents day. have method called when each column (day) rendered , it's passed in date array.

is there better way write code below i'm not repeating myself 7 times whilst still being able pass in array item? cannot see stands out in docs.

datatable = $('#example').datatable({ "bretrieve":true, "bprocessing":true, "aadata": data, // datatables requires render function each column (day) "aocolumns":[     {         "mdata":null,         "fnrender":function (obj) {             return day(obj, week[0]);         }     },     {         "mdata":null,         "fnrender":function (obj) {             return day(obj, week[1]);         }     },     {         "mdata":null,         "fnrender":function (obj) {             return day(obj, week[2]);         }     },     {         "mdata":null,         "fnrender":function (obj) {             return day(obj, week[3]);         }     },     {         "mdata":null,         "fnrender":function (obj) {             return day(obj, week[4]);         }     },     {         "mdata":null,         "fnrender":function (obj) {             return day(obj, week[5]);         }     },     {         "mdata":null,         "fnrender":function (obj) {             return day(obj, week[6]);         }     } ] 

});

here simple solution, using $.map():

datatable = $("#example").datatable({     bretrieve: true,     bprocessing: true,     aadata: data,     aocolumns: $.map(array(7), function(value, index) {         return {             mdata: null,             fnrender: function(obj) {                 return day(obj, week[index]);             }         };     }) }); 

in code above, array(7) creates sparse array of 7 elements (indexed 0 6), set undefined. then, $.map() projects object literals indices in array. result array of object literals second argument day() varying current index.


Comments

Popular posts from this blog

Issues changing the value of an element in an array in matlab -

php - MySQLi binding parameters in a prepared statement doesn't work unless inserted after "WHERE" -

vb.net - Alternative to the T-SQL AS keyword -