Hi All,
I have seen Robert Scheitlin, GISP solution on below link and tried the same using WAB 2.15 but all the fields are getting downloaded in my csv.
Web App Builder - How to limit fields in "export to CSV" option in query widget?
I have tried below code:
var resultfields = [];
for (i=0; i< popupInfos.length-1; i++){
if (popupInfos[i].visible == true){
resultfields.push(popupInfos[i].fieldName)
}
}
var fFlds = array.filter(layer.fields, lang.hitch(this, function(fldInfo) {
return resultfields.indexOf(fldInfo.name) > 0;
;}));
var featureSet = new FeatureSet();
featureSet.fields = lang.clone(fFlds);
Please help.
Thanks,
Krish
Solved! Go to Solution.
Kirsh,
Sorry I was looking at 2.14. 2.15 has changed yet again how they handle CSV export.
In the exportUtils.js change this method (line 7):
exportCSV: function() {
return this.getFeatureSet().then(lang.hitch(this, function(fs){
var popupInfo = this.findPopupInfo(fs);
var layerDefinition = this.findLayerDefinition(fs);
var features = fs.features;
if (layerDefinition && layerDefinition.fields) {
//change here
var outFields = lang.clone(fs.fields); //layerDefinition.fields);
this._addXYAttribute(outFields, 'x');
this._addXYAttribute(outFields, 'y');
layerDefinition.fields = outFields;
features = [];
array.forEach(fs.features, lang.hitch(this, function(feature) {
var graphic = new Graphic(feature.toJson());
graphic.attributes = this._getAttrsWithXY(graphic);
features.push(graphic);
}));
}
return CSVUtils.exportCSVByGraphics(
this.filename,
layerDefinition,
features,
{
formatNumber: true,
formatDate: true,
formatCodedValue: true,
popupInfo: popupInfo
});
}));
},
Also here is an update to the SingleQueryResult.js > _getFeatureSet function (to insure it get the first and last fields that were being skipped previously).
_getFeatureSet: function () {
var layer = this.currentAttrs.query.resultLayer;
//get popup info for field alias
var popupInfos = null;
var popupInfoObj = this.currentAttrs.config && this.currentAttrs.config.popupInfo;
if (popupInfoObj) {
popupInfos = popupInfoObj.fieldInfos;
}
var resultfields = [];
for (i = 0; i < popupInfos.length; i++) {
if (popupInfos[i].visible == true) {
resultfields.push(popupInfos[i].fieldName);
}
}
var fFlds = array.filter(layer.fields, lang.hitch(this, function (fldInfo) {
return resultfields.indexOf(fldInfo.name) > -1;
}));
var featureSet = new FeatureSet();
featureSet.fields = lang.clone(fFlds);
featureSet.features = [].concat(layer.graphics);
featureSet.geometryType = layer.geometryType;
featureSet.fieldAliases = {};
array.forEach(featureSet.fields, lang.hitch(this, function (fieldInfo) {
var fieldName = fieldInfo.name;
var fieldAlias = this._getFieldAliasByPopupInfo(fieldInfo, popupInfos);
featureSet.fieldAliases[fieldName] = fieldAlias;
}));
console.info(featureSet);
return featureSet;
},
Krish,
Have you tried to console.log the fFlds to see if it is getting what you expect?
Yes. I'm getting expected fields only. But when clicked , on export to csv, i'm getting all the fields in downloaded csv file.
Thanks,
Krish
Krish,
That thread was back using WAB 2.4. What version are you using?
Krish,
So in version 2.15 (or earlier) they made a change in the jimu.js/exportUtils.js file that overrides the featureset.fields back to all the fields. to get past this you have to make a change to this file.
_generateFields: function(featureSet) {
var feature = featureSet.features[0];
var fields, item, layerId;
// if(feature._layer) {
// fields = feature._layer.fields;
// layerId = feature._layer.id;
// }
....
Notice I have commented out lines 5-8 these are the lines that override the filtered fields that was already provided.
I tried to find "_genereateFields" function in jimu.js/exportUtils.js file but unable to find it (WAB 2.15).
Do I have to create another function with this name?
I have tried by commenting below lines but still, it downloads all the fields:
findPopupInfo: function(featureSet) {
if (!featureSet || !featureSet.features || featureSet.features.length === 0) {
return null;
}
var feature = featureSet.features[0];
var layerId;
var fields;
/*
if(feature._layer) {
//fields = feature._layer.fields;
//layerId = feature._layer.id;
var layerInfos = LayerInfos.getInstanceSync();
var layerInfo = layerInfos.getLayerInfoById(layerId);
if (layerInfo) {
var popupInfo = layerInfo.getPopupInfo();
if (!popupInfo) {
// Try another way to get popupInfo
popupInfo = layerInfo.layerObject.infoTemplate && layerInfo.layerObject.infoTemplate.info;
}
return popupInfo;
}
}*/
return null;
},
findLayerDefinition: function(featureSet) {
if (!featureSet || !featureSet.features || featureSet.features.length === 0) {
return null;
}
var feature = featureSet.features[0];
// if(feature._layer) {
// return {
// geometryType: featureSet.geometryType,
// fields: feature._layer.fields
// };
// }
var fields = [];
var attributes = feature.attributes;
var item;
for (item in attributes) {
if(attributes.hasOwnProperty(item)){
fields.push({
name: item
});
}
}
return {
geometryType: featureSet.geometryType,
fields: fields
};
},
Thanks,
Krish
Kirsh,
Sorry I was looking at 2.14. 2.15 has changed yet again how they handle CSV export.
In the exportUtils.js change this method (line 7):
exportCSV: function() {
return this.getFeatureSet().then(lang.hitch(this, function(fs){
var popupInfo = this.findPopupInfo(fs);
var layerDefinition = this.findLayerDefinition(fs);
var features = fs.features;
if (layerDefinition && layerDefinition.fields) {
//change here
var outFields = lang.clone(fs.fields); //layerDefinition.fields);
this._addXYAttribute(outFields, 'x');
this._addXYAttribute(outFields, 'y');
layerDefinition.fields = outFields;
features = [];
array.forEach(fs.features, lang.hitch(this, function(feature) {
var graphic = new Graphic(feature.toJson());
graphic.attributes = this._getAttrsWithXY(graphic);
features.push(graphic);
}));
}
return CSVUtils.exportCSVByGraphics(
this.filename,
layerDefinition,
features,
{
formatNumber: true,
formatDate: true,
formatCodedValue: true,
popupInfo: popupInfo
});
}));
},
Also here is an update to the SingleQueryResult.js > _getFeatureSet function (to insure it get the first and last fields that were being skipped previously).
_getFeatureSet: function () {
var layer = this.currentAttrs.query.resultLayer;
//get popup info for field alias
var popupInfos = null;
var popupInfoObj = this.currentAttrs.config && this.currentAttrs.config.popupInfo;
if (popupInfoObj) {
popupInfos = popupInfoObj.fieldInfos;
}
var resultfields = [];
for (i = 0; i < popupInfos.length; i++) {
if (popupInfos[i].visible == true) {
resultfields.push(popupInfos[i].fieldName);
}
}
var fFlds = array.filter(layer.fields, lang.hitch(this, function (fldInfo) {
return resultfields.indexOf(fldInfo.name) > -1;
}));
var featureSet = new FeatureSet();
featureSet.fields = lang.clone(fFlds);
featureSet.features = [].concat(layer.graphics);
featureSet.geometryType = layer.geometryType;
featureSet.fieldAliases = {};
array.forEach(featureSet.fields, lang.hitch(this, function (fieldInfo) {
var fieldName = fieldInfo.name;
var fieldAlias = this._getFieldAliasByPopupInfo(fieldInfo, popupInfos);
featureSet.fieldAliases[fieldName] = fieldAlias;
}));
console.info(featureSet);
return featureSet;
},
Thanks Robert Scheitlin, GISP! It worked Perfectly.
I'm just getting extra columns x and y in csv.
Thanks,
Krish
That is by design