Trying to create custom "titleText" for Print template

1601
5
Jump to solution
11-08-2013 07:32 AM
by Anonymous User
Not applicable
I'm trying a custom print title, so that the user can enter a specific map title. I'm able to create the prompt but I get an error saying that the mapTitle is not able to be defined. Any ideas?

function createPrintDijit() {
var printTemplates = [
  {
   "name": "Landscape",
   "label": "Landscape",
   "format": "pdf",
   "layout": "Landscape",
   "exportOptions": {
    dpi : 300,
   },
   "layoutOptions": {
    "legendLayers": [],
    "titleText": mapTitle,
    "authorText": "GIS Division",
    "copyrightText": "Murray City",
    "scalebarUnit": "Miles"
            }
  },
  {
   "name": "Portrait",
   "label": "Portrait",
   "format": "pdf",
   "layout": "Portrait",
   "exportOptions": {
    dpi : 300,
   },
   "layoutOptions": {
    "legendLayers": [],
    "titleText": mapTitle,
    "authorText": "GIS Division",
    "copyrightText": "Murray City",
    "scalebarUnit": "Miles"
            }
  },
]


  var templates = [];
        dojo.forEach(printTemplates, function(lo) {
          var t = new esri.tasks.PrintTemplate();
          t.printTemplate = lo.name;  
          t.label = lo.label;
          t.format = lo.format;
          t.layout = lo.layout;
          t.exportOptions = {"dpi":"300"};
          t.layoutOptions = {"titleText": mapTitle,}   
          templates.push(t);
        });


var printer = new esri.dijit.Print(
  { 
    map: map,
    templates: templates,
    url: "http://gisfile:6080/arcgis/rest/services/Printing/MurrayPrinting/GPServer/Export%20Web%20Map",
    "content": {
              "f": "json"
            }
  },
    dojo.byId("printButton")
   
  );
 
  dojo.connect(printer, "onPrintStart", function(){
  mapTitle = prompt ("Enter the Map title");
                            this.templates[1].layoutOptions.titleText = mapTitle;
 
      });   
  printer.startup();
 


}
0 Kudos
1 Solution

Accepted Solutions
JasonZou
Occasional Contributor III
Ben,

In your code, you only change the titleText of the second template, not all of them.

this.templates[  1].layoutOptions.titleText = mapTitle;


Change it to:

array.forEach(this.templates, function(aTemplate) {
    aTemplate.layoutOptions.titleText = mapTitle;
});


Not sure how you implement the print GUI. In my app, I use a <select> element to host all the template options, so the user can select which template they like. Then after prompt for the map title, you will know which template's titleText to change instead of changing for all templates.

View solution in original post

0 Kudos
5 Replies
JasonZou
Occasional Contributor III
Try to make the below changes.

If mapTitle is not declared, declare it and give its initial value right below function createPrintDijit() {:
var mapTitle = "Default Title";


Change:
t.layoutOptions = {"titleText": mapTitle,} 


To:
t.layoutOptions = {"titleText": mapTitle} 
0 Kudos
by Anonymous User
Not applicable
Thanks for the reply,
Removing the comma, fixes it so that I don't get the error of mapTitle not defined. But it doesn't change the title based on the input variable, the title stays the same as to what it was defined when the createprintdigit was created. It seems like you need to defined the variable of the map title before the print button or createprintdigit is even created?
0 Kudos
JasonZou
Occasional Contributor III
Ben,

In your code, you only change the titleText of the second template, not all of them.

this.templates[  1].layoutOptions.titleText = mapTitle;


Change it to:

array.forEach(this.templates, function(aTemplate) {
    aTemplate.layoutOptions.titleText = mapTitle;
});


Not sure how you implement the print GUI. In my app, I use a <select> element to host all the template options, so the user can select which template they like. Then after prompt for the map title, you will know which template's titleText to change instead of changing for all templates.
0 Kudos
by Anonymous User
Not applicable
Jason
That's it! I changed the code to:

var i;
for (i = 0; i < this.templates.length; i++) {
this.templates.layoutOptions.titleText = mapTitle;
}

And now its working, Thank you for your help!!!!!
0 Kudos
JasonZou
Occasional Contributor III
Glad to help, Ben:) Please consider to mark the thread as Answered so other people may find it helpful. Thanks.
0 Kudos