From f09ed6e1d77eb113db14e8f4a00afc09531a43d8 Mon Sep 17 00:00:00 2001 From: Moritz Bunkus Date: Mon, 27 Dec 2010 13:57:43 +0100 Subject: [PATCH] Automatisches Sortieren der Liste --- js/jquery.multiselect2side.js | 21 +- js/jquery.selectboxes.js | 553 ++++++++++++++++++++++++++++++++++ 2 files changed, 565 insertions(+), 9 deletions(-) create mode 100644 js/jquery.selectboxes.js diff --git a/js/jquery.multiselect2side.js b/js/jquery.multiselect2side.js index 259614192..928a3ce04 100644 --- a/js/jquery.multiselect2side.js +++ b/js/jquery.multiselect2side.js @@ -118,17 +118,17 @@ // FIRST HIDE ALL div.find(".RemoveOne, .MoveUp, .MoveDown, .MoveTop, .MoveBottom, .SelSort").addClass('ms2side__hide'); - if (selectDx.size() > 1) - div.find(".SelSort").removeClass('ms2side__hide'); + // if (selectDx.size() > 1) + // div.find(".SelSort").removeClass('ms2side__hide'); if (selectedDx.size() > 0) { div.find(".RemoveOne").removeClass('ms2side__hide'); - // ALL SELECTED - NO MOVE - if (selectedDx.size() < selectDx.size()) { // FOR NOW (JOE) && selectedDx.size() == 1 - if (selectedDx.val() != selectDx.val()) // FIRST OPTION, NO UP AND TOP BUTTON - div.find(".MoveUp, .MoveTop").removeClass('ms2side__hide'); - if (selectedDx.last().val() != selectDx.last().val()) // LAST OPTION, NO DOWN AND BOTTOM BUTTON - div.find(".MoveDown, .MoveBottom").removeClass('ms2side__hide'); - } + // // ALL SELECTED - NO MOVE + // if (selectedDx.size() < selectDx.size()) { // FOR NOW (JOE) && selectedDx.size() == 1 + // if (selectedDx.val() != selectDx.val()) // FIRST OPTION, NO UP AND TOP BUTTON + // div.find(".MoveUp, .MoveTop").removeClass('ms2side__hide'); + // if (selectedDx.last().val() != selectDx.last().val()) // LAST OPTION, NO DOWN AND BOTTOM BUTTON + // div.find(".MoveDown, .MoveBottom").removeClass('ms2side__hide'); + // } } if (selectSx.size() == 0 || (o.maxSelected >= 0 && selectSx.size() >= o.maxSelected)) @@ -140,6 +140,9 @@ div.find(".RemoveAll").addClass('ms2side__hide'); else div.find(".RemoveAll").removeClass('ms2side__hide'); + + leftSel.sortOptions(); + rightSel.sortOptions(); }); // DOUBLE CLICK ON LEFT SELECT OPTION diff --git a/js/jquery.selectboxes.js b/js/jquery.selectboxes.js new file mode 100644 index 000000000..033b7a554 --- /dev/null +++ b/js/jquery.selectboxes.js @@ -0,0 +1,553 @@ +/* + * + * Copyright (c) 2006-2010 Sam Collett (http://www.texotela.co.uk) + * Dual licensed under the MIT (http://www.opensource.org/licenses/mit-license.php) + * and GPL (http://www.opensource.org/licenses/gpl-license.php) licenses. + * + * Version 2.2.5 + * Demo: http://www.texotela.co.uk/code/jquery/select/ + * + * + */ + +;(function($) { + +/** + * Adds (single/multiple) options to a select box (or series of select boxes) + * + * @name addOption + * @author Sam Collett (http://www.texotela.co.uk) + * @type jQuery + * @example $("#myselect").addOption("Value", "Text"); // add single value (will be selected) + * @example $("#myselect").addOption("Value 2", "Text 2", false); // add single value (won't be selected) + * @example $("#myselect").addOption({"foo":"bar","bar":"baz"}, false); // add multiple values, but don't select + * + */ +$.fn.addOption = function() +{ + var add = function(el, v, t, sO, index) + { + var option = document.createElement("option"); + option.value = v, option.text = t; + // get options + var o = el.options; + // get number of options + var oL = o.length; + if(!el.cache) + { + el.cache = {}; + // loop through existing options, adding to cache + for(var i = 0; i < oL; i++) + { + el.cache[o[i].value] = i; + } + } + if (index || index == 0) + { + // we're going to insert these starting at a specific index... + // this has the side effect of el.cache[v] being the + // correct value for the typeof check below + var ti = option; + for(var ii =index; ii <= oL; ii++) + { + var tmp = el.options[ii]; + el.options[ii] = ti; + o[ii] = ti; + el.cache[o[ii].value] = ii; + ti = tmp; + } + } + + // add to cache if it isn't already + if(typeof el.cache[v] == "undefined") el.cache[v] = oL; + el.options[el.cache[v]] = option; + if(sO) + { + option.selected = true; + } + }; + + var a = arguments; + if(a.length == 0) return this; + // select option when added? default is true + var sO = true; + // multiple items + var m = false; + // other variables + var items, v, t; + if(typeof(a[0]) == "object") + { + m = true; + items = a[0]; + } + if(a.length >= 2) + { + if(typeof(a[1]) == "boolean") + { + sO = a[1]; + startindex = a[2]; + } + else if(typeof(a[2]) == "boolean") + { + sO = a[2]; + startindex = a[1]; + } + else + { + startindex = a[1]; + } + if(!m) + { + v = a[0]; + t = a[1]; + } + } + this.each( + function() + { + if(this.nodeName.toLowerCase() != "select") return; + if(m) + { + for(var item in items) + { + add(this, item, items[item], sO, startindex); + startindex += 1; + } + } + else + { + add(this, v, t, sO, startindex); + } + } + ); + return this; +}; + +/** + * Add options via ajax + * + * @name ajaxAddOption + * @author Sam Collett (http://www.texotela.co.uk) + * @type jQuery + * @param String url Page to get options from (must be valid JSON) + * @param Object params (optional) Any parameters to send with the request + * @param Boolean select (optional) Select the added options, default true + * @param Function fn (optional) Call this function with the select object as param after completion + * @param Array args (optional) Array with params to pass to the function afterwards + * @example $("#myselect").ajaxAddOption("myoptions.php"); + * @example $("#myselect").ajaxAddOption("myoptions.php", {"code" : "007"}); + * @example $("#myselect").ajaxAddOption("myoptions.php", {"code" : "007"}, false, sortoptions, [{"dir": "desc"}]); + * + */ +$.fn.ajaxAddOption = function(url, params, select, fn, args) +{ + if(typeof(url) != "string") return this; + if(typeof(params) != "object") params = {}; + if(typeof(select) != "boolean") select = true; + this.each( + function() + { + var el = this; + $.getJSON(url, + params, + function(r) + { + $(el).addOption(r, select); + if(typeof fn == "function") + { + if(typeof args == "object") + { + fn.apply(el, args); + } + else + { + fn.call(el); + } + } + } + ); + } + ); + return this; +}; + +/** + * Removes an option (by value or index) from a select box (or series of select boxes) + * + * @name removeOption + * @author Sam Collett (http://www.texotela.co.uk) + * @type jQuery + * @param String|RegExp|Number what Option to remove + * @param Boolean selectedOnly (optional) Remove only if it has been selected (default false) + * @example $("#myselect").removeOption("Value"); // remove by value + * @example $("#myselect").removeOption(/^val/i); // remove options with a value starting with 'val' + * @example $("#myselect").removeOption(/./); // remove all options + * @example $("#myselect").removeOption(/./, true); // remove all options that have been selected + * @example $("#myselect").removeOption(0); // remove by index + * @example $("#myselect").removeOption(["myselect_1","myselect_2"]); // values contained in passed array + * + */ +$.fn.removeOption = function() +{ + var a = arguments; + if(a.length == 0) return this; + var ta = typeof(a[0]); + var v, index; + // has to be a string or regular expression (object in IE, function in Firefox) + if(ta == "string" || ta == "object" || ta == "function" ) + { + v = a[0]; + // if an array, remove items + if(v.constructor == Array) + { + var l = v.length; + for(var i = 0; i=0; i--) + { + if(v.constructor == RegExp) + { + if(o[i].value.match(v)) + { + remove = true; + } + } + else if(o[i].value == v) + { + remove = true; + } + // if the option is only to be removed if selected + if(remove && a[1] === true) remove = o[i].selected; + if(remove) + { + o[i] = null; + } + remove = false; + } + } + else + { + // only remove if selected? + if(a[1] === true) + { + remove = o[index].selected; + } + else + { + remove = true; + } + if(remove) + { + this.remove(index); + } + } + } + ); + return this; +}; + +/** + * Sort options (ascending or descending) in a select box (or series of select boxes) + * + * @name sortOptions + * @author Sam Collett (http://www.texotela.co.uk) + * @type jQuery + * @param Boolean ascending (optional) Sort ascending (true/undefined), or descending (false) + * @example // ascending + * $("#myselect").sortOptions(); // or $("#myselect").sortOptions(true); + * @example // descending + * $("#myselect").sortOptions(false); + * + */ +$.fn.sortOptions = function(ascending) +{ + // get selected values first + var sel = $(this).selectedValues(); + var a = typeof(ascending) == "undefined" ? true : !!ascending; + this.each( + function() + { + if(this.nodeName.toLowerCase() != "select") return; + // get options + var o = this.options; + // get number of options + var oL = o.length; + // create an array for sorting + var sA = []; + // loop through options, adding to sort array + for(var i = 0; i o2t ? -1 : 1; + } + } + ); + // change the options to match the sort array + for(var i = 0; i