Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Select2 jquery help

Posted on 2016-10-25
9
Medium Priority
?
130 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
Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

 
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 34

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 34

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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

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…
In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
The viewer will learn how to dynamically set the form action using jQuery.
The is a quite short video tutorial. In this video, I'm going to show you how to create self-host WordPress blog with free hosting service.

610 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