EasyUI Forum

General Category => EasyUI for jQuery => Topic started by: devnull on September 06, 2018, 03:04:59 AM



Title: Globall include a ajax parameter for every easyui element
Post by: devnull on September 06, 2018, 03:04:59 AM
I need to include a couple of parameters in the ajax request for EVERY element.

Rather than manually adding the parameter to each instance in every page, is it possible to extend eui and do this globally ?

Thanks


Title: Re: Globall include a ajax parameter for every easyui element
Post by: stworthy on September 06, 2018, 07:33:17 AM
Many components have 'loader' function to load data from remote server. Override this function to append extra parameter values. The code below shows how to add additional parameter values to the datagrid 'load' function.
Code:
$.extend($.fn.datagrid.defaults, {
loader: function(param, success, error){
var extParams = {
param1: 'value1',
param2: 'value2'
};
var opts = $(this).datagrid('options');
if (!opts.url) return false;
$.ajax({
type: opts.method,
url: opts.url,
data: extend({}, extParams, param),
dataType: 'json',
success: function(data){
success(data);
},
error: function(){
error.apply(this, arguments);
}
});
}
});


Title: Re: Globall include a ajax parameter for every easyui element
Post by: devnull on September 06, 2018, 04:35:52 PM
Thanks, but that requires me to extend every element individually.

Could this be done at the jquery $.ajax level instead ?


Title: Re: Globall include a ajax parameter for every easyui element
Post by: stworthy on September 06, 2018, 04:53:44 PM
Yes, you can do it like this:
Code:
$.ajaxSetup({
  data: {
    param1: 'value1',
    param2: 'value2'
  }
});