Create datatable and then dyanamically change columns.

Create datatable and then dyanamically change columns.

jimkielyjimkiely Posts: 9Questions: 10Answers: 0

I have a bunch of functions like below and the only difference between each function is the model variable and the columns in the datatable. I would like to find a way to reduce the number of functions I have down to one. Any ideas?

var getModelTable = function () {
return $('#tblAdminGrid').DataTable({
"ajax": function (data, callback, settings) { // Make the Ajax call ourselves
$.ajax({
url: "../../Search/GetList",
type: "POST",
dataType: "json",
data: {
draw: data.draw, // Needed for paging
start: data.start, // Needed for paging
length: data.length, // Needed for paging
model: 'VehicleModel'
},
beforeSend: function (xhrObj) {
// Whatever
}
})
},
"deferRender": true,
"autoWidth": false,
"order": [[1, "asc"]],
"columns": [
{ "title": "ModelId", "data": "ModelId" },
{ "title": "Model", "data": "Model" },
{ "title": "Updated By", "data": "UpdateBy" },
{ "title": "Update Date", "data": "UpdateDate" },
{ "title": "Create By", "data": "AddBy" },
{ "title": "Create Date", "data": "AddDate" },
{ "title": "Action", "render": function (data, type, row) {
return '<a href=\"javascript:void(0)\" class=\"btn btn-info edit\" data-id=\"' + row.ModelId + '\"><i class=\"fa fa-edit\"></i>' + '  edit' + '</a> <a href=\"#\" class=\"btn btn-warning delete\" data-toggle=\"modal\" data-target=\"#modals-alerts-danger\" data-id=\"' +row.ModelId + '\"><i class="fa fa-times-circle"></i>' + '  delete' + '</a>';
},
"class": "col-lg-3",
"sortable": false,
"searchable": false
}
]
});
}

This discussion has been closed.