Link to home
Start Free TrialLog in
Avatar of Aleks
AleksFlag for United States of America

asked on

Select2 jquery help

I have a form element (menu). When adding it works fine, but in my update page I am trying to display the selections made which is a value saved in my database, with comma separated values.
This is my code:

<select class="js-example-basic-multiple" multiple="multiple" name="countries_traveled" id="countries_traveled" >
  <%
While (NOT rs_counrties.EOF)
%>
  <option value="<%=(rs_counrties.Fields.Item("country").Value)%>" <%If (Not isNull((travel_history.Fields.Item("countries_traveled").Value))) Then If (CStr(rs_counrties.Fields.Item("country").Value) = CStr((travel_history.Fields.Item("countries_traveled").Value))) Then Response.Write("selected=""selected""") : Response.Write("")%> ><%=(rs_counrties.Fields.Item("country").Value)%></option>
  <%
  rs_counrties.MoveNext()
Wend
If (rs_counrties.CursorType > 0) Then
  rs_counrties.MoveFirst
Else
  rs_counrties.Requery
End If
%>
</select>

Open in new window


The problem is that the values don't show, if there is more than one selection made. If there is only one then it shows fine.
The database value is:  Andorra, Angola, Anguilla

Which are 3 values selected from the menu.

I think I may need to split the values into rows. I have a split function (below) that might help, just not sure how to use it.

ALTER function [dbo].[Split] (@list nvarchar(max), @delim nchar(1) = ',')

returns table as

return
   with csvtbl(start, [stop]) as (
     select start = convert(bigint, 1), [stop] = charindex(@delim collate slovenian_bin2, @list + @delim)
     union all
     select start = [stop] + 1, [stop] = charindex(@delim collate slovenian_bin2, @list + @delim, [stop] + 1)
     from   csvtbl
     where  [stop] > 0
  )
  select ltrim(rtrim(substring(@list, start, case when [stop] > 0 then [stop] - start else 0 end))) as value
  from   csvtbl
  where  ([stop] > 0)
  and (ltrim(rtrim(substring(@list, start, case when [stop] > 0 then [stop] - start else 0 end))) <> '')

Open in new window

Avatar of Ryan Chong
Ryan Chong
Flag of Singapore image

The database value is:  Andorra, Angola, Anguilla
in which field it contains such values? is it in travel_history.Fields.Item("countries_traveled").Value ?

I guess you probably would need a small function to split and compare the country values with the dropdown items.

this part of code probably need to be updated:
If (CStr(rs_counrties.Fields.Item("country").Value) = CStr((travel_history.Fields.Item("countries_traveled").Value))) Then

Open in new window

SOLUTION
Avatar of Aleks
Aleks
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Aleks

ASKER

I also tried:

SELECT  Split(countries_traveled, ',') AS countries
FROM history_travel

Error:

Msg 195, Level 15, State 10, Line 1
'Split' is not a recognized built-in function name.

Also tried:

SELECT value FROM dbo.Split(countries_traveled, ',')) FROM history_travel

Error:

Msg 321, Level 15, State 1, Line 5
"countries_traveled" is not a recognized table hints option. If it is intended as a parameter to a table-valued function or to the CHANGETABLE function, ensure that your database compatibility mode is set to 90.
i guess you can simply to do the comparison at server side scripting instead but not at backend database.

for a quick fix, you can try this:
<select class="js-example-basic-multiple" multiple="multiple" name="countries_traveled" id="countries_traveled" >
  <%
While (NOT rs_counrties.EOF)
%>
  <option value="<%=(rs_counrties.Fields.Item("country").Value)%>" <%If (Not isNull((travel_history.Fields.Item("countries_traveled").Value))) Then If compareSplit(CStr(rs_counrties.Fields.Item("country").Value), CStr((travel_history.Fields.Item("countries_traveled").Value))) Then Response.Write("selected=""selected""") : Response.Write("")%> ><%=(rs_counrties.Fields.Item("country").Value)%></option>
  <%
  rs_counrties.MoveNext()
Wend
If (rs_counrties.CursorType > 0) Then
  rs_counrties.MoveFirst
Else
  rs_counrties.Requery
End If
%>
</select>

<%

function compareSplit(ArrStr, v)
	Arr = Split(ArrStr, ",")
	compareSplit = false
	for i = 0 to ubound(Arr)
		if trim(ucase(Arr(i))) = ucase(v) then
			compareSplit = true
			exit for
		end if
	next
end function


%>

Open in new window

Avatar of Aleks

ASKER

Same result. It still shows them separated by comma and no selections in the menu
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Aleks

ASKER

This was put in the back burner, we will get back to it next year. I will repost then.