skip to Main Content

I want to keep the column state in an array with stateSaveCallback in jQuery datatable. By default, I have 13 columns and I only have 9 of them hide/show in my colvis button. My problem is that the array I keep is 13 true in the first place, after starting to hide, the length of the array decreases by 1 every time I hide it. however, the length of the array I want should remain constant 13, if the index is hidden, it should turn into false in that array, I’ve been trying for a long time, please help

I just tried colvis to rotate between the columns I added to the button, but it still showed the same behavior.

2

Answers


  1. Chosen as BEST ANSWER

    this is my stateSaveCallback function

    stateSaveCallback: function (settings, data) {
                        var columns = [];
                        $('#' + tableId + ' th').each(function () {
                            var index = $(this).index();
                            columns.push(settings.aoColumns[index].bVisible ? true : false);
                        });
                        console.log(columns);
                        var portfolioId = "@ViewBag.CurrentPortfolio.Id";
                        
                        var columnStatus = columns;
                        $.ajax({
                            type: 'POST',
                            url: '@Url.Action("DataTableVisibilityCustomization", "Portfolio")',
                            data: { ColumnStatus: JSON.stringify(columnStatus), portfolioId: portfolioId },
                            success: function (result) { console.log(result.data); }
                        });
                    },


  2. SOLVED
    stateSaveCallback: function (settings, data) {
    var columns = element.columns().visible();

                    var colVisState = $.map(columns, function (col, i) {
                        return col ? true : false;
                    });
    
                    data.colVisState = colVisState;
                    var portfolioId = "@ViewBag.CurrentPortfolio.Id";
    
                    var columnStatus = data.colVisState;
                    $.ajax({
                        type: 'POST',
                        url: '@Url.Action("DataTableVisibilityCustomization", "Portfolio")',
                        data: { ColumnStatus: JSON.stringify(columnStatus), portfolioId: portfolioId },
                        success: function (result) { console.log(result.data); }
                    });
                },
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search