Having trouble incorporating symbols with functionalities using HTML, CSS, and JS

I have implemented a table with three columns: "Action", "Question Subquestion/Text", and "Sort order". Additionally, I have incorporated a feature that allows for drag and drop functionality on the rows.

Below is my code snippet:

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible"
        content="IE=edge">
    <meta name="viewport"
        content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<style>
    table th,
    table td {
        width: 50px;
        padding: 5px;
        border: 0.5px solid black;


    }

    .GFG {
        color: green;
    }

    .OK {
        font-size: 18px;
    }
</style>

<body>
    <h2 class="GFG">Overall Experience</h2>
    <p class="OK">Drag and Drop Table Row</p>
    <table id="GFG">
        <tr>
            <td>Action</td>
            <th>Question Subquestion/Text</th>
            <th>Sort Order</th>
        </tr>
        <tr>
            <td>Delete</td>
            <td>Edit</td>
            <td>View</td>
        </tr>
        <tr>
            <td>Symbol1</td>
            <td>Question1</td>
            <td>1</td>
        </tr>
        <tr>
            <td>Symbol2</td>
            <td>Question2</td>
            <td>2</td>
        </tr>
        <tr>
            <td>Symbol3</td>
            <td>Question3</td>
            <td>3</td>
        </tr>
        <tr>
            <td>Symbol4</td>
            <td>Question4</td>
            <td>4</td>
        </tr>
    </table>
    <script type="text/javascript" src=
"https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js">
    </script>
    <link rel="stylesheet"
        href=
"https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.24/themes/smoothness/jquery-ui.css" />
    <script type="text/javascript"
        src=
"https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.24/jquery-ui.min.js">
    </script>
    <script>
        $(function () {
            $("#GFG").sortable({
                items: 'tr:not(tr:first-child)',
                dropOnEmpty: false,
                start: function (G, ui) {
                    ui.item.addClass("select");
                },
                stop: function (G, ui) {
                    ui.item.removeClass("select");
                    $(this).find("tr").each(function (GFG) {
                        if (GFG > 0) {
                            $(this).find("td").eq(2).html(GFG);
                        }
                    });
                }
            });
        });
    </script>

</body>

</html>

Currently, I am looking to enhance the functionality by replacing the numbers in the "Action" column with symbols for Delete, Edit, and View. The delete symbol will allow users to remove the entire row, while the edit and view symbols will direct them to another page for editing and viewing purposes. As a newcomer to this, any assistance in implementing these changes would be greatly appreciated.

Answer №1

When working with jQuery, it's important to consider the scalability of your app. As it grows, you may find that using a JavaScript framework like React could be beneficial.

Keep in mind that the data will not persist unless saved to a database. If you want to maintain the state, proper storage is necessary.


<h2 class="GFG">Overall Experience</h2>
<p class="OK">Drag and Drop table row</p>
<table id="GFG"></table>
<button id='reset'>Reset</button>

CSS remains unchanged

$(document).ready(() => {
  // variable to hold the top row of the table
  const tableHeader = `
    <tr>
      <td>Action</td>
      <th>Question Subquestion/Text</th>
      <th>Sort order</th>
      <th>Delete</th>
      <th>View</th>
      <th>Edit</th>
    </tr>
  `

  // initialise the list of questions - this may come from an API or external file
  let questions = [
      {id: 1, question: 'Question1' },
      {id: 2, question: 'Question2' },
      {id: 3, question: 'Question3' },
      {id: 4, question: 'Question4' },
      {id: 5, question: 'Question5' },
      {id: 6, question: 'Question6' },
      {id: 7, question: 'Question7' },
      {id: 8, question: 'Question8' },
    ]; 
  
  // the function to call to rebuild the table, it makes a list of rows from
  // the questions variables and prepends the top row to make the table
  // this will be slower the more rows you have 

  // the delete button is clicked via an on click event handler below
  // view and edit are simple links that open in new tabs, change the 
  // href attribute to what you want to show
  function buildTable() {
    const list = questions.map((q, index) => {
      return (`
          <tr>
            <td>${q.id}</td>
            <td>${q.question}</td>
            <td>${index+1}</td>
            <td class='delete' data-id='${q.id}'>Delete</td>
            <td class='view'><a target="_blank" href='/view/${q.id}'>View</a></td>
            <td class='edit'><a target="_blank" href='/edit/${q.id}'>Edit</a></td>
          </tr>
        `)
    })
    $('table#GFG').html(tableHeader + list.join(''))
  }
  
  // call the buildTable function to make the table when the page loads
  buildTable()
  
  // when the delete button is clicked, delete the row associated, then rebuild
  // the table when the row is deleted to show changes
  $('table#GFG').on('click', 'td.delete', (e) => { 
    questions = questions.filter(q => {
      if (q.id != e.target.dataset.id) { return true }
    })
    buildTable() 
  })
  
  // reset the questions to the default state and rebuild table 
  $('button#reset').on('click', () => {
    questions = [
      {id: 1, question: 'Question1' },
      {id: 2, question: 'Question2' },
      {id: 3, question: 'Question3' },
      {id: 4, question: 'Question4' },
      {id: 5, question: 'Question5' },
      {id: 6, question: 'Question6' },
      {id: 7, question: 'Question7' },
      {id: 8, question: 'Question8' },
    ]
    buildTable()
  })
});


// did not change this 
$(function () {
  $("#GFG").sortable({
    items: 'tr:not(tr:first-child)',
    dropOnEmpty: false,
    start: function (G, ui) {
      ui.item.addClass("select");
    },
    stop: function (G, ui) {
      ui.item.removeClass("select");
      $(this).find("tr").each(function (GFG) {
        if (GFG > 0) {
          $(this).find("td").eq(2).html(GFG);
        }
      });
    }
  });
});

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

Updating and Preserving Content in Angular

I've encountered an issue while working on a code that allows users to edit and save a paragraph on the screen. Currently, the editing functionality is working fine and the save() operation is successful. However, after saving, the edited paragraph do ...

Can next.js rewrites be configured with environment variables?

Currently, I am in the process of developing my application with a next.js frontend and express.js backend. During development, I simply run the relevant dev servers in the terminal. However, now I am looking to deploy my app using Docker for production. I ...

Duplicate table row and insert new cell

I am currently attempting to duplicate a row from one table into a new table and then include an additional td at the end of the copied row containing a delete button. Thus far, I have successfully duplicated the row but am struggling to find a method for ...

Removing a document from Firestore using React

Is there a way to delete a document in Firestore without knowing the document ID? If I only have the name of the document, how can I go about deleting it? This is how I currently export the database: export const db = getFirestore(app) I feel like I nee ...

Is there any potential security vulnerability when using ActiveRecord::Serialization.from_json() in RoR to parse uploaded JSON?

Currently in the process of setting up an export-import feature in Ruby on Rails. I have been contemplating the possibility of malicious code injection since JSON has the capability to include JavaScript. Is there a risk that someone could inject harmful ...

Issue regarding the slash format in credit card expiration dates

When entering the credit card expiration date, I am currently facing an issue. Every time I input two numbers, I need to manually add a slash (/) after them. However, if I try to delete the third number, it only removes one character. Is there a way to mak ...

Utilizing Javascript to Connect with Java Web API

I'm seeking assistance with transferring a file from a browser to another device connected to a server-operated machine. I am relatively new to web application and back-end programming. The current code allows for moving a file from the server to the ...

Refreshing infinitescroll plugin with updated URL link upon clicking

I am facing a situation where I need to rebind the infinite scroll plugin when a link is clicked, passing a new URL to differentiate the content section that will be generated later by it. Currently, I am able to trigger the infinite scroll and pass the s ...

The expandable row remains open even after filtering the table with JavaScript

An expandable row in the table displays details of rows. I have implemented a JavaScript search and filter function to find specific row content. However, when using the search filter, it successfully shows results but does not expand the details as intend ...

Select a division and retrieve the identification of a different division

I am looking to trigger an event by clicking on one element and extracting the ID from a separate, unrelated div. Here is my attempt: $(".map_flag").on("click",function(){ var selectedID = ($(this).attr("data_modal")); $("#" + selectedID).fade ...

Using Python's BeautifulSoup library to extract tables from archived HTML webpages

I'm currently working on extracting data from saved HTML webpages using Python 2.7 + Windows. There are several similar saved HTML webpages, each containing a table with 5 columns and an unspecified number of rows. The source code appears as follows ...

Experimenting with a Jest test on an express middleware

I'm currently faced with a challenge in testing my controller (express middleware) using Jest. To better illustrate the issue, I will share the code snippet below: import request from 'utils/request'; import logger from 'config/logger& ...

An error is occurring in asp.net where the ajax call is returning an undefined object

Here is the C# getter method that retrieves meanings of a word input by a user through a query. The meanings are then filled in a data table and added to a list of strings using a loop. Finally, the list is converted into a JSON string and returned: publ ...

Utilizing jQuery for AJAX to send data variables

Is it feasible to pass a variable to jQuery using the .ajax method? Instead of the traditional way, can I do something like this: <button onclick="sendQuery()"> function sendQuery(){ $.ajax({ type: "GET", url: "action.php", ...

Having trouble showing the information in JavaScript

Here is some code snippet: if(data.error) { msg1.textContent=data.error } else { msg1.textContent=data.location msg2.textContent=data.forecast console.log(data.forecast) } }) Unfortunately, I'm facing an is ...

Avoiding mistakes in class and id names with preventative tools

Is there a simple way to spot typos in HTML/CSS/JavaScript code? I'm looking for basic debugging tools that can catch mistakes like "#fron" instead of "#from" in class names and ids. I'm aware of more advanced IDEs, but I'm curious if there ...

"Enhance your data visualization with Highcharts Windbarb and effortless AJAX data

Alright. Let's rephrase a question that hasn't been answered yet. I've got a chart below with data loading dynamically via ajax. A simplified version (without ajax) of this graph works well as shown in this jsfiddle. The code below represe ...

Signing up for event using react leaflet

I've been working on integrating React-Leaflet into my Create React App. I've successfully overlaid GeoJSON data on a base map, but I'm struggling to register clicks on this layer. While troubleshooting this problem, I came across a helpful ...

Is there a way for me to send a URL request from my website to a different server using PHP?

I am looking to initiate a request from my website to my server using a specific IP address. From there, I need the server to send another request to a different server and then display the response on the webpage. Can anyone provide guidance on how to ...

Interacting between two occurrences of the identical Angular component

Within a Razor view, I include an angular component: <my-widget id="myWidget" isfullscreen="false" class="some-class"></my-widget> Upon clicking the 'Popup' button, a popup appears in an iframe and the s ...