Open maedi opened 9 years ago
v-el
is a "literal directive".
Literal directives treat their attribute value as a plain string; ... Literal directives accept mustache expressions inside their value, but these expressions will be evaluated only once on first compile and do not react to data changes.
Generally speaking, DOM operations belong to directives, so I would write a custom directive, that focuses item on bind
: https://jsfiddle.net/simplesmiler/48jxvnzt/
Very nice solution, but | key enter
doesn't work. Replace it with | key 'enter'
and everything works great. :)
@simplesmiler Thank you! I'll wrap my head around the code.
I'd also like to move the focus up and down fields with the keyboard arrow keys. Is directives also the place for this kind of logic? I keep thinking of jQuery... but I know that will probably lead to spaghetti code.
Ye, jquery will of course lead to spaghetti code. :smile: I've extended @simplesmiler's example to give you a possible solution: https://jsfiddle.net/48jxvnzt/2/
If you don't want to use any global functions or global directives, you can do it like this: https://jsfiddle.net/48jxvnzt/3/
@swift1 you are correct with the | key 'enter'
, my cdn was pointing at 0.11.8 instead of the latest.
@maedi I would probably make focus
a part of the viewmodel, listen to the focus event of inputs, and introduce methods focusUp
and focusDown
.
Something like this: https://jsfiddle.net/simplesmiler/48jxvnzt/7/ Note the directive now takes a boolean value.
i guess now it is ref
and this.$refs
...
<h1 ref="heading">Hello World!</h1>
...
// Vue Instance
...
someMethod: function( ) {
this.$refs.heading; // this is the hook for the <h1> element above...
}
...
I'd like to create an input field that responds to a keyboard enter by adding a new field below and focusing the cursor on that field. However
v-el
doesn't appear to work on sub HTML elements, just the element that corresponds to the Vue.HTML
JS
Error
Uncaught TypeError: Cannot read property 'field1' of undefined
even though the object is there infields.$data.fields
and in the DOM.fields.$$.thisWorks
is accessible.Happy to hear a more Vue way of doing this using components etc.