首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使我的删除条目按钮功能

如何使我的删除条目按钮功能
EN

Stack Overflow用户
提问于 2021-12-29 16:45:26
回答 1查看 28关注 0票数 0

请原谅我的新手,因为我对编码不熟悉。我一直试图让我的删除按钮工作在我的网页和mysql,但我的删除按钮只是带我到一个url /delete页面。我没有收到任何错误,但是我的“确认删除”按钮没有删除该条目,而是将我带到另一个空白页。如果能提供任何建议,我们将不胜感激。这是一些代码,如果需要更多的代码,请告诉我。再次感谢。

代码语言:javascript
复制
<template>
  <div class="between:flex bottom:margin-3">
    <div class="center: flex-items">
      <span class="right:margin-1">Show</span>
      <select v-model="currentEntries" class="select" @change="paginateEntry">
        <option v-for="se in showEntries" :key="se" :value="se">
          {{ se }}
        </option>
      </select>
      <span class="left:margin-1">Entries</span>
      <div class="end:flex"></div>
    </div>
  </div>
  <div id="tableHolderDiv">
    <table class="table table-striped">
      <thead>
        <tr>
          <th scope="col">ID</th>
          <th scope="col">Location</th>
          <th scope="col">End User</th>
          <th scope="col">Order Number</th>
          <th scope="col">Date</th>
          <th scope="col">Application</th>
          <th scope="col">Service Tech</th>
          <th scope="col">Department</th>
          <th scope="col">Hours</th>
          <th scope="col">Travel Hours</th>
          <th scope="col">Contact Name</th>
          <th scope="col">Reason</th>
        </tr>
      </thead>
      <tbody>
        <tr
          v-for="row in serviceEntries"
          :key="row.id"
          @click="alertID(row.id)"
        >
          <th scope="row">{{ row.id }}</th>
          <td>{{ row.location }}</td>
          <td>{{ row.end_user }}</td>
          <td>{{ row.order_number }}</td>
          <td>{{ row.date }}</td>
          <td>{{ row.application }}</td>
          <td>{{ row.service_tech }}</td>
          <td>{{ row.department }}</td>
          <td>{{ row.hours }}</td>
          <td>{{ row.travel_hours }}</td>
          <td>{{ row.contact_name }}</td>
          <td>{{ row.reason }}</td>
          <a
            href="/delete/{{this.id}}"
            type="button"
            class="btn btn-light btn-small"
            onclick="event.stopPropagation(); return confirm('Are you sure you want to delete this entry?');"
            ><i class="bi bi-trash"></i> Delete</a
          >
        </tr>
      </tbody>
    </table>
  </div>
</template>

<script>
import axios from "axios";

export default {
  name: "ServiceTable",
  computed: {
    serviceEntries() {
      return this.$store.state.serviceEntries;
    },
  },
  methods: {
    alertID(id) {
      this.$router.push({
        path: `/modify/${id}`,
      });
    },
  },
  async onDelete() {
    if (!confirm("Are you sure you want to delete this entry?")) {
      return;
    }
    await axios.delete(`/api/service/delete/${this.id}`);
  },
  alertID(id) {
    this.$router.push({
      path: `/modify/${id}`,
    });
  },
};
</script>

这在我的index.js里

代码语言:javascript
复制
 async deleteServiceEntry() {
      return new Promise((resolve, reject) => {
        axios({
          method: "delete",
          url:
            prefix +
            "/api/service/delete/",
          headers: { "Content-Type": "application/json" }
        })
          .then(function(response) {
            //handle success
            console.log(response);
            resolve(true);
          })
          .catch(function(response) {
            //handle error
            console.log(response);
            reject(false);
          });
      });
  },

这在我的app.js里

代码语言:javascript
复制
app.delete("/api/service/:id", (req, res) => {
  // console.log("DELETE /api/user/" + req.params.id + " called");
  pool.query(
    "DELETE FROM `master` WHERE id = ?",
    [req.params.id],
    function (error, results, fields) {
      if (error) res.json(error);
      else res.json(results);
    }
  );
});

app.delete("/api/service/:id", (req, res) => {
  pool.query(
    "DELETE FROM `master` WHERE id= ? AND location = ? AND end_user = ? AND order_number = ? AND date = ? AND application = ? AND service_tech = ? AND department = ? AND hours = ? And travel_hours = ? AND contact_name = ? AND reason = ?",
    [req.params.id, req.params.location, req.params.end_user, req.params.order_number, req.params.date, req.params.application, req.params.service_tech, req.params.department, req.params.hours, req.params.travel_hours, req.params.contact_name, req.params.reason],
    function (error, results, fields) {
      if (error) res.json(error);
      else res.json(results);
    }
  );
});
EN

回答 1

Stack Overflow用户

发布于 2021-12-29 17:07:21

由于锚标签中的href链接,它正在重定向到一个新页面。您应该在单击时调用方法deleteServiceEntry,它应该工作得很好。你能做的是:

代码语言:javascript
复制
              <a
                href="#"
                type="button"
                class="btn btn-light btn-small"
                @click="deleteServiceEntry($event,row.id)"
                ><i class="bi bi-trash"></i> Delete</a
              >

然后在方法上:

代码语言:javascript
复制
 async deleteServiceEntry(event,id) {
  event.preventDefault();
  const verify = confirm("Are you sure you want to delete this entry?");
  if (verify) {
   return new Promise((resolve, reject) => {
    axios({
      method: "delete",
      url:
        prefix +
        "/api/service/delete/"+id,
      headers: { "Content-Type": "application/json" }
    })
      .then(function(response) {
        //handle success
        console.log(response);
        resolve(true);
      })
      .catch(function(response) {
        //handle error
        console.log(response);
        reject(false);
      });
  });

},}

希望这能帮上忙。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70522416

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档