繁体   English   中英

Tailwind/Flowbite 模态不适用于 Vue.js 3 v-for

[英]Tailwind/Flowbite modal not working with Vue.js 3 v-for

我试图从表中调用顺风模态,其中行是从 vue.js 中的 for 循环生成的v-for ,但无法调用模态。 但是,我在没有 for 循环的情况下生成表,它工作得很好。 这是代码:

表格代码(带 v-for 循环)

<div class="overflow-x-auto relative sm:rounded-lg my-5 scrollbar-style">
  <table
    class="w-full text-sm text-left text-gray-700 dark:text-gray-400 relative"
  >
    <thead
      class="text-xs text-gray-700 uppercase bg-gray-100 dark:bg-gray-700 dark:text-gray-400"
    >
      <tr>
        <th scope="col" class="py-3 px-6">ID</th>
        <th scope="col" class="py-3 px-6">Product name</th>
        <th scope="col" class="py-3 px-6">Description</th>
        <th scope="col" class="py-3 px-6">Price</th>
        <th scope="col" class="py-3 px-6"></th>
        <th scope="col" class="py-3 px-6"></th>
      </tr>
    </thead>
    <tbody>
      <template v-for="product in products" :key="product.id">
        <tr
          class="bg-white border-b dark:bg-gray-900 dark:border-gray-700 hover:opacity-75"
        >
          <th
            scope="row"
            class="py-4 px-6 font-medium text-gray-900 whitespace-nowrap dark:text-white"
          >
            {{ product.id }}
          </th>
          <td class="py-4 px-6">{{ product.name }}</td>
          <td class="py-4 px-6 max-w-xs break-words">
            {{ product.description }}
          </td>
          <td class="py-4 px-6">{{ product.price }} $</td>
          <td class="py-4 px-6">
            <IconC
              iconName="Pencil"
              iconClass="w-5 h-5 text-blue-700 cursor-pointer hover:text-blue-500 rounded-full"
            />
          </td>
          <td class="py-4 px-6">
            <button data-modal-toggle="delete-modal">
              Delete
            </button>
          </td>
        </tr>
      </template>
    </tbody>
  </table>
</div>

模态代码(在for循环/表之外)

<div id="delete-modal" tabindex="-1" class="hidden overflow-y-auto overflow-x-hidden fixed top-0 right-0 left-0 z-50 md:inset-0 h-modal md:h-full">
    <div class="relative p-4 w-full max-w-md h-full md:h-auto">
        <div class="relative bg-white rounded-lg shadow dark:bg-gray-700">
            <button type="button" class="absolute top-3 right-2.5 text-gray-400 bg-transparent hover:bg-gray-200 hover:text-gray-900 rounded-lg text-sm p-1.5 ml-auto inline-flex items-center dark:hover:bg-gray-800 dark:hover:text-white" data-modal-toggle="delete-modal">
                <svg aria-hidden="true" class="w-5 h-5" fill="currentColor" viewBox="0 0 20 20" xmlns="http://www.w3.org/2000/svg"><path fill-rule="evenodd" d="M4.293 4.293a1 1 0 011.414 0L10 8.586l4.293-4.293a1 1 0 111.414 1.414L11.414 10l4.293 4.293a1 1 0 01-1.414 1.414L10 11.414l-4.293 4.293a1 1 0 01-1.414-1.414L8.586 10 4.293 5.707a1 1 0 010-1.414z" clip-rule="evenodd"></path></svg>
                <span class="sr-only">Close modal</span>
            </button>
            <div class="p-6 text-center">
                <svg aria-hidden="true" class="mx-auto mb-4 w-14 h-14 text-gray-400 dark:text-gray-200" fill="none" stroke="currentColor" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 8v4m0 4h.01M21 12a9 9 0 11-18 0 9 9 0 0118 0z"></path></svg>
                <h3 class="mb-5 text-lg font-normal text-gray-500 dark:text-gray-400">Are you sure you want to delete this product?</h3>
                <button data-modal-toggle="delete-modal" type="button" class="text-white bg-red-600 hover:bg-red-800 focus:ring-4 focus:outline-none focus:ring-red-300 dark:focus:ring-red-800 font-medium rounded-lg text-sm inline-flex items-center px-5 py-2.5 text-center mr-2">
                    Yes, I'm sure
                </button>
                <button data-modal-toggle="delete-modal" type="button" class="text-gray-500 bg-white hover:bg-gray-100 focus:ring-4 focus:outline-none focus:ring-gray-200 rounded-lg border border-gray-200 text-sm font-medium px-5 py-2.5 hover:text-gray-900 focus:z-10 dark:bg-gray-700 dark:text-gray-300 dark:border-gray-500 dark:hover:text-white dark:hover:bg-gray-600 dark:focus:ring-gray-600">No, cancel</button>
            </div>
        </div>
    </div>
</div>

对于您的场景,我认为您不需要data-modal-toggle ,而是尝试在 Viewmodal 中打开模式。

<button @click="openModal">
  Delete
</button>

openModal() {
  // call Modal.open() here
}

您可以在此处查看详细实现: https://flowbite.com/docs/components/modal/#example

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM