Welcome back to Learn Tech Tips Channel
This Topic today I will share with you how to easy integrated Select2 Library into CAKEPHP
First we need to know what is select2. Select2 is a jquery library gives you a customizable select box with support for searching, tagging, remote data sets, infinite scrolling, and many other highly used options.
On Our Developer. This library is easy to use, if you working with combobox I suggest you use this, because this easy to use and you will don't waste many time to do it.
The main feature on Select2 include Remote data support: Using Ajax, so you can effeciently search large lists of items
The second feature is Fully skinnable, CSS built with Sass and an optioanl theme for Bootstrap 4
The third feature is Fully extensible: This plugin system allow you to easily customize Select2 to work exactly how you want it to
The fourth feature is Dynamic item creation, this feature allow users to type in a new iption and add it on the fly.
The fifth featue is full browser support, support for both modern and legacy browsers is built-in, even including internet explorer 11
Another feature please look for the main page of select2.
Ok, Back to this topic, I will show you how to integrated Select2 into cakephp. Let's go
Please leave me one like/share on Tiktok and facebook for support channel. Many thanks
Explore My Other Channel for More Cool and Valuable Insights
π Youtube Learn Tech Tipsπ Tiktok
π Facebook:When integrated SELECT2, you need 3 simple steps:
Step 1: Add Css, Javascript on SELECT2 into your projects:
<script src="https://cdn.jsdelivr.net/npm/select2@4.1.0-rc.0/dist/js/select2.min.js"></script>
<link href="https://cdn.jsdelivr.net/npm/select2@4.1.0-rc.0/dist/css/select2.min.css" rel="stylesheet" />
Step 2: Add below code: with id="select-branch"
-> please be note we will use this id for init SELECT2
<select id="select-branch" class="form-control select-branch" name="branch_id">
<option value=""> <?= __('please select') ?> </option>
<?php if (isset($branches) && !empty($branches)) { ?>
<?php foreach ($branches as $key => $value) { ?>
<option value="<?= $value['id'] ?>" > <?= $value['name'] ?> </option>
<?php } ?>
<?php } ?>
</select>
Step 3: init SELECT2 function
<script>
$(document).ready(function() {
// integrated combobox with select and filter
$('#select-branch').select2({
placeholder: "Car Name",
allowClear: true
});
});
</script>
Here is the full source code.
<!-- Author: huuvi168@gmail.com - Zidane -->
<div class="row">
<div class="col-xs-12">
<div class="box box-primary">
<div class="box-header">
<h3 class="box-title"><?php echo __('Integrated Select2 Jquery'); ?></h3>
</div>
<div class="box-body">
<?php echo $this->Form->create('Demo', array('role' => 'form')); ?>
<fieldset>
<select id="select-branch" class="form-control select-branch" name="branch_id">
<option value=""> <?= __('please select') ?> </option>
<?php if (isset($branches) && !empty($branches)) { ?>
<?php foreach ($branches as $key => $value) { ?>
<option value="<?= $value['id'] ?>" > <?= $value['name'] ?> </option>
<?php } ?>
<?php } ?>
</select>
<h1 class="blue"> THANKS FOR WATCHING, LEARN TECH TIPS - ZIDANE</h1>
<a href="#"> https://learn-tech-tips.blogspot.com </a>
</fieldset>
<?php echo $this->Form->end(); ?>
</div>
</div>
</div>
</div>
});
<script>
$(document).ready(function() {
// integrated combobox with select and filter
$('#select-branch').select2({
placeholder: "Car Name",
allowClear: true
});
});
</script>
Is it easily way to integrated select2? do you think so, If you have another way leave your comment we can discuss about it too !!!.
Here is the live demo, you can reference here if you get any issue when integrated!
@learntechtips How to easily use SELECT2 into CAKEPHP #cakephp #php #sourcecode #learntechtips #learnontiktok #html #jquery ♬ Sensitivity - ζ‘δΉ
Any question or feecback about how to integrated select2 into cakephp, leave tour comment on below topic so we can discus about it
Learn Tech Tips - Zidane
View more Cakephp tech tips and trick here