Populating a select in an admin template

Solved Plugin Development
  • I am able to set its value via settings.sync & data-key. Is there a way to directly map the data-options to a setting value?

    If that's not possible, is the best way to retrieve the setting in the same method I do a settings.sync and set the options via JS?

  • You can do that by registering a new settings plugin. Just add the registerPlugin call before you do your settings.sync()

    In the plugin, map the name/value pairs to an array using jQuery, then add the current value to the array.

    settings.registerPlugin({
    	types: ['mySelect'], // This is your data-type for the <select>
    	set: function (element, value, trim) {
    
    		// Here we set the options based on the name/value pairs that were saved previously.
    		element = $(element);
    		if (value) value.forEach(function (mapping) {
    			if (mapping.name === 'value') {
    				element.val(mapping.value);
    			}else{
    				element.append('<option value="'+mapping.value+'">'+mapping.name+'</option>');
    			}
    		});
    
    		//////////////////
    		// Here you probably want to append any default options if they don't already exist.
    		//////////////////
    	},
    	get: function (element, trim, empty) {
    
    		// Here we get the mapping for saving into the database.
    		element = $(element);
    		var	value = [];
    		element.children().each(function (i, option) {
    			option = $(option);
    			value.push({name:option.text(),value:option.attr('value')}); // Get the text and value of each <option>
    		});
    		value.push({name:'value',value:element.val()}); // Get the current value.
    		// return the mapping.
    		return value;
    	}
    });
    

    In the template, you would just do this:

    <select data-type="mySelect" data-key="something"></select>
    

Suggested Topics


  • Accesing JQuery in widget template

    Unsolved Plugin Development
    0 Votes
    2 Posts
    265 Views

    Javascript embedded in the widget can't access the jquery object on page load since jquery hasn't loaded yet. You can use something like this

    (function() { function onLoad() { // widget code } if (window.jQuery) { onLoad(); } else { window.addEventListener('DOMContentLoaded', onLoad); } })();
  • JQuery in Widget Template

    Solved Plugin Development
    0 Votes
    3 Posts
    2k Views

    You can close this thread..

    Resolved by using app.loadJQueryUI()

  • 0 Votes
    3 Posts
    2k Views

    For anyone looking to do this for a plugin, it's easy enough using getObjectValues()

    var db = require.main.require('./src/database') var User = require.main.require('./src/user') db.getObjectValues('steamid:uid', function (err, uids) { if (err) return console.log(err) uids = uids.sort().filter(function(item, pos, ary) { return !pos || item != ary[pos - 1] }) User.getUsersFields(uids, ['username', 'email', 'steamid', 'profileurl'], function (err, users) { if (err) return console.log(err) users.forEach(function (user) { // Do a thing here. console.dir(user) }) }) })
  • 1 Votes
    11 Posts
    4k Views

    I did a try and i liked it 🙂

    It would be awesome if you could choose what category works that way and what category works on the standard forum way 🙂

  • 1 Votes
    3 Posts
    2k Views
    templates.render ('tpl/name', {}, function() { // templates.cache['tpl/name'] });

    I think so anyways, I'm writing this from my phone