首頁 >web前端 >js教程 >輕鬆使用jQuery雙向select控制項Bootstrap Dual Listbox_jquery

輕鬆使用jQuery雙向select控制項Bootstrap Dual Listbox_jquery

WBOY
WBOY原創
2016-05-16 15:26:093197瀏覽

本文主要為大家介紹了雙向select控制項Bootstrap Dual Listbox的使用方法,Bootstrap Dual列表是一個為響應Twitter優化的列錶框插件,它可以用在所有的現代瀏覽器和觸摸設備上,分享給大家,具體如下:

效果圖:

一、使用

1、引用css和js檔案

 <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、初始化class屬性為demo1的select元素

<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代碼

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

這樣就完成了插件的呼叫

二、擴充

一個通用的、初始化資料的js函數:

/*初始化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代碼:

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

呼叫:

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

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

DataHandler.ashx程式碼:

<%@ 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;
    }
  }

}

效果:

本文的demo所使用的開發環境:VS2013、.NET Framework4.5.

以上就是雙向select控制項Bootstrap Dual Listbox的使用方法,希望對大家的學習有所幫助。

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn