Option to display only certain columns?
Option to display only certain columns?
wastedspace
Posts: 5Questions: 0Answers: 0
Hi,
I would like to have the option of only displaying the first x amount of columns in a table which is dynamically built with json. Is this possible? Reason being is that when the table is first loaded, I just want to display a subset of the data as a kind of summary. I don't have the option currently of tweaking the server-side code unfortunately. It is currently returning about 15 columns of data. I thought something like the code below would work to display only the first 4 columns, but it doesn't:
[code]$.getJSON("url/path/here.php", null, function (json) {
var columns = [];
$.each(json.headers, function(i, value){
var obj = { sTitle: value };
columns.push(obj);
});
$("#example").dataTable( {
"bProcessing": true,
"bPaginate": true,
"sPaginationType": "full_numbers",
"aaData": json.rows,
"aoColumns": columns,
"aoColumnDefs": [
{ "bVisible": true, "aTargets": [ 0,1,2,3 ] }
]
});
}); [/code]
Also, as you can see I'm building the column titles dynamically. I am potentially going to be dealing with thousands of rows, so was hoping to use the server-side processing feature. But is this possible with dynamic headers?
I'm fairly new to DataTables, so please be gentle ;)
Many thanks!
I would like to have the option of only displaying the first x amount of columns in a table which is dynamically built with json. Is this possible? Reason being is that when the table is first loaded, I just want to display a subset of the data as a kind of summary. I don't have the option currently of tweaking the server-side code unfortunately. It is currently returning about 15 columns of data. I thought something like the code below would work to display only the first 4 columns, but it doesn't:
[code]$.getJSON("url/path/here.php", null, function (json) {
var columns = [];
$.each(json.headers, function(i, value){
var obj = { sTitle: value };
columns.push(obj);
});
$("#example").dataTable( {
"bProcessing": true,
"bPaginate": true,
"sPaginationType": "full_numbers",
"aaData": json.rows,
"aoColumns": columns,
"aoColumnDefs": [
{ "bVisible": true, "aTargets": [ 0,1,2,3 ] }
]
});
}); [/code]
Also, as you can see I'm building the column titles dynamically. I am potentially going to be dealing with thousands of rows, so was hoping to use the server-side processing feature. But is this possible with dynamic headers?
I'm fairly new to DataTables, so please be gentle ;)
Many thanks!
This discussion has been closed.
Replies
colvis is an extra which lets users change the columns they can see, which might be exactly what you are looking for.
http://datatables.net/extras/colvis/
[code]
$.getJSON("url/path/here.php", null, function (json) {
var columns = [];
$.each(json.headers, function(i, value){
var obj = { sTitle: value };
columns.push(obj);
});
$("#example").dataTable( {
"bProcessing": true,
"bPaginate": true,
"sPaginationType": "full_numbers",
"aaData": json.rows,
"aoColumns": columns,
"aoColumnDefs": [
{ "bVisible": false, "aTargets": [ '_all' ] },
{ "bVisible": true, "aTargets": [ 0,1,2,3 ] }
]
});
});
[/code]
"_all" is a magic string for "aTargets" which will target all columns, and then the second entry overrides that. "_all" is currently the only magic string for aTargets, although other options in future might be "_odd", "_even" etc...
Regards,
Allan
colvis isn't really an option here, as I don't want to give the user the control in this particular instance.
Allan - I did try your example, but it didn't seem to work. I just get a blank space where the table should be... strangely though, I see no errors in the Firebug console...
This did work however (although perhaps not the most elegant):
[code]$.getJSON("url/path/here.php", null, function (json) {
var columns = [];
$.each(json.headers, function(i, value){
var obj = { sTitle: value };
columns.push(obj);
});
var oTable = $("#example").dataTable( {
"bProcessing": true,
"bPaginate": true,
"sPaginationType": "full_numbers",
"aaData": json.rows,
"aoColumns": columns,
"aoColumnDefs": [
{ "bVisible": false, "aTargets": [ '_all' ] }
]
});
var i=0;
for (i=0;i<=3;i++){
oTable.fnSetColumnVis(i, true );
}
}); [/code]
[code]
$.getJSON("url/path/here.php", null, function (json) {
var columns = [];
$.each(json.headers, function(i, value){
var obj = { sTitle: value };
columns.push(obj);
});
$("#example").dataTable( {
"bProcessing": true,
"bPaginate": true,
"sPaginationType": "full_numbers",
"aaData": json.rows,
"aoColumns": columns,
"aoColumnDefs": [
{ "bVisible": true, "aTargets": [ 0,1,2,3 ] },
{ "bVisible": false, "aTargets": [ '_all' ] }
]
});
});
[/code]
Allan
can you put this into the documentation at http://www.datatables.net/ref#aTargets ?
I saw that you were planning on allowing named indexes for aoColumns/Defs and was wondering if you were planning on implementing this through aTargets or some other variable name. http://www.datatables.net/forums/discussion/7184/defining-columns-by-name-instead-of-position-aka-aocolumndefs-for-makeeditable#Item_3
> I saw that you were planning on allowing named indexes
I'm not quite sure I follow - sorry. Currently you can use a class name which is on the column TH element to target columns, or the magic _all or index numbers. If the columns were named (which is quite possible and could be very nice) that would probably need to be another parameter such as mDataProp or something else.
Allan
Ironically though, I no longer need this functionality, as I can now control the column visibility from the server side before it is injected into the table(!). Good to know though, as I spotted another problem... If you use the search filter, it still searches through the hidden columns too, giving head-scratching results for those who don't know about the hidden columns - unless there is a setting to stop this happening?
BTW never said. This is a fantastic tool!