UNPKG

magicsuggest

Version:

MagicSuggest is a multiple selection auto-suggest input box for Bootstrap 3.

188 lines (169 loc) 9.49 kB
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1"> <meta name="description" content="MagicSuggest is a multiple selection combo box built for bootstrap themes"> <meta name="author" content="Nicolas Bize"> <link rel="shortcut icon" href="http://nicolasbize.com/magicsuggest/assets/ico/favicon.ico"> <title>MagicSuggest - Tutorial</title> <link href="lib/bootstrap/css/bootstrap.min.css" rel="stylesheet"> <link href="assets/css/custom.css" rel="stylesheet"> <link href="assets/lib/highlightjs/styles/default.css" rel="stylesheet"> <script type="text/javascript" src="assets/lib/highlightjs/highlight.pack.js"></script> <script>hljs.initHighlightingOnLoad();</script> <!-- HTML5 shim and Respond.js IE8 support of HTML5 elements and media queries --> <!--[if lt IE 9]> <script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script> <script src="https://oss.maxcdn.com/libs/respond.js/1.4.2/respond.min.js"></script> <![endif]--> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script> <script src="lib/bootstrap/js/bootstrap.min.js"></script> </head> <body> <a id="forkme_banner" href="https://github.com/nicolasbize/magicsuggest">View on GitHub</a> <div class="site-wrapper"> <div class="site-wrapper-inner"> <div class="cover-container"> <div class="masthead clearfix"> <div class="inner"> <h3 class="masthead-brand">MagicSuggest</h3> <ul class="nav masthead-nav"> <li><a href="index.html">Home</a></li> <li><a href="examples.html">Examples</a></li> <li><a href="doc.html">Documentation</a></li> <li class="active"><a href="tutorial.html">Tutorial</a></li> </ul> </div> </div> </div> <div class="tutorial"> <div class="container"> <h1>Tutorial Part Five</h1> <p>Setting initial values and remote functions.</p> <div class="row"> <div class="col-md-12"> <h3>Initialize with values</h3> <p>A common scenario is to have some default or initial value set for the combo box. This is done by using the <code>value</code> property. This property can either be set directly in the original <code>input</code> tag, or it can be set within our <code>js/script.js</code> file during initialization. As the combo is currently set to recognize country codes as IDs, we set those in our index.php file:</p> <h3><code>index.php</code></h3> <pre><code>&lt;!DOCTYPE html&gt; &lt;html lang=&quot;en&quot;&gt; &lt;head&gt; &lt;meta http-equiv=&quot;Content-Type&quot; content=&quot;text/html; charset=ISO-8859-1&quot; /&gt; &lt;meta http-equiv=&quot;X-UA-Compatible&quot; content=&quot;IE=edge&quot;&gt; &lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1&quot;&gt; &lt;title&gt;MagicSuggest Tutorial&lt;/title&gt; &lt;link href=&quot;lib/bootstrap/css/bootstrap.min.css&quot; rel=&quot;stylesheet&quot;&gt; &lt;link href=&quot;lib/magicsuggest/magicsuggest.css&quot; rel=&quot;stylesheet&quot;&gt; &lt;link rel=&quot;stylesheet&quot; type=&quot;text/css&quot; href=&quot;css/style.css&quot;&gt; &lt;/head&gt; &lt;body&gt; &lt;div class=&quot;container&quot;&gt; &lt;h1&gt;Hello, world!&lt;/h1&gt; &lt;form action=&quot;subscribe.php&quot; method=&quot;post&quot;&gt; &lt;div class=&quot;form-group&quot;&gt; &lt;label&gt;First name&lt;/label&gt; &lt;input class=&quot;form-control&quot; name=&quot;firstname&quot;/&gt; &lt;/div&gt; &lt;div class=&quot;form-group&quot;&gt; &lt;label&gt;Last name&lt;/label&gt; &lt;input class=&quot;form-control&quot; name=&quot;lastname&quot;/&gt; &lt;/div&gt; &lt;div class=&quot;form-group&quot;&gt; &lt;label&gt;Countries&lt;/label&gt; &lt;input id=&quot;ms&quot; class=&quot;form-control&quot; name=&quot;countries[]&quot; value=&quot;[3,5,6,7,39]&quot;/&gt; &lt;/div&gt; &lt;input type=&quot;submit&quot; class=&quot;btn btn-success pull-right&quot; value=&quot;Submit&quot;/&gt; &lt;/form&gt; &lt;/div&gt; &lt;!-- jQuery (necessary for Bootstrap's JavaScript plugins) --&gt; &lt;script src=&quot;lib/jquery/jquery-1.11.1.min.js&quot;&gt;&lt;/script&gt; &lt;!-- Include all compiled plugins (below), or include individual files as needed --&gt; &lt;script src=&quot;lib/bootstrap/js/bootstrap.min.js&quot;&gt;&lt;/script&gt; &lt;script src=&quot;lib/magicsuggest/magicsuggest.js&quot;&gt;&lt;/script&gt; &lt;script src=&quot;js/script.js&quot;&gt;&lt;/script&gt; &lt;/body&gt; &lt;/html&gt;</code></pre> <p>Something interesting to note here is that when we are using an AJAX source to fetch the combo's results, the component is required to make a request to retrieve the records we ask it for initialization. This is only done when an inital value is set.</p> <h3><code>js/script.js</code></h3> <p>We make a small change to make it look nicer and display selections as a comma separated value string. Also, we are now performing our sorting server-side in which we only want to look through country names.</p> <pre><code>$(function() { var ms = $('#ms').magicSuggest({ data: 'get_countries.php', valueField: 'idCountry', displayField: 'countryName', groupBy: 'continentName', mode: 'remote', renderer: function(data){ return '&lt;div class=&quot;country&quot;&gt;' + '&lt;img src=&quot;img/flags/' + data.countryCode.toLowerCase() + '.png&quot; /&gt;' + '&lt;div class=&quot;name&quot;&gt;' + data.countryName + '&lt;/div&gt;' + '&lt;div style=&quot;clear:both;&quot;&gt;&lt;/div&gt;' + '&lt;div class=&quot;prop&quot;&gt;' + '&lt;div class=&quot;lbl&quot;&gt;Population : &lt;/div&gt;' + '&lt;div class=&quot;val&quot;&gt;' + data.population + '&lt;/div&gt;' + '&lt;/div&gt;' + '&lt;div class=&quot;prop&quot;&gt;' + '&lt;div class=&quot;lbl&quot;&gt;Capital : &lt;/div&gt;' + '&lt;div class=&quot;val&quot;&gt;' + data.capital + '&lt;/div&gt;' + '&lt;/div&gt;' + '&lt;div style=&quot;clear:both;&quot;&gt;&lt;/div&gt;' + '&lt;/div&gt;'; }, resultAsString: true, selectionRenderer: function(data){ return '&lt;img src=&quot;img/flags/' + data.countryCode.toLowerCase() + '.png&quot; /&gt;' + '&lt;div class=&quot;name&quot;&gt;' + data.countryName + '&lt;/div&gt;'; } }); });</code></pre> <h3><code>get_countries.php</code></h3> <p>Every time the user types, a query parameter is sent to the server. The frequency of those sent messages is set with the <code>typeDelay</code> parameter. Also <code>method</code> is default to <code>POST</code> but that can be changed as well. See how we use the parameter to build our custom query and get exactly what we want. Now if you type <code>par</code> it should only return <code>Paraguy</code> and not France who had Paris as Capital.</p> <pre><code>&lt;?php $mysql = new mysqli('localhost','root','root','magicsuggest', 8889); $q = isset($_POST['query']) ? $mysql-&gt;real_escape_string($_POST['query']) : ''; $result = $mysql-&gt;query(&quot;select * from countries where countryName like '%&quot; . $q .&quot;%'&quot;); $rows = array(); while($row = $result-&gt;fetch_array(MYSQL_ASSOC)) { $rows[] = array_map(&quot;utf8_encode&quot;, $row); } echo json_encode($rows); $result-&gt;close(); $mysql-&gt;close(); ?&gt; </code></pre> <div class="well"> <h4>Wrapping up...</h4> <a href="tutorial/5.html">View the results</a><br/> <a href="tutorial/5/5.zip">Download the files for the tutorial</a><br/> <br/> <p>That's all folks!</p> </div> </div> </div> </div> </div> <div style="height:200px"></div> </div> <script type="text/javascript"> var _gaq = _gaq || []; _gaq.push(['_setAccount', 'UA-5470193-5']); _gaq.push(['_trackPageview']); (function() { var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true; ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js'; var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s); })(); </script> </body> </html>