Solved

Select2 jquery help

Posted on 2016-10-25
9
41 Views
Last Modified: 2016-11-24
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

0
Comment
Question by:amucinobluedot
  • 4
  • 3
  • 2
9 Comments
 
LVL 49

Expert Comment

by:Ryan Chong
ID: 41859657
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

0
 

Assisted Solution

by:amucinobluedot
amucinobluedot earned 0 total points
ID: 41859658
Ryan: You are correct. I need to split the values. I tried the code below but didn't work, even though I have the function in my database I get an error.

code
SELECT  dbo.Split(countries_traveled, ',') AS countries
FROM history_travel

Open in new window


Error:

Msg 4121, Level 16, State 1, Line 3
Cannot find either column "dbo" or the user-defined function or aggregate "dbo.Split", or the name is ambiguous.
0
 

Author Comment

by:amucinobluedot
ID: 41859660
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.
0
 
LVL 49

Expert Comment

by:Ryan Chong
ID: 41859669
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

0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

Author Comment

by:amucinobluedot
ID: 41859670
Same result. It still shows them separated by comma and no selections in the menu
0
 
LVL 49

Assisted Solution

by:Ryan Chong
Ryan Chong earned 250 total points
ID: 41859672
can you give sample data of what was returned from:

1. CStr(rs_counrties.Fields.Item("country").Value)
2. CStr((travel_history.Fields.Item("countries_traveled").Value))

?
0
 
LVL 32

Accepted Solution

by:
Big Monty earned 250 total points
ID: 41860336
try this:

<select class="js-example-basic-multiple" multiple="multiple" name="countries_traveled" id="countries_traveled" >
  <%
While (NOT rs_counrties.EOF)
     country = rs_counrties.Fields.Item("country").Value
     countriesTraveled = travel_history.Fields.Item("countries_traveled").Value
     selected = ""

     if CStr( "" & country ) = CStr( "" & countriesTraveled ) then selected = " selected='selected'"
%>
  <option value="<%=country%>" <%=selected%> ><%=country%></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


sometimes it's better to break things up into smaller pieces, then build the core component (in this case the select box) with those pieces.

if this doesn't work, try removing the select2 class(es) from the markup. This will tell us if there is a potential initialization process that wipes out the value from select2
0
 
LVL 32

Assisted Solution

by:Big Monty
Big Monty earned 250 total points
ID: 41880629
any luck with this?
0
 

Author Closing Comment

by:amucinobluedot
ID: 41900937
This was put in the back burner, we will get back to it next year. I will repost then.
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Join & Write a Comment

JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now