Page 1 of 1

Selectable drop down list with registered users

Posted: 2020-11-07 22:33
by landinialejandro
Hello, I want to leave you in this opportunity a function that allows you to list all the users of the application.
The first thing they need is that there is a text type field in the destination table, it doesn't need anything special.
Then they need to put the function somewhere where it can be accessed at any time, for example in header-extras.php.

Inside the tablename-dv.js file, place the function call. the only parameters that we pass are the name of the table and the field where the data is to be saved.

I hope you find it useful both to me.

function definition:

Code: Select all

/**
 * Construct a selectable drop down list with registered users.
 * @param {string} f - Field name to replace wit drop-down list.
 * @param {string} t - table name destiny.
 */
function users_dropdown(f, t) {
    var $selectField = $j('#' + f + '').hide();
    var $span = $j('<span/>', { id: 's2_users_' + f });
    $selectField.closest('div').append($span);
    var val = $selectField.val();

    $span.select2({
        width: '100%',
        formatNoMatches: function(term) { return 'No matches found!'; },
        minimumResultsForSearch: 5,
        loadMorePadding: 200,
        escapeMarkup: function(m) { return m; },
        ajax: {
            url: 'admin/getUsers.php',
            dataType: 'json',
            cache: true,
            data: function(term, page) { return { s: term, p: page, t: t }; },
            results: function(resp, page) { return resp; }
        }
    }).on('change', function(e) {
        $j('[name="' + f + '"]').val(e.added.id);
    });
    if (val) {
        $span.select2('data', { text: val, id: val });
    }
}
caller fucntion inside tablename-dv.js

Code: Select all

t = 'contacto';
f = 'user';
users_dropdown(f, t);
The attachment normal_text.gif is no longer available

Re: Selectable drop down list with registered users

Posted: 2020-11-12 11:31
by a.gneady
Thanks Landini :)
Just a headsup: this would work only for admin user.

Re: Selectable drop down list with registered users

Posted: 2020-11-12 16:00
by landinialejandro
a.gneady wrote:
2020-11-12 11:31
Thanks Landini :)
Just a headsup: this would work only for admin user.
Hello! thanks. Yes, I forgot to say it is a condition that only works for the admin. since it should be the only one that assigns a directory to a data record.