Export from a table using snippet that uses table columns as export rows? Also only visible column

  • 1
  • Question
  • Updated 4 months ago
  • In Progress
I prefer using a snippet to export so i don't have to have checkboxes on my table, and it's just more user-friendly to have a button. 

However, in doing so, I lose the ability to use table columns as export columns. Is there a way to access the list of fields that are used in the table?

Even better, is there a way to access only the visible columns in a table? With the standard table export, it includes hidden columns, which is a bummer. 

I'm thinking if I could get an array of only the visible columns we could do this

var params = arguments[0],
	$ = skuid.$;

var model = skuid.$M('ModelName');


var fields = [
	
	//Insert Array of API names for visible columns in table
	
	    ];



model.exportData({
    fields: fields,
    doNotAppendRowIdColumn: true,
    useAPINamesForHeaders: false
    });
Photo of Jack Sanford

Jack Sanford, Champion

  • 8,322 Points 5k badge 2x thumb
  • hopeful

Posted 4 months ago

  • 1
Photo of Jack Sanford

Jack Sanford, Champion

  • 8,322 Points 5k badge 2x thumb
Solved!

mega thanks to Youri with his snippets to get at the column names in a table and his super cool idea to have buttons that can quickly change the set of visible columns: https://community.skuid.com/skuid/topics/snippet-to-show-hide-set-of-column-in-table

var params = arguments[0],
	$ = skuid.$;
	
var model = skuid.$M('ListingTransplant');	

var fields = [];
var columnsArray = [];

var settings = params.component._personalizationService.getSettings();
var columns = settings.columnSettingsByUID;

//put each column into an array that can be sorted
$.each (columns, function(i,column){
    columnsArray.push(column);
});
columnsArray.sort(function(a,b) {return (a.order > b.order) ? 1 : ((b.order > a.order) ? -1 : 0);} );

//loop through the columns and push the field api name to the fields array
$.each (columnsArray, function(i,column){
    
    var field = column.fieldId;
    var hidden = column.userHidden;
    
    if (!!field && !hidden){
        fields.push(field);
    }
    
});

//the result gets populated in reverse order, this puts it right
var fieldNames = fields.reverse();


//get the field objects from each field api name
//note template fields are not included, and lookup fields seem to always return the Id not the Name
var fieldsWithCorrectLabels = $.map(fieldNamess, function(v){
    var actualField = model.getField(v);
     
    return {
        id: actualField.id,
        label: actualField.label
    };     
     
});


model.exportData({
    fields: fieldsWithCorrectLabels,
    doNotAppendRowIdColumn: true,
    useAPINamesForHeaders: false
    });
(Edited)
Photo of Mark DeSimone

Mark DeSimone, Official Rep

  • 11,050 Points 10k badge 2x thumb
Jack and Youri, thank you both for taking the time to highlight this use case and your solution.  Your contributions to the community don't go unnoticed!