I have successfully implemented a Bootstrap DataTable component in my Asp.net Core MVC App to display data from my database. However, I am facing a challenge when trying to add a sparkline to a column. The sparkline component requires an array of values, but I only have a comma-separated string. I need assistance in understanding how to modify the JavaScript code to accommodate my data type.
To simplify the issue, I created a new App without a database and directly defined data in the JavaScript, similar to the example shown here: https://datatables.net/examples/basic_init/stocks.html
While this example works perfectly, I need to adjust the format of values in the "last" field from an array to a string, as shown below:
var stock_data = [
{
"name": "ACME Gadgets",
"symbol": "AGDTS",
"last": "2.57, 2.54, 2.54, 2.56, 2.57, 2.58, 2.59"
},
.....
]
Here is the JavaScript code that requires modification:
$(document).ready(function () {
debugger;
var stock_data = [
{
"name": "ACME Gadgets",
"symbol": "AGDTS",
"last": [2.57, 2.54, 2.54, 2.56, 2.57, 2.58, 2.59]
},
{
"name": "Spry Media Productions",
"symbol": "SPMP",
"last": [1.12, 1.11, 1.08, 1.08, 1.09, 1.11, 1.08]
},
...
];
// DataTable initialization and other functionalities
});
Despite trying to use .split(", ") in the data rendering section, I have been unsuccessful. Debugging the JavaScript code (in site.js) has also proven challenging, as console.log statements do not produce any output.
UPDATE
Thanks to @andrewJames, my code now works flawlessly without the need for any data transformation. I have also added additional functionalities such as grouping by columns and formatting datetime display using moment.js.
Here is the updated code:
$(document).ready(function () {
// DataTable configuration with grouping and datetime formatting
});
One remaining issue is that when I enable responsive: true
, the sparkline column gets hidden. Upon expanding the row to show hidden columns, the entire array of values is displayed instead of the sparkline. It seems that the drawCallback function is not applied to hidden columns.
I believe I need to capture the mouse click on the expand icon and dynamically inject the canvas to display the sparkline, but I am unsure how to achieve this.