Home >Web Front-end >JS Tutorial >Easily use jQuery two-way select control Bootstrap Dual Listbox_jquery

Easily use jQuery two-way select control Bootstrap Dual Listbox_jquery

WBOY
WBOYOriginal
2016-05-16 15:26:093197browse

This article mainly introduces how to use the two-way select control Bootstrap Dual Listbox. Bootstrap Dual List is a list box plug-in optimized for responding to Twitter. It can be used on all modern browsers and touch devices. Share it with everyone. , the details are as follows:

Rendering:

1. Use

1. Quote css and js files

 <link href="scripts/bootstrap-3.3.5-dist/css/bootstrap.min.css" rel="stylesheet" />
  <!--<link href="//cdnjs.cloudflare.com/ajax/libs/prettify/r298/prettify.min.css" rel="stylesheet">-->
  <link href="scripts/duallistbox/bootstrap-duallistbox.min.css" rel="stylesheet" />
  <script src="scripts/jquery/jquery-2.1.4.min.js"></script>
  <script src="scripts/bootstrap-3.3.5-dist/js/bootstrap.min.js"></script>
  <!--<script src="//cdnjs.cloudflare.com/ajax/libs/prettify/r298/run_prettify.min.js"></script>-->
  <script src="scripts/duallistbox/jquery.bootstrap-duallistbox.min.js"></script>

2. Initialize the select element whose class attribute is demo1

<script type="text/javascript">
    $(function () {
      var demo2 = $('.demo1').bootstrapDualListbox({
        nonSelectedListLabel: 'Non-selected',
        selectedListLabel: 'Selected',
        preserveSelectionOnMove: 'moved',
        moveOnSelect: false,
        nonSelectedFilter: 'ion ([7-9]|[1][0-2])'
      });

      $("#showValue").click(function () {
        alert($('[name="duallistbox_demo1"]').val());
      });
    });
  </script>

3. html code

<div class="col-md-7">
    <select multiple="multiple" size="10" name="duallistbox_demo1" class="demo1">
      <option value="1">Option 1</option>
      <option value="2">Option 2</option>
      <option value="3" selected="selected">Option 3</option>
      <option value="4">Option 4</option>
      <option value="5">Option 5</option>
      <option value="6" selected="selected">Option 6</option>
      <option value="7">Option 7</option>
      <option value="8">Option 8</option>
      <option value="9">Option 9</option>
      <option value="10">Option 10</option>
    </select>
    <br />
    <input id="showValue" type="button" value="show selected data" />
  </div>

This completes the plug-in calling

2. Extension

A general js function that initializes data:

/*初始化duallistbox*/
    //queryParam1:参数
    //selectClass:select元素class属性
    //selectedDataStr:选中数据,多个以,隔开
    function initListBox(queryParam1,selectClass, selectedDataStr) {
      var paramData = {
        'testParam1': queryParam1
      }
      $.ajax({
        url: 'DataHandler.ashx',
        type: 'get',
        data: paramData,
        async: true,
        success: function (returnData) {
          var objs = $.parseJSON(returnData);
          $(objs).each(function () {
            var o = document.createElement("option");
            o.value = this['id'];
            o.text = this['name'];
            if ("undefined" != typeof (selectedDataStr) && selectedDataStr != "") {
              var selectedDataArray = selectedDataStr.split(',');
              $.each(selectedDataArray, function (i, val) {
                if (o.value == val) {
                  o.selected = 'selected';
                  return false;
                }
              });
            }
            $("." + selectClass + "")[0].options.add(o);
          });
          //渲染dualListbox
          $('.' + selectClass + '').bootstrapDualListbox({
            nonSelectedListLabel: 'Non-selected',
            selectedListLabel: 'Selected',
            preserveSelectionOnMove: 'moved',
            moveOnSelect: false//,
            //nonSelectedFilter: 'ion ([7-9]|[1][0-2])'
          });
        },
        error: function (e) {
          alert(e.msg);
        }
      });
    }

html code:

<div class="col-md-7">
    <select multiple="multiple" size="10" name="duallistbox_demo2" class="demo2">
    </select>
    <br />
    <input id="showValue" type="button" value="show selected data" />
  </div>

Call:

$(function () {
      //初始化
      initListBox('hangwei.cnblogs.com', 'demo2');

      $("#showValue").click(function () {
        alert($('[name="duallistbox_demo2"]').val());
      });
    });

DataHandler.ashx code:

<%@ WebHandler Language="C#" Class="DataHandler" %>

using System;
using System.Web;
using System.Collections.Generic;
using Newtonsoft.Json;

public class DataHandler : IHttpHandler {
  
  public void ProcessRequest (HttpContext context) {
    var china = new { id = "China", name = "中国" };
    var usa = new { id = "USA", name = "美国" };
    var rsa = new { id = "Russia", name = "俄罗斯" };
    var en = new { id = "English", name = "英国" };
    var fra = new { id = "France", name = "法国" };
    List<object> list = new List<object>();
    list.Add(china);
    list.Add(usa);
    list.Add(rsa);
    list.Add(en);
    list.Add(fra);
    string returnJson = JsonConvert.SerializeObject(list);
    context.Response.ContentType = "text/plain";
    context.Response.Write(returnJson);    
  }
 
  public bool IsReusable {
    get {
      return false;
    }
  }

}

Effect:

The development environment used in the demo of this article: VS2013, .NET Framework4.5.

The above is how to use the two-way select control Bootstrap Dual Listbox. I hope it will be helpful to everyone's learning.

Statement:
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