|
Hi all, we've been working on this and have gotten about 90% of the way there. We should have a finished and completely working version of this finished by the end of the week. But this is an alpha build of our code and it mostly works. We found the code in ajaxsuggest.js and have taken to modify it and we're just going to attach it with a BSD license. Here is what we've got so far:
/*
* Copyright (c) 2008, Common Ground Technologies, a wholly owned subidary of
* Common Grounds Coffee House, Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of Comomon Grounds Coffee House, Inc. nor the
* names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY COMMON GROUNDS COFFEE HOUSE, INC. ''AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL COMMON GROUNDS COFFEE HOUSE, INC. BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
function preloadSelectContent(txt, selectControl, selectValue, record_id)
{
if(record_id!="")
record_id="_"+record_id
if($('#'+selectControl+record_id)[0].tagName=='SELECT')
{
var j=0;
var lookup = $('#'+selectControl+record_id).get(0);
lookup.options[0]=new Option(TEXT_PLEASE_SELECT,'');
var str = txt.split('\n');
var index = 0;
for(i=0; i < str.length - 1; i=i+2, j++) {
lookup.options[j+1]=new Option(unescape(str[i+1]),unescape(str[i]));
if ( unescape(str[i]) == selectValue ) {index = j+1;}
}
lookup.selectedIndex = index;
if ( j == 1 && selectValue=="") { lookup.selectedIndex = 1; }
}
else if(txt.length)
{
var str = txt.split('\n');
if(str.length==3)
{
$('#'+selectControl+record_id).val(unescape(str[0]));
$('#display_'+selectControl+record_id).val(unescape(str[1]));
}
}
} function preloadMultiSelectContent(txt, selectControl, selectValue, record_id)
{
if(record_id!="")
record_id="_"+record_id
var j=-1;
var lookup = $('#'+selectControl+record_id).get(0);
var str = txt.split('\n');
var sel = selectValue.split('\n');
var index = 0;
for(i=0; i < str.length - 1; i=i+2, j++)
{
lookup.options[j+1]=new Option(unescape(str[i+1]),unescape(str[i]));
for(k=0;k<sel.length-1;k++)
if ( unescape(str[i]) == unescape(sel[k]) )
{
lookup.options[j+1].selected=true;
break;
}
}
} /**
* caching support added by nathan nobbe, (c) moxune 2008; moxune.com
*/
window.optionCategoryOptionsCache = {
cachedIds : [],
cachedNames : [],
cachedCategoryOptionIds : [], // indexed by cachedIds
cachedCategoryOptionNames : [], // indexed by cachedIds
_lastId : -1,
add : function(id, name) {
if(this.findById(id) !== false) return false;
this.cachedIds.push(id);
this.cachedNames.push(name);
},
addOptionById : function(id, optionId, optionName) {
if(!this._find(id, this.cachedIds)) return false;
if(this.findOptionById(id, optionId) !== false)
return false;
if(!(this.cachedCategoryOptionIds[id] instanceof Array))
this.cachedCategoryOptionIds[id] = [];
if(!(this.cachedCategoryOptionNames[id] instanceof Array))
this.cachedCategoryOptionNames[id] = [];
this.cachedCategoryOptionIds[id].push(optionId);
this.cachedCategoryOptionNames[id].push(optionName);
},
removeById : function(id) {
if(this.findById(id) !== false) return;
this._removeByLastId();
},
removeByName : function(name) {
if(this.findByName(name) !== false) return;
this._removeByLastId();
},
removeOptionsById : function(id) {
if(this.findById(id) !== false) return;
this.cachedCategoryOptionIds[id] = [];
this.cachedCategoryOptionNames[id] = [];
},
findByName : function(name) {
return this._find(name, this.cachcedNames);
},
findById : function(id) {
var buffer = this._find(id, this.cachedIds);
if( (buffer !== false) &&
(this.cachedCategoryOptionIds[id] instanceof Array) )
return buffer;
return false;
},
findOptionsById : function(id) {
if(!this.findById(id)) return false;
if(!(this.cachedCategoryOptionIds[id] instanceof Array))
return false;
var results = [];
for(var m in this.cachedCategoryOptionIds[id]) {
if( ($.isFunction(this.cachedCategoryOptionIds[id][m])) ||
(this.cachedCategoryOptionIds[id][m] == undefined) )
continue;
results.push(this._returnResultObject(
this.cachedCategoryOptionIds[id][m],
this.cachedCategoryOptionNames[id][m]));
}
return results;
},
findOptionById : function(id, optionId) {
if(!this.findById(id)) return false;
if(!(this.cachedCategoryOptionIds[id] instanceof Array))
return false;
for(var i in this.cachedCategoryOptionIds[id])
if(optionId == this.cachedCategoryOptionIds[id][i])
return this.cachedCategoryOptionIds[id][i];
return false;
},
_find : function(value, arrayToSearch) {
for(var i in arrayToSearch)
if(value == arrayToSearch[i]) {
this.lastId = i;
return this._returnResultObject(
this.cachedIds[i], this.cachedNames[i]);
}
return false;
},
_returnResultObject : function(id, name) {
return {id : id, name : name, type : 'OPTION'};
},
_removeByLastId : function() {
var _cachedIds = [];
var _cachedNames = [];
for(var i=0; i<this.cachedIds.length; i++) {
if(i == this._lastId) continue;
_cachedIds.push(this.cachedIds[i]);
_cachedNames.push(this.cachedIds[i]);
}
this.cachedIds = _cachedIds;
this.cachedNames = _cachedNames;
}
} function loadSelectContent(table,main_field, dependent_field, record_id)
{
var field = myEncode( dependent_field );
var value = myEncode( $('#value_'+main_field+record_id).val() );
var type = $('#value_'+dependent_field+record_id)[0].tagName;
var selectedOptionCategories = $("#value_"+main_field+" option:selected"); var updateUi = function(txt) {
var selectedOptionCategories = $("#value_"+main_field+" option:selected");
selectedOptionCategories.scrollable = true;
if($('#value_'+dependent_field+record_id)[0].tagName=='SELECT') {
var targetSelect =$('#value_'+dependent_field+record_id)[0];
var j=0;
var str = txt.split('\n');
var optionsAndOptgroups = '';
if(!targetSelect.multiple)
targetSelect.innerHTML='<option value="">'+TEXT_PLEASE_SELECT+'</option>';
else {
targetSelect.innerHTML='';
j=-1;
}
selectedOptionCategories.each(function(i, e) {
var options = '';
var _e = $(e);
var cachedOptions = window.optionCategoryOptionsCache.findOptionsById(
_e.val());
if(cachedOptions === false) {
for(i=0; i < str.length-1; i=i+=2) {
window.optionCategoryOptionsCache.addOptionById(
selectedOptionCategories.val(), unescape(str[i]), unescape(str[i+1]));
options += '<option value="'+unescape(str[i])+'>'+unescape(str[i+1])+'</option>';
}
} else {
for(i in cachedOptions)
if(cachedOptions[i].type && cachedOptions[i].type == 'OPTION')
options += '<option value="'+cachedOptions[i].id+'>'+cachedOptions[i].name+'</option>';
}
optionsAndOptgroups += '<optgroup label="'+_e.html()+'">'+options+'</optgroup>';
});
$(targetSelect).html(optionsAndOptgroups);
} else {
$('#value_'+dependent_field+record_id).val("");
$('#display_value_'+dependent_field+record_id).val("");
if(txt.length) {
var str = txt.split('\n');
if(str.length==3) {
$('#value_'+dependent_field+record_id).val(unescape(str[0]));
$('#display_value_'+dependent_field+record_id).val(unescape(str[1]));
}
}
}
} if(window.optionCategoryOptionsCache.add(
selectedOptionCategories.val(),
selectedOptionCategories.html()) !== false) {
if(record_id!="")
record_id="_"+record_id
$.get(table+"_autocomplete.php",
{
field: field,
value: value,
type: type
}, updateUi);
} else {
var txt = '';
var options = this.optionCategoryOptionsCache.findOptionsById(selectedOptionCategories.val());
window.myoptions = options;
for(var n in options)
if(options[n] !== undefined)
txt += options[n].id + '\n' + options[n].name + '\n';
updateUi(txt);
}
} We have the display part done, now is getting it to format the output correctly to add into the database id records like 23,35,123,1234,11,34,22 etc.. When we have the finished code done, I'll delete this and post the working version, again under a BSD license so whomever wants to use it can. Open for suggestions/comments.
|