Quantcast
Channel: Recent Discussions — DataTables forums
Viewing all articles
Browse latest Browse all 82132

Dynamically display aoColumns

$
0
0
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:
<th>Column1</th>
@if (UserCanSeeThis)
{ 
    <th>SecretColumn</th>
}
<th>Column3</th>

.js: (sorry for the dump)
  $('#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" }
            ]
});

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"

Viewing all articles
Browse latest Browse all 82132

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>