You're here: Snippet Directory » HTML/JavaScript (65)
Language:

Item selector object

Language: English
Programming Language: JavaScript
Published by: pjjt [not registered]
Last Update: 5/15/2006
Views: 381


Description

Let's you select items in a list (usually in a table), swaps the class names and stores the keys. Can synch checkboxes over multiple pages, if your list goes from page to page. Invaluable for user interfaces in web-based applications. If you email me I can send you a sample HTML page utilizing this.

Code

1 function ItemSelector(strType,strHLClass) { 2 this.Type = strType; 3 this.HLClass = strHLClass; // highlighted class 4 5 this.Class = ""; // normal class 6 this.CheckBox = null; // checkbox collection 7 8 // internal only 9 this.HasSelected = false; 10 this.SelectedItem = null; 11 this.Key = ""; 12 this.HasMultiSelected = false; 13 this.SelectedItems = new Array(); 14 this.Keys = new Array(); 15 16 this.RCM = false; // todo 17 this.DelayRCM = false; // todo 18 19 // methods 20 this.ToggleSelect = ItemSelector_ToggleSelect; 21 this.ToggleCheck = ItemSelector_ToggleCheck; 22 this.StoreChecked = ItemSelector_StoreChecked; 23 this.RestoreChecked = ItemSelector_RestoreChecked; 24 this.Select = ItemSelector_Select; 25 this.ResetLast = ItemSelector_ResetLast; 26 this.Reset = ItemSelector_Reset; 27 this.ResetAll = ItemSelector_ResetAll; 28 this.Warn = ItemSelector_Warn; 29 } 30 31 function ItemSelector_ToggleSelect(oItem,strKey) { 32 if (this.SelectedItem==oItem) this.ResetLast(); 33 else this.Select(oItem,strKey); 34 } 35 36 function ItemSelector_ToggleCheck(oItem, strKey, iCheckIndex) { 37 var bCheck = false; 38 if (this.HasSelected) { 39 bCheck = !(this.SelectedItem==oItem || this.CheckBox[iCheckIndex].checked); 40 this.ResetLast(); 41 } else { 42 bCheck = !this.CheckBox[iCheckIndex].checked; 43 } 44 this.CheckBox[iCheckIndex].checked = bCheck; 45 if (bCheck) { 46 this.HasMultiSelected = true; 47 this.SelectedItems[this.SelectedItems.length] = oItem; 48 this.Keys[this.Keys.length] = strKey; 49 } else { 50 for (var i=0; i<this.SelectedItems.length; i++) if (this.SelectedItems[i] == oItem) { 51 this.SelectedItems = this.SelectedItems.remove(i); 52 this.Keys = this.Keys.remove(i); 53 this.HasMultiSelected = this.SelectedItems.length>0; 54 } 55 } 56 return (this.CheckBox[iCheckIndex].checked); 57 } 58 59 function ItemSelector_Select(oItem,strKey) { 60 this.ResetLast(); 61 this.SelectedItem = oItem; 62 this.Key = strKey 63 this.SelectedItem.normalClassName = oItem.className; 64 this.SelectedItem.className = this.HLClass; 65 this.HasSelected = true; 66 } 67 68 function ItemSelector_ResetLast() { 69 if (this.HasSelected) { 70 this.SelectedItem.className = this.SelectedItem.normalClassName; 71 this.HasSelected = false; 72 this.SelectedItem = null; 73 } 74 } 75 76 function ItemSelector_Reset() { 77 if (this.HasSelected && document.all[this.SelectedItem.id]) { 78 this.SelectedItem = document.all[this.SelectedItem.id] 79 this.SelectedItem.className = this.HLClass; 80 } else { 81 this.ResetLast(); 82 } 83 } 84 85 function ItemSelector_ResetAll() { 86 if (this.HasSelected) { 87 this.SelectedItem.className = this.SelectedItem.normalClassName; 88 this.HasSelected = false; 89 this.SelectedItem = null; 90 } 91 if (this.HasMultiSelected && typeof(this.CheckBox) == "object") { 92 for (var i=0; i<this.CheckBox.length; i++) this.CheckBox[i].checked = false; 93 this.HasMultiSelected = false; 94 this.SelectedItems = new Array(); 95 this.Keys = new Array(); 96 } 97 } 98 99 function ItemSelector_Warn() { 100 alert("Please select a " + this.Type.toLowerCase() + "."); 101 } 102 103 function ItemSelector_StoreChecked(oElem) { 104 if (this.HasMultiSelected) { 105 oElem.value += ((oElem.value!="") ? "," : "") + this.Keys.join(","); 106 } 107 } 108 109 function ItemSelector_RestoreChecked(oElem) { 110 for (var i=0; i<this.CheckBox.length; i++) this.CheckBox[i].checked = false; 111 var arrChecked = oElem.value.split(","); 112 if (arrChecked.length) { 113 for (var i=0; i<arrChecked.length; i++) for (var j=0; j<this.CheckBox.length; j++) if (arrChecked[i] == this.CheckBox[j].value) { 114 this.CheckBox[j].parentElement.parentElement.onclick(); 115 arrChecked = arrChecked.remove(i); 116 i--; 117 } 118 } 119 oElem.value = arrChecked.join(","); 120 } 121

No comments avaiable

Add a comment

Name *  

Email (won't be displayed) *    

Website  

Comment *  

Sicherheitscode Security Code *    

RSS