eSearch substring fields

1801
3
Jump to solution
02-09-2016 10:47 AM
RudoDuncan
New Contributor II

Robert Scheitlin, GISP

Kevin MacLeod

I found this response to one of the questions about substrings and replacing/truncating a specific number of strings. My question is, where do you put the code? Does it go with the config.json file? Thanks

Substring fix.JPG

0 Kudos
1 Solution

Accepted Solutions
RobertScheitlin__GISP
MVP Emeritus

Rudo,

   Here is the updated function that should do what you are wanting. I am assuming that the field is a string based on the field content examples you have given. So in the Widget.js replace the existing _substitute function with this one.

_substitute: function (string, Attribs, currentLayer) {
        var lfields = this._getFieldsfromLink(string);
        for (var lf = 0; lf < lfields.length; lf++) {
          if (Attribs[lfields[lf]]) {
            var fld = this._getField(currentLayer, lfields[lf]);
            if (fld.type === "esriFieldTypeString") {
              if(lang.trim(lfields[lf]) === 'IncDate'){
                var sYear = lang.trim(Attribs[lfields[lf]]).substring(0, 4);
                string = string.replace(new RegExp('{' + lang.trim(lfields[lf]) + '}', 'g'), sYear);
              }else{
                string = string.replace(new RegExp('{' + lang.trim(lfields[lf]) + '}', 'g'), lang.trim(Attribs[lfields[lf]]));
              }
            } else {
              string = string.replace(new RegExp('{' + lang.trim(lfields[lf]) + '}', 'g'), Attribs[lfields[lf]]);
            }
          }
        }
        return string;
      },

View solution in original post

3 Replies
RudoDuncan
New Contributor II

My link is looking at a {incdate} parameter within the url to open a report for different years. The {incdate} field is displayed like 20160203, I need the first 4 characters to get the correct year but get and error because it's gathering the whole string in the {incdate} field. Will this code allow me to do that?

0 Kudos
RobertScheitlin__GISP
MVP Emeritus

Rudo,

   Here is the updated function that should do what you are wanting. I am assuming that the field is a string based on the field content examples you have given. So in the Widget.js replace the existing _substitute function with this one.

_substitute: function (string, Attribs, currentLayer) {
        var lfields = this._getFieldsfromLink(string);
        for (var lf = 0; lf < lfields.length; lf++) {
          if (Attribs[lfields[lf]]) {
            var fld = this._getField(currentLayer, lfields[lf]);
            if (fld.type === "esriFieldTypeString") {
              if(lang.trim(lfields[lf]) === 'IncDate'){
                var sYear = lang.trim(Attribs[lfields[lf]]).substring(0, 4);
                string = string.replace(new RegExp('{' + lang.trim(lfields[lf]) + '}', 'g'), sYear);
              }else{
                string = string.replace(new RegExp('{' + lang.trim(lfields[lf]) + '}', 'g'), lang.trim(Attribs[lfields[lf]]));
              }
            } else {
              string = string.replace(new RegExp('{' + lang.trim(lfields[lf]) + '}', 'g'), Attribs[lfields[lf]]);
            }
          }
        }
        return string;
      },