Dynamically display aoColumns
Dynamically display aoColumns
My Question is related to this one http://www.datatables.net/forums/discussion/13964/create-columns-dynamically-based-on-user-role/p1 .
I Am trying to hide/display columns based on user permissions. I can dynamically control the HTML, but in the .js, since the HTML column doesn't exist, I get an error. Setting bVisible doesn't help because it's still trying to find a column that doesn't exist.
HTML:
[code]
Column1
@if (UserCanSeeThis)
{
SecretColumn
}
Column3[/code]
.js: (sorry for the dump)
[code] $('#grdSearch').dataTable( {
"bProcessing": true,
"sAjaxSource": uri,
"fnServerData": function(sSource, aoData, fnCallback, oSettings) {
oSettings.jqXHR = $.ajax(
{
type: "POST",
url: sSource,
data: JSON.stringify(BuildSearchParams()),
contentType: "application/json",
dataType: 'json',
success: function (data) {
if (typeof data["error"] == "undefined") {
fnCallback(data);
} else {
alert(data["error"]);
}
}
});
},
"aoColumns": [
{ "mData": "SourcePartNumber" },
{ "mData": "SourceVendorName" },
{ "mData": "SourcePartDescription" },
{ "mData": "TargetPartNumber" },
{ "mData": "TargetPartDescription" },
{ "mData": "CrossType" }
]
});
[/code]
When I run this at first nothing happens, then I click again and I get a message that says "DataTables warning (table id = 'grdSearch'): Cannot reinitialize DataTable. To retrieve DataTable object for this table, pass no arguments or see the docs for bRetrieve and bDestroy"
I Am trying to hide/display columns based on user permissions. I can dynamically control the HTML, but in the .js, since the HTML column doesn't exist, I get an error. Setting bVisible doesn't help because it's still trying to find a column that doesn't exist.
HTML:
[code]
Column1
@if (UserCanSeeThis)
{
SecretColumn
}
Column3[/code]
.js: (sorry for the dump)
[code] $('#grdSearch').dataTable( {
"bProcessing": true,
"sAjaxSource": uri,
"fnServerData": function(sSource, aoData, fnCallback, oSettings) {
oSettings.jqXHR = $.ajax(
{
type: "POST",
url: sSource,
data: JSON.stringify(BuildSearchParams()),
contentType: "application/json",
dataType: 'json',
success: function (data) {
if (typeof data["error"] == "undefined") {
fnCallback(data);
} else {
alert(data["error"]);
}
}
});
},
"aoColumns": [
{ "mData": "SourcePartNumber" },
{ "mData": "SourceVendorName" },
{ "mData": "SourcePartDescription" },
{ "mData": "TargetPartNumber" },
{ "mData": "TargetPartDescription" },
{ "mData": "CrossType" }
]
});
[/code]
When I run this at first nothing happens, then I click again and I get a message that says "DataTables warning (table id = 'grdSearch'): Cannot reinitialize DataTable. To retrieve DataTable object for this table, pass no arguments or see the docs for bRetrieve and bDestroy"
This discussion has been closed.
Replies