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

The function Map() in React-Leaflet cannot be executed, despite the presence of data

I am attempting to replicate the React-Leaflet example of a list of markers. I have an array of objects that I am passing to MarkerList to be transformed into Fragments and displayed on the map. However, my mapping function is not functioning as expected ...

Compatibility issues between XMLHttpRequest and curl

Today, I am attempting to create a small XHR in JavaScript, Java, and C#, but it's not working for some reason... Below is the code snippet: var xhr = new XMLHttpRequest(); function init(){ xhr.open("POST","http://www.opsu.gob.ve/portal/controles/ ...

Display current weather conditions with the Open Weather API (featuring weather icons)

Hello everyone, I need some help from the community. I am currently working on a weather app using the openweather API. However, I'm facing an issue with displaying the weather conditions icon for each city. I have stored every icon id in a new array ...

When I remove the `return false` statement in my code, my AJAX call executes successfully. However, keeping it in prevents the call from

I am using jQuery to send an AJAX POST request without refreshing the page. I have tried using the return false command to prevent the page from refreshing, but then the AJAX POST request doesn't go through. If I remove the return false command, the A ...

Issue with Angular 5 - Deselect all checkboxes not reflecting in the UI

I am currently working on integrating a reset button into a Reactive form in Angular 5. The reset functionality works flawlessly for all form fields, except for the dynamically created multiple checkboxes. Although it seems like the reset operation is hap ...

Is it possible for Typescript to resolve a json file?

Is it possible to import a JSON file without specifying the extension in typescript? For instance, if I have a file named file.json, and this is my TypeScript code: import jsonData from './file'. However, I am encountering an error: [ts] Cannot ...

Having trouble with creating a new Next.js app using the latest version with npx?

Having some difficulty with the "npx create-next-app@latest" command while setting up Next.js. As a newcomer to both the community and Next.js, I could use some assistance in resolving this problem. Upon running the command, an unfamiliar message was displ ...

JavaScript Mouseover Custom Trigger with d3.js Library

Currently, I am experimenting with d3.js and am interested in creating a custom event with a unique trigger. From my understanding, the 'mouseover' event occurs when the mouse pointer hovers over a specific element both horizontally and vertical ...

Having trouble reading the file using jQuery in Internet Explorer 8 and earlier versions due to its non-XML format (albeit resembling XML)

Currently, I am utilizing AJAX to load a KML file (which essentially functions as an XML file). The parsing works seamlessly in IE9, FF, and other browsers, but encounters issues in IE8. Although the data is retrieved, I face difficulties parsing it in jQu ...

Nuxt 3: Leveraging SSR for Client-Side Data Refetching with useFetch

My current project involves building a simple SSR site with the help of useFetch. Everything runs smoothly when I hardcode the URL, but once I switch to using a runtime config variable (from the .env file), the fetch operation works on the server side with ...

What are some strategies for creating a recursive function in JavaScript that avoids exceeding the maximum call stack size error?

I need assistance creating a walking robot function in JavaScript, but I am encountering a call stack size error. function walk(meter) { if(meter < 0) { count = 0; } else if(meter <= 2) { count = meter; ...

A more intelligent approach for generating JSON responses using Mysql

Here is the code I have on my server side using Node.js: var mysql = require('mysql'); var connection = mysql.createConnection({ host: 'localhost', user: 'SOMEUSER', password: 'SOMEPASSWD', database: 'SOMED ...

Leveraging the power of `.vue` files in modern web browsers that have native support for

Chrome 60 and the most recent version of Firefox, when used with certain flags enabled, have implemented support for import/export directives. I am interested in utilizing .vue files without relying on NodeJS, but I haven't been able to figure out how ...

Ensure that the jQuery Knob is set to submit any modifications only after the final adjustment

Utilizing jQuery Knob by anthonyterrien, I have configured the step to be 20. My goal is to transmit the knob's value to the server. The issue arises when the change function is triggered every time the user adjusts the knob using either a right or le ...

What is the best method to create Promise API synchronously?

When it comes to testing with NodeJS, I rely on selenium-webdriver. My goal is to streamline the selenium-webdriver API by making it synchronous, which will result in more concise tests. The method getTitle() is used to retrieve the title of the current p ...

Click on the link to open it in a SharePoint modal and populate it with the value from the

After populating a ng-grid with SharePoint items, my goal is to have the SharePoint edit form open in a modal window when the edit button at the end of each row is clicked. However, I am encountering difficulties when using OpenPopUpPage as the {{row.entit ...

Encountered an Uncaught TypeError in VueJS 3 + Laravel: Unable to access the property 'component' as it is undefined

Greetings everyone, I am encountering a minor issue while trying to initiate Vue on my Laravel project. Below is my package.json configuration: "devDependencies": { "@fortawesome/fontawesome-svg-core": "^1.2.32", ...

Creating a password with two distinct numbers using regular expressions

In javascript I am struggling to create a password that meets the criteria of having at least eight characters, including two SEPARATE digits, one uppercase and one lowercase letter, as well as one special character (-, @, #, $, &, *, +) but not /, !, ? ...

Formik: encountering target as undefined while passing Material UI Date Picker as prop to React Component

I'm currently working on developing a component that can be reused. The idea is to pass form fields as props, but I ran into an issue when clicking on the datepicker field. The error message that pops up is: TypeError: target is undefined Any sugge ...

Do I have to divide the small functions in my Node.js controller into smaller ones?

When signing up users in my controller, do I need to break up the sign-up steps into individual asynchronous calls or is one big asynchronous call sufficient? Each step relies on the previous one: Validate user Create user Create group Add user to group ...