directionKey.js 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. export default {
  2. data() {
  3. return {}
  4. },
  5. methods: {
  6. handleKeyDown(event, scope, type) {
  7. // ↑
  8. if (event.keyCode === 38) {
  9. if (type === 'select') {
  10. return;
  11. }
  12. if (scope.$index > 0) {
  13. this.changeFocus(scope.$index - 1, scope.column.property);
  14. }
  15. }
  16. // ↓
  17. if (event.keyCode === 40) {
  18. if (type === 'select') {
  19. return;
  20. }
  21. if (scope.$index < this.addList.length) {
  22. this.changeFocus(scope.$index + 1, scope.column.property);
  23. }
  24. }
  25. // ->
  26. if (event.keyCode === 39) {
  27. if (type === 'select') {
  28. let refName = 'r' + scope.$index + scope.column.property
  29. this.$refs[refName].blur();
  30. }
  31. let y = this.labelArr.indexOf(scope.column.property)
  32. if (y > -1) {
  33. this.changeFocus(scope.$index, this.labelArr[y + 1]);
  34. }
  35. }
  36. // <-
  37. if (event.keyCode === 37) {
  38. if (type === 'select') {
  39. let refName = 'r' + scope.$index + scope.column.property
  40. this.$refs[refName].blur();
  41. }
  42. let y = this.labelArr.indexOf(scope.column.property)
  43. if (y < this.labelArr.length) {
  44. this.changeFocus(scope.$index, this.labelArr[y - 1]);
  45. }
  46. }
  47. },
  48. changeFocus(row, columnProperty) {
  49. let refName = 'r' + row + columnProperty
  50. this.$nextTick(() => {
  51. let ref = this.$refs[refName];
  52. if (ref) ref.focus();
  53. });
  54. },
  55. }
  56. }