Start Building Professional
Web Apps Today


 
Categories Question details Back To List
Question  posted by C. Bernstein on Apr 20, 2009 11:27
open dhtmlx forum
ComboBox getOptionByLabel() throws Exception on special characters

My data string has some special characters in it (&, <, etc.) and getOptionByLabel throws an Exception.  I tried escaping the character as well as putting in the html syntax for them but it still threw an exception.  If wanting to use an "&" what syntax should be sent to getOptionByLabel() so that it doesn't throw an exception?

Answer posted by Alex (support) on Apr 21, 2009 03:08

Hello, 

Possibly the following example will help. 

if xml is

...

<option value="test"><![CDATA[some&amp;text]]></option>

...

The option can be got by:

var option = combo.getOptionByLabel("some&amp;text");

Combo shows "some&text" as option text on the page.

Answer posted on Apr 21, 2009 06:24

The xml is

...

<option value="28">Z&AA&123-&ZZ</option>

...

and the code to get the option is

var option = combo.getOptionByLabel("Z&amp;AA&amp;123-&amp;ZZ");

I have also tried

var option = combo.getOptionByLabel("Z&AA&123-&ZZ");

and this throws an exception as well.

Answer posted by Alex (support) on Apr 21, 2009 07:03
Please, take a look at the attached sample. Possibly it'll help to resolve the issue
Attachments (1)
Answer posted on Apr 21, 2009 07:17
Nothing shows up on the html page from the xml initialization...
Answer posted on Apr 21, 2009 07:28
I don't understand the "!CDATA" string.  Is it just a random string value or is this supposed to be attached to strings with special characters?
Answer posted on Apr 21, 2009 10:22

I changed my xml to look like the following, but I am still getting an exception when calling combo.getOptionByLabel("Z&amp;AA&amp;123-&amp;ZZ");

<row id="112">
  <cell>ART889</cell>
  <cell>ARTEMIS1afdsfa</cell>
  <cell>TO BE ENTERED LATER1333</cell>
  <cell><![CDATA[Z&amp;AA&amp;123-&amp;ZZ]]></cell>
  <cell>TBD</cell>
  <cell>BWI</cell>
  <cell>2009-02-26</cell>
  <cell>2009-12-31</cell>
  <cell>2009-04-20 14:04:34</cell>
  <cell>BERNSCH</cell>
  <cell>ACTIVE</cell>
  <cell>1</cell>
  <cell>Airborne Systems</cell>
  <cell>NONE</cell>
  <cell>0</cell>
  <cell>0</cell>
  </row>

What is wrong with the above?

Answer posted on Apr 21, 2009 10:25
Is there a way to print out all of the options of the combo box?
Answer posted by Alex (support) on Apr 22, 2009 06:27

>> What is wrong with the above?

If the 4th column has "combo" type, you can use getOptionByLabel method as follows:

grid.init();

var combo.getColumnCombo(3);

combo.getOptionByLabel("Z&amp;AA&amp;123-&amp;ZZ");

Locally it works correctly.

If issue still persists, please provide the sample to support@dhtmlx.com (combo excell is available only in PRO version)

>> Is there a way to print out all of the options of the combo box?

Unfortunately there is not public method to get all options of combo.

But you can use the internal property - optionsArr array:

var text = [];

for(var i=0; i<combo.optionsArr.length; i++)
  text[i] = combo.optionsArr[i].text;
}

Answer posted on Apr 22, 2009 12:08

combo.getOptionByLabel("Z&amp;AA&amp;123-&amp;ZZ"); DOES NOT WORK but

combo.getOptionByLabel("Z&AA&123-&ZZ"); DOES work.  Why does it have to be stripped of the html syntax even though the grid contains the html syntax?

Answer posted by Alex (support) on Apr 23, 2009 05:46

Hello,

Which combo version do you use ?

Please, try to use the latest combo (it can be downloaded from our website).

With it combo.getOptionByLabel("Z&amp;AA&amp;123-&amp;ZZ"); works.

Answer posted on Apr 23, 2009 06:04
We are using

dhtmlxCombo v.1.3 Standard edition build 81009

Answer posted by Alex (support) on Apr 23, 2009 07:02
The version, that you use, is not the latest. The latest one is 9026 and it is available on our website.
Answer posted on Apr 23, 2009 07:51
Why does the xml need the CDATA encoding?  &amp; is valid xml?
Answer posted by Alex (support) on Apr 23, 2009 08:10
Yes, &amp; gives valid xml. So, using CDATA is not necessary. 
Answer posted on Apr 23, 2009 08:59

If we don't use the CDATA syntax, it doesn't work.

We are using grid.xml:

<?xml version="1.0" encoding="UTF-8" ?>

- <complete>
  <option value="27">A&B&C&D</option>
  <option value="10">newCust</option>
</complete>
 
custComboBox = new dhtmlXCombo("CustCombo", "CustCombo", 200);
      custComboBox.readonly(true);
      custComboBox.loadXML("grid.xml");
 
The option on the combo box with the ampersands does not show up normally on the combo box screen.  It gets truncated.
 
 
Attachments (1)
Answer posted by Alex (support) on Apr 23, 2009 09:12

Please, check attached sample - all is correct here.

Attachments (1)
Answer posted on Apr 23, 2009 09:18
When I view the sample the drop down is not correct
Answer posted by Alex (support) on Apr 23, 2009 09:27

Sorry for teh misleading information.

Actually, yes, the &d is not shown. 

Possibly IE tries to read &D as some special symbol.

So, please, use CDATA to place the correct html into the combo:

A&amp;B&amp;C&amp;D

If you don't use it,  A&B&C&D  is placed into the combo.


Answer posted on Apr 23, 2009 10:16
I don't think it's a problem with IE.  When displaying the same information in the dhtmlxgrid component, I was getting the same behavior until I specified setColType("rotxt") to account for the special character.  This fixed the string so that it displayed correctly.  Is there something similar to specify the types of the string values in the combo box? 
Answer posted on Apr 23, 2009 10:23

Also,

<cell><![CDATA[A&B&C&D]]></cell>

in the xml file doesn't work which it should

\\emportalt\Website\wlibs\cots\dhtmlx\dhtmlxGrid\doc\articles\Handling_special_characters.html

Answer posted by Alex (support) on Apr 24, 2009 04:33

Hello,

Combo gets the option data as innerHTML. So, please, use:

Please, you<cell><![CDATA[A&amp;B&amp;C&amp;D]]></cell>

rotxt is a pure text editor (without html support), but combo doesn't provide such a mode. 

Answer posted on Apr 24, 2009 05:05

Could you please direct me to documentation that lists and explains the column types (ro, rotxt, etc.)?  Before I was using "ro" for the text in the grid with the ampersand and it was giving the behavior I have been describing to you, where it wasn't handling the ampersand as it should.  So, I found documentation describing how to handle special characters (in your documentation, Handling_special_characters.html) and it mentioned using rotxt.  I changed the type in the grid but I don't understand why the "rotxt", which is supposed to be for pure text, worked to parse a string with html syntax.  It would seem that the "ro" type should have worked since there are special characters, and it's not pure text.

It's a big deal for us to use CDATA because then we will have to start doing xslt transforms, and plus, the xml will not be in correct syntax.  So, we really don't believe we should have to do this.  Can you please explain to me why it doesn't work with valid xml?  Is there some sort of issue with the code?

Answer posted by Alex (support) on Apr 24, 2009 06:39

The information about grid cell types is http://dhtmlx.com/docs/products/dhtmlxGrid/doc/excell_types.html

The same in the grid package is dhtmlxGrid/doc/excell_types.html.

>> So, we really don't believe we should have to do this. Can you please explain to me why it doesn't work with valid xml? Is there some sort of issue with the code?

Please, try to point your browser to the xml that you try to load into the combo. 

In case of <option...>A&amp;B</option> the browser will show <option...>A&B</option>

A&B is not valid html. This value the combo receives and puts as innerHTML.

In case of CDATA <option...><![CDATA[A&amp;B]]></option>  the browser will show <option...>A&amp;B</option>. A&amp;B is valid html.

If you don't want to use CDATA, you can try double escaping: <option...>A&amp;amp;B</option>

Possibly it'll help. 

Answer posted on Apr 24, 2009 12:15
Why does the combo box get the xml as we see it in the browser and not as we see it when we view the source of the html page?
Answer posted by Alex (support) on Apr 25, 2009 00:01

Combo gets data from browser. And these are no ways to change this behavior.