質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.46%
Vue.js

Vue.jsは、Webアプリケーションのインターフェースを構築するためのオープンソースJavaScriptフレームワークです。

Ruby on Rails 6

Ruby on Rails 6は、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

Q&A

解決済

1回答

2596閲覧

【Vue.js】確認モーダルを表示させた後に削除する方法

katahik

総合スコア79

Vue.js

Vue.jsは、Webアプリケーションのインターフェースを構築するためのオープンソースJavaScriptフレームワークです。

Ruby on Rails 6

Ruby on Rails 6は、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

0グッド

0クリップ

投稿2021/08/02 12:15

編集2021/08/02 12:17

概要

現在、画像のような一覧画面から削除ボタンクリックで"そのまま"削除する実装になっています。
イメージ説明
これを、「削除ボタン押下→確認モーダル表示→削除するボタン押下→削除 」のようにしたいです。

しかし、現在、実装したものからどのようにして、モーダルを表示させたあとに削除すればよいか検討がつきません。

こちらの方法について、知恵をお貸しいただければと思います。

もとの状態

・削除は正しく挙動する
・モーダル未実装

コード

Index.vue
<template> <main> <section> <div> <table> <thead> <tr> <th class="w-15">タイトル</th> <th class="w-25">詳細</th> </tr> </thead> <tbody> <tr v-for="latest_information in latest_informations"> <td>{{ latest_information.title }}</td> <td>{{ latest_information.detail }}</td> <td class="text-nowrap text-right"> <button class="btn btn-sm btn-icon mx-1" type="button" v-b-tooltip.hover title="編集" @click="$router.push({name: 'AdminLatestInformationEdit', params: {id: latest_information.id}})"> <i class="fas fa-pen"></i> </button> <button class="btn btn-sm btn-icon mx-1" type="button" v-b-tooltip.hover title="削除" @click="deleteLatestInformation(latest_information.id)"><i class="fas fa-trash"></i> </button> </td> </tr> </tbody> </table> </div> </section> </main> </template> <script> export default { data() { return { latest_informations: {}, } }, methods: { setData(query) { this.$loading.load(this.$auth.api.get(`admin/latest_informations.json`, {params: query}) .then(response => { this.latest_informations = response.data.latest_informations }) .catch(err => { this.$errorHandlers.initial(err); })); }, deleteLatestInformation(id) { this.$loading.load(this.$auth.api.delete(`admin/latest_informations/${id}.json`) .then(response => { this.$router.push({name: 'AdminLatestInformationIndex', query: {toast_type: 'deleted'}}) this.setData(this.$route.query) }) .catch(err => { this.$errorHandlers.initial(err); })); }, }, mounted() { this.setData(this.$route.query); }, } </script>

現在の状態

・削除機能自体は正しく挙動しない
【ログ】
Started DELETE "/api/v1/admin/latest_informations/undefined.json" for 127.0.0.1 at 2021-08-02 12:12:46 +0000

・モーダルは表示される
イメージ説明

コード

Index.vue
<template> <main> <section> <div> <table> <thead> <tr> <th class="w-15">タイトル</th> <th class="w-25">詳細</th> </tr> </thead> <tbody> <tr v-for="latest_information in latest_informations"> <td>{{ latest_information.title }}</td> <td>{{ latest_information.detail }}</td> <td class="text-nowrap text-right"> <button class="btn btn-sm btn-icon mx-1" type="button" v-b-tooltip.hover title="編集" @click="$router.push({name: 'AdminLatestInformationEdit', params: {id: latest_information.id}})"> <i class="fas fa-pen"></i> </button> <button class="btn btn-sm btn-icon mx-1" type="button" v-b-tooltip.hover title="削除" @click="$refs.delete_confirm_modal.open()"> <i class="fas fa-trash"></i> </button> </td> </tr> </tbody> </table> </div> </section> <confirm-modal ref="delete_confirm_modal" :body_text="'この最新情報を削除します。<br>よろしいですか?'" :onClickSend="deleteLatestInformation" :send_button_text="'削除する'" /> </main> </template> <script> import ConfirmModal from '../../../common/pages/ConfirmModal.vue'; export default { components: { ConfirmModal }, data() { return { latest_informations: {}, } }, methods: { setData(query) { this.$loading.load(this.$auth.api.get(`admin/latest_informations.json`, {params: query}) .then(response => { this.latest_informations = response.data.latest_informations }) .catch(err => { this.$errorHandlers.initial(err); })); }, deleteLatestInformation(id) { this.$loading.load(this.$auth.api.delete(`admin/latest_informations/${id}.json`) .then(response => { this.$router.push({name: 'AdminLatestInformationIndex', query: {toast_type: 'deleted'}}) this.setData(this.$route.query) }) .catch(err => { this.$errorHandlers.initial(err); })); }, }, mounted() { this.setData(this.$route.query); }, } </script>

環境

rails 6系
vue@2.6.10

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

任意のモーダルからイベントメソッドを受け渡す場合の参考にならないでしょうか。

Vue.js 汎用的な確認メッセージをモーダルダイアログで定義する

他のサイトを見てもthis.$confirmawaitを使って処理をやりとりして、メッセージがtrueの場合に処理を実行するという手段をとっています。それに当てはめると、trueの際に起動がうまくいっているdelete時のメソッドを実行すれば、期待通りの操作になるのではないかと思います。

投稿2021/08/03 01:57

FKM

総合スコア3644

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.46%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問