Solved

Select2 jquery help

Posted on 2016-10-25
9
94 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 50

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
The New “Normal” in Modern Enterprise Operations

DevOps for the modern enterprise offers many benefits — increased agility, productivity, and more, but digital transformation isn’t easy, especially if you’re not addressing the right issues. Register for the webinar to dive into the “new normal” for enterprise modern ops.

 
LVL 50

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 50

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 33

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 33

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

Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

Question has a verified solution.

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

Developer portfolios can be a bit of an enigma—how do you present yourself to employers without burying them in lines of code?  A modern portfolio is more than just work samples, it’s also a statement of how you work.
FAQ pages provide a simple way for you to supply and for customers to find answers to the most common questions about your company. Here are six reasons why your company website should have a FAQ page
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.
The viewer will get a basic understanding of what section 508 compliance can entail, learn about skip navigation links, alt text, transcripts, and font size controls.

809 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