- added users dropdown field. Closed #1034

This commit is contained in:
Mykyta Synelnikov
2023-06-06 17:00:45 +03:00
parent 76cb1cc448
commit 4d8799b4f9
3 changed files with 88 additions and 41 deletions
+41 -4
View File
@@ -1,17 +1,48 @@
function um_admin_init_users_select() {
if ( jQuery('.um-user-select-field').length ) {
function avatarformat( data ) {
var option;
if ( ! data.id ) {
return data.text;
}
if ( 'undefined' !== typeof data.img ) {
option = jQuery('<span><img style="vertical-align: sub; width: 20px; height: 20px;" src="' + data.img + '" /> ' + data.text + '</span>');
} else {
var img = data.element.attributes['data-img']['value'];
if ( img ) {
option = jQuery('<img style="vertical-align: sub; width: 20px; height: 20px;" src="' + img + '" /> ' + data.text + '</span>');
} else {
option = jQuery('<span>' + data.text + '</span>');
}
}
return option;
}
var select2_atts = {
ajax: {
url: wp.ajax.settings.url,
dataType: 'json',
delay: 250, // delay in ms while typing when to perform a AJAX search
data: function( params ) {
return {
search: params.term, // search query
var args = {
action: 'um_get_users', // AJAX action for admin-ajax.php
search: params.term, // search query
page: params.page || 1, // infinite scroll pagination
nonce: um_admin_scripts.nonce
};
jQuery.each( jQuery(this)[0].attributes, function() {
// this.attributes is not a plain object, but an array
// of attribute nodes, which contain both the name and value
if ( this.specified ) {
if ( -1 !== this.name.indexOf( 'data-ajax-args-' ) ) {
var arg_name = this.name.replace( 'data-ajax-args-', '' ).trim();
args[ arg_name ] = this.value;
}
}
});
return args;
},
processResults: function( response, params ) {
params.page = params.page || 1;
@@ -19,7 +50,11 @@ function um_admin_init_users_select() {
if ( response.data.users ) {
jQuery.each( response.data.users, function( index, text ) {
options.push( { id: text.ID, text: text.user_login + ' (#' + text.ID + ')' } );
if ( typeof text.img !== 'undefined' ) {
options.push({ id: text.ID, text: text.user_login + ' (#' + text.ID + ')', img: text.img });
} else {
options.push( { id: text.ID, text: text.user_login + ' (#' + text.ID + ')' } );
}
});
}
@@ -38,7 +73,9 @@ function um_admin_init_users_select() {
allowHtml: true,
dropdownCssClass: 'um-select2-users-dropdown',
containerCssClass : 'um-select2-users-container',
placeholder: jQuery(this).data('placeholder')
placeholder: jQuery(this).data('placeholder'),
templateSelection: avatarformat,
templateResult: avatarformat
};
jQuery('.um-user-select-field').select2( select2_atts );