Suggestion: Dynamic changing of table headers
Suggestion: Dynamic changing of table headers
Hi,
I really need some advice regarding the best way to deal with dynamic table headers.
I have datatable, on the each value selection using dropdown menu, the table redraws and the header name changes
at the moment, I am doing it by hardcoding , like the example below
if(target == "Tomatoes"){
$( table.column(0).header() ).html('2 Boxes');
$( table.column(1).header() ).html('3 Boxes');
$( table.column(2).header() ).html('4 Boxes');
if(target == "Milk"){
$( table.column(0).header() ).html('2 Ltrs');
$( table.column(1).header() ).html('5 ltres');
$( table.column(2).header() ).html('6 ltrs');
and it continues..........
The issue is that, I have at least 150 products and defining the column header name for each product separately is going to make code longer/messy and time consuming. In addition to this, I need to amend column header names sometimes.
Can anyone please suggest me what is the best way to deal with this situation.
can I save them in json file/database and then append them to table?
Thank you
Kind Regards,
KT
Answers
I would look at using a Javascript object, something like this:
Then do something like this:
You could turn this into a for loop.
How you build the products object is up to your specific environment, Could be done client or server side.
Kevin
@kthorngren
Thank you very much!!!!
Created the js object on clients side and is working perfect. This will definitely save me loads of time.
Implement on the server side as well and again works perfect
Thank you