Display arbitrary columns by string index, allow subsets of columns in all cases
Display arbitrary columns by string index, allow subsets of columns in all cases
Ironwil616
Posts: 50Questions: 0Answers: 0
This is more or less currently available, but combining certain functionalities would, in my opinion, offer a very useful tool. In a table, at runtime, I would like to send dataTables something like:
[code]
$('#myTable').dataTable({
"ColOrder": {
[ 'col 1 name', 'col 16 name', 'col 3 name' ]
},
});
[/code]
In this instance, only 3 columns from the table will be rendered visibly, and in the order specified. Datatables would see that I have only a subset of the total columns, order them as listed, and hide all the others. Currently, to achieve this, I'm using the ColReorder and ColVis plugins (which I assume would still be necessary, but transparently), and the following code:
[code]
var orderedColumns =
eval('[<% =string.Join(", ", Model.OrderedColumns) %>]');
var hiddenColumns =
eval('[<% =string.Join(", ", Model.HiddenColumns) %>]');
$('#allProjects').dataTable({
"sDom": 'Rlfrtip',
"oColReorder": {
"aiOrder": orderedColumns
},
"aoColumnDefs": [
{ "bVisible": false, "aTargets": hiddenColumns }
]
});
[/code]
I'm using C# and ASP.NET MVC. My controller code is creating the two integer collections for orderedColumns and hiddenColumns, which are then converted to JavaScript arrays as shown. I have a static C# method that takes in a string collection of the columns to render and returns a collection of integers that holds the index of each column and the order in which they should appear. All columns that should not be visible are then tacked onto the end of this collection so I don't get an error for the number of columns of the table not matching the number of indexes in my orderedColumns collection. Then I use those same tacked-on values to hide the columns I don't want shown. This works, and works well, but the code (my code) is brittle and clunky. I have to keep the table cell entries in a certain order, etc., both in code and in the web page so they match. It would be extremely cool to replace this with something like the code I suggested at the top of this post.
I really like the "aoColumnDefs" option to use only a subset of all possible columns. I'm not sure how much effort it would take, but I think removing the "aoColumns" entirely, and using "aoColumnDefs" for all cases would be better. I have no idea if this would cause inefficiencies or other problems. Probably it would break previous versions, unless you could just forward calls from "aoColumns" to "aoColumnDefs".
Also, this is mostly just a syntax sugar suggestion, but changing initialization code like:
[code]
"sDom": 'Rlfrtip',
"oColReorder": { "aiOrder": [ 4, 3, 2, 1, 0 ]}
[/code]
and replacing it with something like:
[code]
"ColDragDrop": 'true',
"ColOrder": { [ 4, 3, 2, 1, 0 ] }
[/code]
This would be primo. Then the initialization code in the plugin could just insert the relevant values into the settings object. I'm sure that would save you a lot of time answering questions about initialization options. Even better would be to allow the string indexers instead of integer indexers. This last bit is just for extra coolness.
[code]
$('#myTable').dataTable({
"ColOrder": {
[ 'col 1 name', 'col 16 name', 'col 3 name' ]
},
});
[/code]
In this instance, only 3 columns from the table will be rendered visibly, and in the order specified. Datatables would see that I have only a subset of the total columns, order them as listed, and hide all the others. Currently, to achieve this, I'm using the ColReorder and ColVis plugins (which I assume would still be necessary, but transparently), and the following code:
[code]
var orderedColumns =
eval('[<% =string.Join(", ", Model.OrderedColumns) %>]');
var hiddenColumns =
eval('[<% =string.Join(", ", Model.HiddenColumns) %>]');
$('#allProjects').dataTable({
"sDom": 'Rlfrtip',
"oColReorder": {
"aiOrder": orderedColumns
},
"aoColumnDefs": [
{ "bVisible": false, "aTargets": hiddenColumns }
]
});
[/code]
I'm using C# and ASP.NET MVC. My controller code is creating the two integer collections for orderedColumns and hiddenColumns, which are then converted to JavaScript arrays as shown. I have a static C# method that takes in a string collection of the columns to render and returns a collection of integers that holds the index of each column and the order in which they should appear. All columns that should not be visible are then tacked onto the end of this collection so I don't get an error for the number of columns of the table not matching the number of indexes in my orderedColumns collection. Then I use those same tacked-on values to hide the columns I don't want shown. This works, and works well, but the code (my code) is brittle and clunky. I have to keep the table cell entries in a certain order, etc., both in code and in the web page so they match. It would be extremely cool to replace this with something like the code I suggested at the top of this post.
I really like the "aoColumnDefs" option to use only a subset of all possible columns. I'm not sure how much effort it would take, but I think removing the "aoColumns" entirely, and using "aoColumnDefs" for all cases would be better. I have no idea if this would cause inefficiencies or other problems. Probably it would break previous versions, unless you could just forward calls from "aoColumns" to "aoColumnDefs".
Also, this is mostly just a syntax sugar suggestion, but changing initialization code like:
[code]
"sDom": 'Rlfrtip',
"oColReorder": { "aiOrder": [ 4, 3, 2, 1, 0 ]}
[/code]
and replacing it with something like:
[code]
"ColDragDrop": 'true',
"ColOrder": { [ 4, 3, 2, 1, 0 ] }
[/code]
This would be primo. Then the initialization code in the plugin could just insert the relevant values into the settings object. I'm sure that would save you a lot of time answering questions about initialization options. Even better would be to allow the string indexers instead of integer indexers. This last bit is just for extra coolness.
This discussion has been closed.