1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
| /*! AjaxSelectOption 1.0.0-dev
* ©2014 LifeOfData Inc - lifeofdata.com/license
*/
/*
* @summary SelectOptAjax
* @description dynamic completion of select box with remote data on change
* @version 1.0.0-dev
* @file selectedOptAjax.js
* @author Jean-Marc Christfried BALIZOU
* @contact fr.linkedin.com/pub/jean-m-christfried-balizou/5a/5b3/bb8/
* @copyright Copyright 2014 LifeOfData Inc <christfriedbalizou@gmail.com>.
*
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301, USA.
*
*
*/
(function($){
$.fn.selectOptAjax = function(options){
/*
* @Variable: defaults
* @Purpose : Storage of default parameters, used for selectOptAjax
* @Function : $.fn.selectOptAjax
*/
var defaults = {
destination : null,
serverUrl : null,
serverFlag : null
};
/*
* @Variable: settings
* @Purpose : Storage of settings parameters
* @Function : $.fn.selectOptAjax
*/
var settings = $.extend(true, {}, defaults, options);
/* Check if the parameters are not null */
var exit = settings.destination == null
? false : settings.serverUrl == null
? false : settings.serverFlag == null ? false : true
/* If false exit the plugin */
if(!exit){
console.log("Error, initialize all the parameters");
alert("Error, initialize all the parameters");
return $(this);
}
/* Binding on change action of current element */
return $(this).on('change', function(event){
/* Disabling the default behavior on the element */
event.preventDefault();
/* Starting ajax process */
$.ajax({
url : settings.serverUrl,
type : "GET",
dataType : "json",
data : [{"name" : settings.serverFlag, "value" : $("option:selected", this).val()}],
success : function(data){
/* Clearing previous data */
$(settings.destination).empty();
console.log(data);
/* Looping and adding new data from serveur
for(var i = 0; i < data.length; i++){
$(settings.destination).append(
$("<option/>", {
value : data[i],
text : data[i]
})
);
}*/
},
error : function( jqXHR, textStatus, errorThrown ){
console.error("Status: " + textStatus + ", Error Thrown: " + errorThrown);
alert("Status: " + textStatus + ", Error Thrown: " + errorThrown);
}
});
});
}
})(jQuery); |