Home > Web Front-end > JS Tutorial > body text

Detailed explanation of how to use JS component Bootstrap Select2_javascript skills

WBOY
Release: 2016-05-16 15:18:00
Original
2600 people have browsed it

When introducing the select component, I previously shared an article about the competition between the two major components of bootstrap multiselect in JS components . The functions of these two components are indeed very powerful. This article Share some usage and features of the select component.
Some general single-select, multi-select, grouping and other functions will not be introduced here. Multiselect is the strong point in this aspect. Let’s focus on some features and effects of select2:
1. Characteristic effects
1. Multiple selection effect


You can set up to a few selections

2. The effect of combining pictures and text


3. Remote search function (that is, dynamically fetching data from the background when the user enters search content)
Before entering content

Enter a space to search all

Slide the scroll bar to the bottom to automatically load the remaining items

Enter text to dynamically filter in the background

More advanced usage is like:

In fact, it is not difficult to use, it is just a process of putting together HTML.

2. Code examples
1. Multiple selection effect
Select2's multi-selection is very simple, just set an attribute.

<script src="~/Scripts/jquery-1.10.2.js"></script>

 <script src="~/Content/bootstrap/js/bootstrap.js"></script>
 <link href="~/Content/bootstrap/css/bootstrap.css" rel="stylesheet" />

 <script src="~/Content/select2-master/dist/js/select2.js"></script>
 <link href="~/Content/select2-master/dist/css/select2.css" rel="stylesheet" />


  <select id="sel_menu2" multiple="multiple" class="form-control">
   <optgroup label="系统设置">
    <option value="1">用户管理</option>
    <option value="2">角色管理</option>
    <option value="3">部门管理</option>
    <option value="4">菜单管理</option>
   </optgroup>
   <optgroup label="订单管理">
    <option value="5">订单查询</option>
    <option value="6">订单导入</option>
    <option value="7">订单删除</option>
    <option value="8">订单撤销</option>
   </optgroup>
   <optgroup label="基础数据">
    <option value="9">基础数据维护</option>
   </optgroup>
  </select>

 //多选
 $("#sel_menu2").select2({
  tags: true,
  maximumSelectionLength: 3 //最多能够选择的个数
 });

Copy after login

2. The effect of combining pictures and text

<select id="sel_menu" class="js-example-templating js-states form-control">
    <optgroup label="系统设置">
     <option value="1">用户管理</option>
     <option value="2">角色管理</option>
     <option value="3">部门管理</option>
     <option value="4">菜单管理</option>
    </optgroup>
    <optgroup label="订单管理">
     <option value="5">订单查询</option>
     <option value="6">订单导入</option>
     <option value="7">订单删除</option>
     <option value="8">订单撤销</option>
    </optgroup>
    <optgroup label="基础数据">
     <option value="9">基础数据维护</option>
    </optgroup>
   </select>



$(function () {
 //带图片
 $("#sel_menu").select2({
  templateResult: formatState,
  templateSelection: formatState
 });
});

function formatState(state) {
 if (!state.id) { return state.text; }
 var $state = $(
  '<span><img src="/content/images/' + state.element.value.toLowerCase() + '.ico" class="img-flag" /> ' + state.text + '</span>'
 );
 return $state;
};

Copy after login

3. Remote search function (that is, dynamically fetching data from the background when the user enters the search content)

 <select id="sel_menu3" class="js-data-example-ajax form-control">
  <option value="3620194" selected="selected">请选择</option>
 </select>



$(function () {
 //远程筛选
 $("#sel_menu3").select2({
  ajax: {
   url: "/Home/GetProvinces",
   dataType: 'json',
   delay: 250,
   data: function (params) {
    return {
     q: params.term, // search term
     page: params.page
    };
   },
   processResults: function (data, params) {
    params.page = params.page || 1;

    return {
     results: data.items,
     pagination: {
      more: (params.page * 10) < data.total_count
     }
    };
   },
   cache: true
  },
  escapeMarkup: function (markup) { return markup; }, // let our custom formatter work
  minimumInputLength: 1,
  templateResult: formatRepoProvince, // omitted for brevity, see the source of this page
  templateSelection: formatRepoProvince // omitted for brevity, see the source of this page
 });
});



function formatRepoProvince(repo) {
 if (repo.loading) return repo.text;
 var markup = "<div>"+repo.name+"</div>";
 return markup;
}

Copy after login

One thing to note here is that the method corresponding to the processResults attribute has a more attribute for whether to display in pages. The value here must match the number of values ​​you need to display at one time.

The corresponding methods in the background are as follows:

public List<string> lstProvince = new List<string>() {"北京市","天津市","重庆市","上海市","河北省","山西省","辽宁省","吉林省","黑龙江省","江苏省","浙江省","安徽省","福建省","江西省","山东省","河南省","湖北省","湖南省","广东省","海南省","四川省","贵州省","云南省","陕西省","甘肃省","青海省","台湾省","内蒙古自治区","广西壮族自治区","西藏自治区","宁夏回族自治区","新疆维吾尔自治区","香港特别行政区","澳门特别行政区" };
  public JsonResult GetProvinces(string q, string page)
   {
   var lstRes = new List<Province>();
   for (var i = 0; i < 30; i++)
   {
    var oProvince = new Province();
    oProvince.id = i;
    oProvince.name = lstProvince[i];
    lstRes.Add(oProvince);
   }
   if (!string.IsNullOrEmpty(q.Trim()))
   {
    lstRes = lstRes.Where(x => x.name.Contains(q)).ToList();
   }
   var lstCurPageRes = string.IsNullOrEmpty(page) &#63; lstRes.Take(10) : lstRes.Skip(Convert.ToInt32(page) * 10 - 10).Take(10);
   return Json(new { items = lstCurPageRes, total_count = lstRes.Count }, JsonRequestBehavior.AllowGet);
  }
Copy after login

So much has been said above, so how do we get and assign values ​​after selecting the select2 option?

1. Get the selected value

 var oMenuIcon = $("#txt_menuicon_web").select2({
   placeholder: "请选择菜单图标",
   templateResult: oInit.formatState,
   templateSelection: oInit.formatState
  });
oMenuIcon.val();
Copy after login

2. Set the selected value of select2

 var oMenuIcon = $("#txt_menuicon_web").select2({
   placeholder: "请选择菜单图标",
   templateResult: oInit.formatState,
   templateSelection: oInit.formatState
  });
 oMenuIcon.val("CA").trigger("change");
Copy after login

The above is an introduction to some features and effects of select2. I hope it will be helpful to everyone's learning.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!