• Home
  • Categories
  • Recent
  • Popular
  • Top
  • Tags
  • Users
  • Groups
  • Documentation
    • Home
    • Read API
    • Write API
    • Plugin Development
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
v3.5.2 Latest
Buy Hosting

Populating a select in an admin template

Scheduled Pinned Locked Moved Solved Plugin Development
2 Posts 2 Posters 1.1k Views
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • ferikF Offline
    ferikF Offline
    ferik
    wrote on last edited by
    #1

    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?

    1 Reply Last reply
    0
  • yariplusY Offline
    yariplusY Offline
    yariplus Community Rep
    wrote on last edited by yariplus
    #2

    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>
    
    1 Reply Last reply
    0

Copyright © 2023 NodeBB | Contributors
  • Login

  • Don't have an account? Register

  • Login or register to search.
Powered by NodeBB Contributors
  • First post
    Last post
0
  • Home
  • Categories
  • Recent
  • Popular
  • Top
  • Tags
  • Users
  • Groups
  • Documentation
    • Home
    • Read API
    • Write API
    • Plugin Development