?
Solved

Select2 jquery help

Posted on 2016-10-25
9
Medium Priority
?
117 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 2
9 Comments
 
LVL 53

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
Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

 
LVL 53

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
 

Author Comment

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

Assisted Solution

by:Ryan Chong
Ryan Chong earned 1000 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 33

Accepted Solution

by:
Big Monty earned 1000 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 33

Assisted Solution

by:Big Monty
Big Monty earned 1000 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

Containers & Docker to Create a Powerful Team

Containers are an incredibly powerful technology that can provide you and/or your engineering team with huge productivity gains. Using containers, you can deploy, back up, replicate, and move apps and their dependencies quickly and easily.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When the s#!t hits the fan, you don’t have time to look up who’s on call, draft emails, call collaborators, or send text messages. An instant chat window is definitely the way to go, especially one like HipChat. HipChat is a true business app. An…
Q&A with Course Creator, Mark Lassoff, on the importance of HTML5 in the career of a modern-day developer.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

762 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