jquery 无法使用HTML CSS和JS添加具有功能的符号

aiqt4smr  于 2023-03-17  发布在  jQuery
关注(0)|答案(1)|浏览(150)

我创建了一个有三列的表格。第一列命名为“操作”,第二列命名为“问题子问题/文本”,第三列命名为“排序顺序”。此外,我还添加了一个功能,我们可以拖放行。
我的代码如下:

<!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 Experince</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>1</td>
            <td>Question1</td>
            <td>1</td>
        </tr>
        <tr>
            <td>2</td>
            <td>Question2</td>
            <td>2</td>
        </tr>
        <tr>
            <td>3</td>
            <td>Question3</td>
            <td>3</td>
        </tr>
        <tr>
            <td>4</td>
            <td>Question4</td>
            <td>4</td>
        </tr>
        <tr>
            <td>5</td>
            <td>Question5</td>
            <td>5</td>
        </tr>
        <tr>
            <td>6</td>
            <td>Question6</td>
            <td>6</td>
        </tr>
        <tr>

            <td>7</td>
            <td>Question7</td>
            <td>7</td>
        </tr>
        <tr>
            <td>8</td>
            <td>Question8</td>
            <td>8</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>

现在,我想添加三个以上的符号与功能删除,编辑和查看“行动”列,而不是数字提到的上述代码。
删除符号将删除整组行和查看和编辑将打开另一个页面进行编辑和查看。我是新的在这方面。需要帮助来做到这一点。

6tr1vspr

6tr1vspr1#

这是一个jQuery的例子,如果你的应用增长,你可能会受益于像React这样的JS框架。
请注意,数据不会持久化--如果您想保持状态,则需要将其保存到数据库等。

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

CSS未变更

$(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);
        }
      });
    }
  });
});

相关问题