The lists show name; the selection is kept as id. Move roles and watch both readouts.
The lists show name; the selection is kept as id. Move roles and watch both readouts.
jqxPickList bound to objects: displayMember names the field the user sees, valueMember the field that selectedItems, val() and getSelectedValues() work with. The lists show role names; what the application saves is the role ids.
A live JavaScript/jQuery example of jqxPickList, the jQWidgets PickList component, with its complete source code below. It is one of 7 jqxPickList demos for jQuery.
width property – Sets or gets the width of the widget.height property – Sets or gets the height of the widget.source property – Sets or gets the pool of available items.selectedItems property – Sets or gets the values that start in the Selected list.displayMember property – Sets or gets the field of a source object used as the item label.valueMember property – Sets or gets the field of a source object used as the item value.sourceHeader property – Sets or gets the header text of the available list.targetHeader property – Sets or gets the header text of the selected list.change event – This event is triggered when the selected items change.getSelectedItems method – Returns the items currently in the selected list, in order.getSelectedValues method – Returns the values currently in the selected list, in order.To run it in your own page, load jqx.base.css, a theme and the library files below from npm (npm install jqwidgets-scripts) or the jsDelivr CDN, then add the markup and the script. jQuery is not required: jqxcore.js provides $.
The page loads jqx.base.css, a theme and these library files: jqxcore.js, jqxbuttons.js, jqxscrollbar.js, jqxlistbox.js, jqxpicklist.js.
It also loads helper scripts of this demo site, which are not part of the library: demos.js (getWidth() and other layout helpers).
$(document).ready(function () {
// Roles as the server sends them. The user picks by name; the application reads
// ids back - getSelectedValues() and val() return the valueMember field, and
// getSelectedItems() returns the original objects.
var roles = [
{ id: 'admin', name: 'Administrator' },
{ id: 'editor', name: 'Editor' },
{ id: 'author', name: 'Author' },
{ id: 'contributor', name: 'Contributor' },
{ id: 'subscriber', name: 'Subscriber' },
{ id: 'moderator', name: 'Moderator' },
{ id: 'billing', name: 'Billing manager' },
{ id: 'support', name: 'Support agent' }
];
$('#jqxPickList').jqxPickList({
width: 620,
height: 320,
source: roles,
displayMember: 'name',
valueMember: 'id',
selectedItems: ['editor', 'contributor'], // ids, not names
sourceHeader: 'Available roles',
targetHeader: 'Assigned roles'
});
var show = function () {
var values = $('#jqxPickList').jqxPickList('getSelectedValues');
var items = $('#jqxPickList').jqxPickList('getSelectedItems');
$('#values').text(JSON.stringify({ userId: 4021, roles: values }, null, 2));
$('#items').text(items.length ? items.map(function (r) { return r.name + ' (' + r.id + ')'; }).join('\n') : '(none)');
};
$('#jqxPickList').on('change', show);
show();
});
<div id="content">
<div class="demo">
<p class="demo-hint">
The lists show <code>name</code>; the selection is kept as <code>id</code>. Move roles and watch both readouts.
</p>
<div id="jqxPickList"></div>
<div class="demo-out">
<div>
<strong>getSelectedValues() - what the form submits</strong>
<code id="values"></code>
</div>
<div>
<strong>getSelectedItems() - the objects</strong>
<code id="items"></code>
</div>
</div>
</div>
</div>