I'm struggling to populate my jQuery Datatable with a List. Currently, I am writing the list to a file and accessing it in my view-model
. Is this the correct approach?
This is the snippet of my code:
List<string> list = new List<string>();
foreach (var item in db.Pos)
{
var total = 0;
decimal costo = 0;
for (int i = 1; i <= 31; i++)
{
var value = 0;
if (item.Fecha.Day == i) { value = item.Cantidad; costo = costo + item.Total; }
total += value;
}
// Add items to the list
}
// Serialize list to JSON and write to file
var json = JsonConvert.SerializeObject(new List<object>() { list });
System.IO.File.WriteAllText(@"\path.txt", json);
And my AJAX call to populate the Datatable:
$(document).ready(function () {
var table = $('#pftable_hdr').DataTable({
ajax: {
url: "/path.txt",
dataSrc: ""
},
scrollY: "500px",
scrollX: true,
scrollCollapse: true,
fixedColumns: {
leftColumns: 3
}
});
});
Sample output from my text file:
[["ENS FRUTAS","REST","CENAS","$26.50","0","1" ... "$25.50"]]
How can I properly load my list into the Jquery datatable? The text file should have brackets []
at the beginning and end but I'm having trouble achieving that.