UI5 – Translating the ValueHelpRequest into filters

Home / Blog / UI5 – Translating the ValueHelpRequest into filters

I’ve been talking a lot about this lately, because quite frankly, I feel like I fought really hard to figure this out and I couldn’t find anything online to really help me out.  So today, I’m gonna talk about converting the valuehelprequest output into something I could use to create filters.  A little background first.  I have a settings page that has a bunch of possible filters for my master/detail list.  I pull my list, then I can apply these filters.  Well, up till now, everything has been a straight single value text or date input.  Now, suddenly with this new trick, I could have multiple values, and I could have exclusions, or other operations.  The thing that sucks, is that these do NOT seem to automatically translate into something the filter could read.  So I needed to spend some time to see what all the options were.  At the end of the day, here’s what I came up with:

I’ll talk a little about what’s going on, then you can read the code for yourself.  I played with the help request and I found 10 distinct scenarios that I needed to account for.  Then to either recreate a token (I’ll talk about this challenge tomorrow) or to apply it to the filter, I needed 4 additional values that I couldn’t extact from the token.

First off, the token provides you to pieces of data (at least that you can easily get to).  Key & Text.  Now, the interesting thing is that in most of the operations, Text contains all of the details.  Let me show what each thing looks like:

  1. Equal to a value has 2 options:
    1. key: 1000, text: is just a description
    2. key: range_X, text: =1000 (this means that the value is 1000)
  2. Next, the exclusion.  Key: range_X, Text: !(=1000)
  3. Less than: Key: range_x, Text: <1000
  4. Less than equal to: Key: range_x, Text: <=1000
  5. Greater than: Key: range_x, Text: >1000
  6. Greater than equal to: Key: range_x, Text: >=1000
  7. Starts with: Key: range_x, Text: 1000*
  8. Ends with: Key: range_x, Text: *1000
  9. Contains: Key: range_x, Text: *1000*
  10. Between: Key: range_x, Text: 0000…1000

The following code is just using some string comparisons to figure out which case, then get the values needed for filtering & token recreation. (I apologize for the formatting, but you get the idea).  At the very end, I’m just returning a simple object with the list of fields I need to extract.  Otherwise, it’s just a bunch of imbedded if/else statements.

var iLen, iStr, iEnd, sOp, sVal1, sVal2, sEx;
iLen = sText.length;
// total of 10 scenarios
// if sKey contains range_ then need to check for operations
// 1. else, return rSign = EQ, sVal1 = sKey, sVal2 = “”
if (!sKey.match(“range_”)) {
sOp = “EQ”;
sEx = false;
sVal1 = sKey;
sVal2 = “”;
}

// next check for different cases
if (sKey.match(“range_”)) {
// 1. alternate method for EQ single value
if (sText.startsWith(“=”)) {
sOp = “EQ”;
sEx = false;
sVal1 = sText.slice(1, iEnd);
sVal2 = “”;
} else {
// 2. NE sText[1] = !
if (sText.startsWith(“!”)) {
iStr = 3;
iEnd = iLen – 1;
sOp = “NE”;
sEx = true;
sVal1 = sText.slice(iStr, iEnd);
sVal2 = “”;
} else {
// < or <=
// 3. sText[1] = < and sText[2] != “=” then LT
if (sText.startsWith(“<“)) {
iStr = 1;
iEnd = iLen;
sOp = “LT”;
sEx = false;
sVal1 = sText.slice(iStr, iEnd);
sVal2 = “”;
} else {
// 4. sText[1] = <, sText[2] = “=”, then LE
if (sText.startsWith(“<=”)) {
iStr = 2;
iEnd = iLen;
sOp = “LE”;
sEx = false;
sVal1 = sText.slice(iStr, iEnd);
sVal2 = “”;
} else {
// > or >=
// 5. sText[1] = > and sText[2] != “=” then GT
if (sText.startsWith(“>”)) {
iStr = 1;
iEnd = iLen;
sOp = “GT”;
sEx = false;
sVal1 = sText.slice(iStr, iEnd);
sVal2 = “”;
} else {
// 6. sText[1] = >, sText[2] = “=”, then GE
if (sText.startsWith(“>=”)) {
iStr = 2;
iEnd = iLen;
sOp = “GE”;
sEx = false;
sVal1 = sText.slice(iStr, iEnd);
sVal2 = “”;
} else {
// * or *X* or X}
// 7. sText[1] = *. then EndsWith
if (sText.startsWith(“*”)) {
iStr = 1;
iEnd = iLen;
sOp = “EndsWith”;
sEx = false;
sVal1 = sText.slice(iStr, iEnd);
sVal2 = “”;
} else {
// 8. sText[1] != *, if sText[length] = * then StartsWith
if (sText.endsWith(“*”)) {
iStr = 0;
iEnd = iLen – 1;
sOp = “StartsWith”;
sEx = false;
sVal1 = sText.slice(iStr, iEnd);
sVal2 = “”;
} else {
// 9. sText[1] = *. sText.endsWith(*)
if (sText.startsWith(“*”) && sText.endsWith(“*”)) {
iStr = 1;
iEnd = iLen – 1;
sOp = “Contains”;
sEx = false;
sVal1 = sText.slice(iStr, iEnd);
sVal2 = “”;
} else {
// 10. if “…” then between BT
// sVal1 = everything before …
// sVal2 = evertoToken after …
if (sText.match(“…”)) {
iStr = 0;
iEnd = sText.indexOf(“…”);
sOp = “BT”;
sEx = false;
sVal1 = sText.slice(iStr, iEnd);
iStr = sText.lastIndexOf(“…”) + 3;
iEnd = iLen;
sVal2 = sText.slice(iStr, iEnd);
}
}
}
}
}
}
}
}
}
}
}
var aTok = {
key: sKey,
text: sText,
op: sOp,
exclude: sEx,
val1: sVal1,
val2: sVal2
};
return aTok;

Thanks for reading,

As always, thanks for reading and don't forget to check out our SAP Service Management Products at my other company JaveLLin Solutions,
Mike

Leave a Reply

Your email address will not be published. Required fields are marked *