0

I have a HTML table with 2 columns that looks like this:

Item       Amount
Icecream   $3
Hotdog     $5
Hamburger  $10

The "amount" column consists of <input> elements that live in a <td>

The user should be able to select the $3 field, (hold down control?) and then also select the $5 field, then type in $6 , causing the $3 and the $5 to be changed to $6.
I understand this is kind of 2 questions. I think once i know how to let the user select multiple inputs I will be able to figure out the typing part. I'm using Vue in case that helps

1 Answer 1

1

You must do a few things to achieve that:

  1. Listen to on click event, which is fairly easy in vuejs
<element @click="firstWay"/> // this will pass only the `$event` as an argument
<element @click="$event => secondWay($event, someOtherProperty)"/> // and this way, you can define your own arguments
methods: {
  firstWay ($event) { },
  secondWay ($event, someOtherProperty) {}
}
  1. Know when ctrl key is pressed (event.ctrlKey is there for you)
  2. Keep track of selected items - using your data object.

Example

Template

    <tr
      v-for="(item, idx) in items"
      :key="idx"
    >
      <td v-text="item.name"/>
      <td>
        <input
          type="number"
          :value="item.price"
          step="0.01"
          :selected="item.selected"
          @click="$event => onClick($event, item)"
          @input="$event => onInput($event, item)"
        />
      </td>
    </tr>

JavaScript

  data: {
    items: [
        { name: 'Icecream',     price: 3.14, selected: false },
        { name: 'Hotdog',       price: 6.00, selected: false },
        { name: 'Hamburger',    price: 2.00, selected: false }
    ]
  },
  methods: {
    onClick (event, item) {
      if (event.ctrlKey) {
        item.selected = !item.selected
      }
    },

    onInput (event, item) {
      const value = event.target.value
      item.price = value

      if (!item.selected) return

      this.items
        .filter(it => it.selected)
        .forEach(it => {
            it.price = value
        })
    }
  }

Styles

input[selected] {
  outline: 1px solid red;
}

Here you play with working demo

Sign up to request clarification or add additional context in comments.

5 Comments

wow thank you for such a thorough answer. I'm on the fiddle now and am unable to select multiple HTML holding down Control. I'm on a mac using Chrome. Am I doing something wrong?
You should be able to select multiple inputs by clicking on them while holding ctrl key. I've just tested it on chrome (linux-83.0.4103.61), firefox (linux - 68.8.0esr) and opera (windows-68.0.3618.125), i don't have any Mac rn. Perhaps the outline styles are being overridden by your browser. Is it only graphical issue, or the input logic is not working as well?
when i click on an input holding the control key a pop up appears with options such as copy / paste
Looks like some binding which is specific to your browser (or it's plugin?), os, etc. Perhaps using Event#preventDefault and / or Event#stopImmediatePropagation will help. Check this version of a fiddle.
unfortunately I'm still just getting the pop up! Can you try it with holding down shift instead maybe?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.