Get selected columns for a columntoggle table

99 views Asked by At

I have a columntoggle table with some information in it. I would like to get what are the selected columns, to stock those values in a database and when the user logs back in, using those values to make the table load with the columns he has previously selected.

Is there any way to do this?

Thanks by advance

1

There are 1 answers

1
ezanker On BEST ANSWER

There is nothing built-in to do this. Here is one way to do it:

jQM creates a column toggle popup with a checkbox for each column assigned a data-priority. The popup takes the id of the table plus '-popup', so you can access the checkboxes with this selector:

$("#myTableid-popup .ui-checkbox label");

Checked items have the class .ui-checkbox-on, while unchecked items have .ui-checkbox-off. Therefore you could get the indexes (indices) of all visible columns:

var selIndex = [];
function SaveSelectedColumns(){
    selIndex = [];
    $("#myTable-popup .ui-checkbox label").each(function(idx){
        if ($(this).hasClass("ui-checkbox-on")){
            selIndex.push(idx);    
        }
    });
}

Then to restore visible columns:

function LoadSavedColumns(){
    $("#myTable-popup .ui-checkbox label").each(function(idx){
        var vis = IsColVisible(idx);
        if ($(this).hasClass("ui-checkbox-on") && !vis){
            $(this).click();    
        }
         if ($(this).hasClass("ui-checkbox-off") && vis){
            $(this).click();    
        }
    });
}

function IsColVisible(idx){
    for (var i=0; i<selIndex.length; i++){
         if (selIndex[i] == idx) return true;   
    }
    return false;
}

Working DEMO