Installations
npm install ampersand-select-view
Developer Guide
Typescript
No
Module System
CommonJS
Node Version
6.5.0
NPM Version
3.10.6
Score
70.4
Supply Chain
95.7
Quality
80
Maintenance
100
Vulnerability
97.9
License
Releases
Contributors
Unable to fetch Contributors
Languages
JavaScript (99.62%)
HTML (0.38%)
Love this project? Help keep it running — sponsor us today! 🚀
Developer
AmpersandJS
Download Statistics
Total Downloads
186,948
Last Day
28
Last Week
122
Last Month
609
Last Year
5,351
GitHub Statistics
11 Stars
170 Commits
26 Forks
10 Watching
1 Branches
23 Contributors
Bundle Size
80.23 kB
Minified
23.88 kB
Minified + Gzipped
Package Meta Information
Latest Version
8.0.0
Package Id
ampersand-select-view@8.0.0
Size
14.28 kB
NPM Version
3.10.6
Node Version
6.5.0
Total Downloads
Cumulative downloads
Total Downloads
186,948
Last day
64.7%
28
Compared to previous day
Last week
-43.3%
122
Compared to previous week
Last month
42%
609
Compared to previous month
Last year
-22.6%
5,351
Compared to previous year
Daily Downloads
Weekly Downloads
Monthly Downloads
Yearly Downloads
ampersand-select-view
Lead Maintainer: Christopher Dieringer (@cdaringe)
overview
A view module for intelligently rendering and validating selectbox input. Works well with ampersand-form-view.
install
npm install ampersand-select-view
Part of the Ampersand.js toolkit for building clientside applications.
API Reference
clear() - [Function] - returns this
Alias to calling setValue(null, true)
. Sets the selected option to either the unselectedText option or a user defined option whose value is null
. Be mindful that if no unselectedText or null
option exists, the view will error.
reset() - [Function] - returns this
Sets the selected option and view value to the original option value provided during construction.
setValue([value, skipValidationMessage]) - [Function] - returns this
Sets the selected option to that which matches the provided value. Updates the view's .value
accordingly. SelectView will error if no matching option exists. null
, undefined
, and ''
values will preferentially select unselectedText if defined.
constructor - [Function] new SelectView([options])
options
general options
name
: the<select>
'sname
attribute's value. Used when reporting to parent formparent
: parent form referenceoptions
: array/collection of options to render into the select box[groupOptions]
: use instead ofoptions
to generate<optgroup>
elements within your<select>
. If this is set, any values passed inoptions
will be ignored and replaced with values coming fromgroupOptions
.[el]
: element if you want to render the view into[template]
: a custom template to use (see 'template' section, below, for more)[required]
: [default:false
] field required[eagerValidate]
: [default:false
] validate and show messages immediately. Note: field will be validated immediately to provide a true.valid
value, but messages by default are hidden.[unselectedText]
: text to display if unselected[value]
: initial value for the<select>
.value
must be a member of theoptions
set[tabindex]
: [default:0
] Specify the tab index number for your field (integer).
label & validation options
[label]
: [default:name
value] text to annotate your select control[invalidClass]
: [default:'select-invalid'
] class to apply to root element if invalid[validClass]
: [default:'select-valid'
] class to apply to root element if valid[requiredMessage]
: [default:'Selection required'
] message to display if invalid and required
collection option set
If using a collection to produce <select>
<option>
s, the following may also be specified:
[disabledAttribute]
: boolean model attribute to flag disabling of the option node[idAttribute]
: model attribute to use as the id for the option node. This will be returned bySelectView.prototype.value
[textAttribute]
: model attribute to use as the text of the option node in the select box[yieldModel]
: [default:true
] if options is a collection, yields the full model rather than just itsidAttribute
to.value
When the collection changes, the view will try and maintain its currently .value
. If the corresponding model is removed, the <select>
control will default to the 0th index <option>
and update its value accordingly.
custom template
You may override the default template by providing your own template string to the constructor options hash. Technically, all you must provided is a <select>
element. However, your template may include the following under a single root element:
- An element with a
data-hook="label"
to annotate your select control - An
<select>
element to hold youroptions
- An element with a
data-hook="message-container"
to contain validation messages - An element with a
data-hook="message-text"
nested beneath thedata-hook="message-container"
element to show validation messages
Here's the default template for reference:
1<label class="select"> 2 <span data-hook="label"></span> 3 <select></select> 4 <span data-hook="message-container" class="message message-below message-error"> 5 <p data-hook="message-text"></p> 6 </span> 7</label>
You may SelectView.extend({ template: ...})
to create a View definition with a more permanent default template of your liking as well.
example
1var FormView = require('ampersand-form-view'); 2var SelectView = require('ampersand-select-view'); 3 4module.exports = FormView.extend({ 5 fields: function () { 6 return [ 7 new SelectView({ 8 label: 'Pick a color!', 9 // actual field name 10 name: 'color', 11 parent: this, 12 // you can pass simple string options 13 options: ['blue', 'orange', 'red'], 14 // if included this will add option for an unselected state 15 unselectedText: 'please choose one', 16 // you can specify that they have to pick one 17 required: true, 18 // specify tab index for usability 19 tabindex: 1, 20 }), 21 new SelectView({ 22 name: 'option', 23 parent: this, 24 // you can also pass array, first is the value, second is used for the label 25 // and an optional third value can used to disable the option 26 options: [ ['a', 'Option A'], ['b', 'Option B'], ['c', 'Option C', true] ], 27 tabindex: 2, 28 }), 29 new SelectView({ 30 name: 'option', 31 parent: this, 32 // define groupOptions to generate <optgroup> elements. Pass it an array of 33 // Objects, each object will become an <optgroup> with groupName being the 34 // <optgroup>'s name and options being an array (either of strings or array, see 35 // previous two examples) that will become the <option>s under that <optgroup> 36 groupOptions: [ 37 { 38 groupName: "Options 1", 39 options: [ ['1', 'Option 1'], ['2', 'Option 2'], ['3', 'Option 3', true] ] 40 }, 41 { 42 groupName: "Options 2", 43 options: [ ['a', 'Option A'], ['b', 'Option B'], ['c', 'Option C', true] ] 44 } 45 ], 46 tabindex: 3, 47 }), 48 new SelectView({ 49 name: 'model', 50 parent: this, 51 // you can pass in a collection here too 52 options: collection, 53 // and pick an item from the collection as the selected one 54 value: collection1.at(2), 55 // here you specify which attribute on the objects in the collection 56 // to use for the value returned. 57 idAttribute: 'id', 58 // you can also specify which model attribute to use as the title 59 textAttribute: 'title', 60 // you can also specify a boolean model attribute to render items as disabled 61 disabledAttribute: 'disabled', 62 // here you can specify if it should return the selected model from the 63 // collection, or just the id attribute. defaults `true` 64 yieldModel: false, 65 tabindex: 4, 66 }) 67 ]; 68 } 69});
gotchas
-
Numeric option values are generally stringified by the browser. Be mindful doing comparisons. You'll generally desire to inspect
selectView.value
(the value of your selected options' input) overselectView.select.value
(the value returned from the browser).- Additionally, do not use option sets containing values that
==
one another. E.g., do not use options whose values are "2" (string) and 2 (number). Browsers cannot distinguish between them in the select control context, thus nor can ampersand-select-view.
- Additionally, do not use option sets containing values that
-
null
,undefined
, or''
option values are not consideredvalid
when the field is required. This does not apply when options are from a collection andyieldModel
is enabled.- The
unselectedText
option will always be preferred in updating the control to an empty-ish value.
- The
browser support
changelog
-
7.0.0
- Upgrade to &-view 9.x
-
6.2.1
- Added the
tabindex
option to allow custom tab-ordering of fields for usability
- Added the
-
6.2.0
- Support extending
template
- Support extending
-
6.1.0
- Generate
<optgroup>
elements by passing the newoptions.groupOptions
parameter
- Generate
-
6.0.0
- Match field label rendering behavior to ampersand-input-view. removes label fallback to
name
attr - Improve x-browser testing CI
- Match field label rendering behavior to ampersand-input-view. removes label fallback to
-
5.0.0
- Change events now always get triggered on the select element instead of blindly calling on the root element.
-
4.0.0
- Extend ampersand-view and support
autoRender
, where previously this view would autoRender unconditionally
- Extend ampersand-view and support
-
3.0.0
- Improve general option edge cases, and add supporting test cases. Primarily targets falsy option value handling.
- Validate immediately to assist when parent FormView tests onload for field validity. Update
skipValidation
toskipValidationMessage
, permit immediate validation, but conditionally display messages. - Throw an
Error
when trying tosetValue(value)
and an option matching the requestedvalue
does not exist. The exception to this is when the provided value isnull
,undefined
, or''
, and anull
option value exists. Because the DOM can only recognize a single empty value for any<option>
, which is the empty string''
, only a single empty-ish option can only be supported by the view. - Support
0
value options, both in Model id's and array values. - Add
eagerValidate
. - Denote a plan for 4.x release
- Bulk update README, and some cody tidying
credits
Originally designed & written by @philip_roberts.
license
MIT
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
0 existing vulnerabilities detected
Reason
license file detected
Details
- Info: project has a license file: LICENSE.md:0
- Info: FSF or OSI recognized license: MIT License: LICENSE.md:0
Reason
Found 5/18 approved changesets -- score normalized to 2
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
- Warn: no security policy file detected
- Warn: no security file to analyze
- Warn: no security file to analyze
- Warn: no security file to analyze
Reason
project is not fuzzed
Details
- Warn: no fuzzer integrations found
Reason
branch protection not enabled on development/release branches
Details
- Warn: branch protection not enabled for branch 'master'
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
- Warn: 0 commits out of 17 are checked with a SAST tool
Score
3.3
/10
Last Scanned on 2025-02-03
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn MoreOther packages similar to ampersand-select-view
ampersand-multiselect-view
A view module for intelligently rendering and validating multiple selectbox input. Works well with ampersand-form-view. Based off of ampersand-select-view.
ampersand-bootstrap-tagsinput-view
Ampersand select/input view for creating tags in Bootstrap 2/3.
ampersand-date-view2
A view module for user date input. This form displays all fields as inputs. Fork of https://github.com/mikehedman/ampersand-date-view which uses a select for month.