Transfer the text from one cell and insert it into the "neighbor" cell of a different column when the content is editable

present situation: Clicking on a row fills the entire row of another column, instead of just filling a single row.

<p
  class="p-of-that"
  v-html="thatText"
  contenteditable
  @click="writeThat(myArr, $event)"
></p>

It should somewhat resemble this when clicking on a row in That's column

https://i.stack.imgur.com/HUgfz.png

The same issue occurs with 'Johan'. Clicking on a row displays 'Johan' and the text 'Michael' remains

Here is the reproduction: https://codesandbox.io/s/boring-dirac-mk3sk

Click on a row within the 'That' column


I hope this aligns with the broader context

Answer №1

It's uncertain if the issue will be resolved, but an attempt will be made to provide guidance. Initially, consider working with a two-dimensional array like this:

const myTable = [
    [myObject1, myObject2],
    [myObject3, myObject4]
];

Alternatively, utilize objects with designated space for the second column:

const myTable = [
    {
        id, column1, column2
    }
];

Subsequently, clicking on the second column should result in values from column1 being transferred to column2. Update the array accordingly to complete the process!

Implementation of Suggestions

<template>
  <div id="app">
    <table border="1">
      <thead>
        <th>This</th>
        <th>That</th>
      </thead>
      <tbody>
        <tr v-for="(myArr, index) in myArrs" :key="index">
          <td style="width: 120px">{{ myArr.column1.name }}</td>
          <td style="width: 120px">
            <p class="p-of-that" contenteditable @click="writeThat(index)">
              {{ myArr.column2.name }}
            </p>
          </td>
        </tr>
      </tbody>
    </table>
  </div>
</template>

<script>
export default {
  name: "App",

  data: () => ({
    myArrs: [
      {
        column1: { id: 1, name: "Michael" },
        column2: { id: 1, name: "" },
      },
      {
        column1: { id: 2, name: "Johan" },
        column2: { id: 2, name: "" },
      },
    ],
  }),

  methods: {
    writeThat(index) {
      this.myArrs[index].column2 = this.myArrs[index].column1;
    },
  },
};
</script>

<style>
#app {
  font-family: "Avenir", Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-align: center;
  color: #2c3e50;
  margin-top: 60px;
}
</style>

Answer №2

When you click on a row, make sure to pass only the myArr.name parameter to the writeThat function instead of passing the entire myArr object.

Additionally, include an if statement to compare the current value of thatText with myArr.name.

<table border="1">
    <thead>
        <th>This</th>
        <th>That</th>
    </thead>
    <tbody>
        <tr v-for="myArr in myArrs" :key="myArr.id">
            <td style="width: 120px">{{ myArr.name }}</td>
            <td style="width: 120px" @click="writeThat(myArr.name, $event)" contenteditable>
                <p
                    v-if="thatText === myArr.name"
                    class="p-of-that"
                    v-html="thatText"
                ></p>
             </td>
         </tr>
    </tbody>
</table>

Also, within the writeThat function, update the value of thatText as follows:

const writeThat = (data, e) => {
    thatText.value = data
    console.log(e);
};

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Im testing the creation of a global style using styled-components

Is there a way to create snapshot tests for styled-components with createGlobalStyle? The testing environment includes jest v22.4.4, styled-components v4.1.2, react v16.7, jest-styled-components v5.0.1, and react-test-renderer v16.6.3 Currently, the outp ...

Obtaining Navigation Parameters within Custom React Navigation Title

In the React Navigation StackNavigator, I created a custom title that looks like this: const CustomStackNavigator = StackNavigator({ Home: { screen: HomeScreen } }, { navigationOptions: { headerTitle: <GradientHeader title={this.props.nav ...

Encountering an issue with a class component stating that "this.setState is not a function

I am currently learning React JS and encountered an error when calling my first API in React. I keep getting the message "Unhandled Rejection (TypeError): this.setState is not a function." I have been trying to troubleshoot this issue on my own but haven ...

Inject JavaScript Object Information into Bootstrap Modal

After iterating through each object and assigning its data to an individual div along with a button, I encountered an issue. When testing the buttons, I noticed that only the last object's data was displayed in all of the modal windows. Any suggestion ...

When a radiobutton is clicked, a jQuery call to a PHP function triggers an AJAX request which results in a JavaScript function becoming unrefer

Currently, I have a situation where two radio buttons are representing different products. When one of them is clicked, the goal is to update the price displayed on the website based on the selected product. Everything seems to be working fine when using t ...

Attempting to load using ajax, Chrome and jquery-mobile will modify the location of the window through setting window.location.href

I'm encountering a challenge with my mobile application where I need to redirect users to the login page on a 401 ajax call. However, it seems that jQM is attempting to load this via AJAX when the request is sent. This solution works flawlessly for s ...

Interactive HTML5 canvas: Dragging and dropping multiple items

I'm currently working on a project that involves allowing users to drag and drop multiple objects within a specified area. To achieve this, I am utilizing the html5 canvas tag. While the functionality works smoothly when dragging and dropping a single ...

"Is there a virtual keyboard available that supports multiple inputs and automatically switches to the next input when the maximum length

Looking to build a virtual keyboard with multiple inputs that automatically switch to the next input field after reaching the maximum length. Currently having an issue where I can only fill the first input and not the second one. Any assistance in resolvin ...

Exploring the possibilities of integrating vuex into Vue 3

In the past year, I dedicated time to learning Vue 2 and found it very enjoyable. However, I didn't proceed with a project at that time. Now, I have the opportunity to start a new project and would like to utilize Vue 3 along with the composition API, ...

Revolutionary AJAX technology seamlessly updates and refreshes elements within every row or form on a webpage

<script type="text/javascript"> $(document).ready(function() { $("#submit<?php echo $resultshome['hskid']; ?>").click(function() { $.ajax({ type : "POST", url : "/scores/printPOST.php", data : { "subro ...

Guide to displaying a progress bar while submitting a form using JavaScript and AJAX

After successfully creating the progress bar for my project, I encountered a technical issue. When I choose to upload a file, it gets uploaded twice - once when selecting the file and then again when submitting the form. This creates a poor user experience ...

Iterating through a JSON array using the JQuery .each method

Greetings! I'm trying to figure out how to access the msg -> items using jQuery's .each() method. { "msg": [ { "msg_id": "18628", "msg_userid": "12", "msg ...

The function has exceeded the time limit of 60000 milliseconds. Please make sure that the callback is completed

Exploring the capabilities of Cucumber with Protractor has been an intriguing journey for me. As I delved into creating a feature in Gherkin language, outlining the steps and scenarios necessary for my end-to-end tests, a new world of possibilities opened ...

Creating a hover effect for a specific card while maintaining the rest of the cards unaffected

When hovering over a card, all icons are displayed instead of just the icons for that specific card. I want to show only the icons for the card being hovered on (example output: image). The cards are generated automatically using v-for and fetching data fr ...

The setState function in React Native seems to be having trouble updating

I've been attempting to access state from a component, but for some reason, I'm not seeing any changes when I use setState(). Here is the current state of my component: class MyTestComponent extends React.Component { constructor(props){ ...

Leveraging Vue.js plugin within a single file component

I am looking to utilize the vue-chartkick plugin in my Vue application, but I want to register it within my single-file components instead of globally. Is there a way to achieve this same functionality without using Vue.use(VueChartkick, { Chartkick })? ...

Discovering the process of retrieving API data upon a button click within Next.js using server-side rendering

Hi there, I'm fairly new to working with next.js and would greatly appreciate some assistance. I am trying to figure out how to fetch data from an API upon clicking a button in next.js on the server side. I understand that using onClick directly is ...

Retrieve Gridview properties using JavaScript

I need to adjust the font size of my gridview using JavaScript to make it more suitable for printing. What is the best way to change the font size specifically for a gridview using JavaScript? ...

ReactJS Error: Attempting to access undefined property "updateNumber"

Just getting my feet wet with js and React, attempting to create a simple Sudoku program. Encountering an issue when adding UpdateNumberInCell={this.updateNumber} as a property in the Cell component - receiving the error: "Uncaught TypeError: Cannot read ...

Move information from one webpage to a different page

After developing a site using NextJs, I successfully integrated Discord login functionality and was able to retrieve the user's guilds in the oauth file. Now, I am looking to send this list of guilds (in JSON format) to my dashboard page. In the oau ...