Vue 3在Safari上点击事件不起作用。

huangapple go评论84阅读模式
英文:

Vue 3 click event is not working on safari

问题

我有这个表格组件,并且在 Safari 上遇到了一个问题,@click 事件无法触发,但在 Chrome 上可以正常工作。

<table class="h-full w-full">
  <thead class="sticky top-0 rounded bg-gray-100 shadow-sm">
    <tr class="t-head">
      <th
        v-for="column in visibleCols"
        :key="column.index"
        class="min-w-fit whitespace-nowrap rounded border-r p-3 text-left font-medium text-gray-800 last:border-0"
        @mouseenter="showSortIcon({ column })"
        @mouseleave="hideSortIcon({ column })"
      >
        <div class="flex item-center gap-3">
          <div>{{ column.displayName || "" }}</div>
          <button
            v-if="isSortEnabled && (column as Column).isSortable"
            class="hover:text-indigo-600 transition-opacity duration-300 ease-in-out"
            :class="[
              activeSortColumn.column == column || (sortIconState.column == column && sortIconState.visible)
                ? 'opacity-100'
                : 'opacity-0',
            ]"
            @click="onSortIconClicked(column)"
          >
            <i
              class="fa-solid fa-arrow-up transition-all duration-300 ease-in-out"
              :class="[column == activeSortColumn.column && activeSortColumn.isDesc ? 'rotate-180' : 'rotate-0']"
            ></i>
          </button>
        </div>
      </th>
    </tr>
  </thead>
  <tbody v-if="loading" class="bg-white">
    <tr>
      <td :colspan="visibleCols.length">
        <div class="flex">
          <i class="fa-solid fa-circle-notch m-auto animate-spin text-3xl text-indigo-600"></i>
        </div>
      </td>
    </tr>
  </tbody>
  <tbody v-else-if="data && data.length !== 0 && visibleCols.length !== 0" class="bg-white">
    <tr
      v-for="(row, index) in data"
      :key="index"
      class="border-b last:border-0"
      :class="{ 'cursor-pointer hover:bg-gray-100': areRowsClickable }"
      @click="handleRowClick(row)"
    >
      <td
        v-for="cell in visibleCols"
        :key="cell.index"
        class="min-w-fit whitespace-nowrap border-r px-3 py-2 text-left last:border-0"
        :class="cell.class"
      >
        <component :is="cell.component(row, emit)" v-if="!('accessor' in cell) && cell.component !== undefined"></component>
        <span v-else-if="'accessor' in cell && cell.component === undefined && row[cell.accessor] !== null">
          {{ row[cell.accessor] }}
        </span>
        <component
          :is="cell.component(row[cell.accessor])"
          v-else-if="'accessor' in cell && cell.component !== undefined"
        ></component>
        <span v-else>---</span>
      </td>
    </tr>
  </tbody>
  <tbody v-else class="bg-white">
    <tr>
      <td :colspan="visibleCols.length">
        <slot name="empty-state">
          <div class="flex h-[200px]">
            <span class="m-auto">No data to display</span>
          </div>
        </slot>
      </td>
    </tr>
  </tbody>
</table>

此外,Tailwind 的 hover 效果也没有显示出来。
我检查了页面并发现了以下情况:

  1. 当我尝试使用元素选择工具选择表格主体时,我无法选择。
  2. 当我从元素选项卡中选择一行并选中了 hover 复选框时,hover 效果成功渲染。
    我使用的是 macOS Ventura 13.0 版本,Safari 版本是 16.1。
英文:

I have this table component and I'm facing an issue with the @click event not firing on safari while being working just fine on chrome

  &lt;table class=&quot;h-full w-full&quot;&gt;
&lt;thead class=&quot;sticky top-0 rounded bg-gray-100 shadow-sm&quot;&gt;
&lt;tr class=&quot;t-head&quot;&gt;
&lt;th
v-for=&quot;column in visibleCols&quot;
:key=&quot;column.index&quot;
class=&quot;min-w-fit whitespace-nowrap rounded border-r p-3 text-left font-medium text-gray-800 last:border-0&quot;
@mouseenter=&quot;showSortIcon({ column })&quot;
@mouseleave=&quot;hideSortIcon({ column })&quot;
&gt;
&lt;div class=&quot;flex item-center gap-3&quot;&gt;
&lt;div&gt;{{ column.displayName || &quot;&quot; }}&lt;/div&gt;
&lt;button
v-if=&quot;isSortEnabled &amp;&amp; (column as Column).isSortable&quot;
class=&quot;hover:text-indigo-600 transition-opacity duration-300 ease-in-out&quot;
:class=&quot;[
activeSortColumn.column == column || (sortIconState.column == column &amp;&amp; sortIconState.visible)
? &#39;opacity-100&#39;
: &#39;opacity-0&#39;,
]&quot;
@click=&quot;onSortIconClicked(column)&quot;
&gt;
&lt;i
class=&quot;fa-solid fa-arrow-up transition-all duration-300 ease-in-out&quot;
:class=&quot;[column == activeSortColumn.column &amp;&amp; activeSortColumn.isDesc ? &#39;rotate-180&#39; : &#39;rotate-0&#39;]&quot;
/&gt;
&lt;/button&gt;
&lt;/div&gt;
&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody v-if=&quot;loading&quot; class=&quot;bg-white&quot;&gt;
&lt;tr&gt;
&lt;td :colspan=&quot;visibleCols.length&quot;&gt;
&lt;div class=&quot;flex&quot;&gt;
&lt;i class=&quot;fa-solid fa-circle-notch m-auto animate-spin text-3xl text-indigo-600&quot;&gt;&lt;/i&gt;
&lt;/div&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;tbody v-else-if=&quot;data &amp;&amp; data.length !== 0 &amp;&amp; visibleCols.length !== 0&quot; class=&quot;bg-white&quot;&gt;
&lt;tr
v-for=&quot;(row, index) in data&quot;
:key=&quot;index&quot;
class=&quot;border-b last:border-0&quot;
:class=&quot;{ &#39;cursor-pointer hover:bg-gray-100&#39;: areRowsClickable }&quot;
@click=&quot;handleRowClick(row)&quot;
&gt;
&lt;td
v-for=&quot;cell in visibleCols&quot;
:key=&quot;cell.index&quot;
class=&quot;min-w-fit whitespace-nowrap border-r px-3 py-2 text-left last:border-0&quot;
:class=&quot;cell.class&quot;
&gt;
&lt;component :is=&quot;cell.component(row, emit)&quot; v-if=&quot;!(&#39;accessor&#39; in cell) &amp;&amp; cell.component !== undefined&quot; /&gt;
&lt;span v-else-if=&quot;&#39;accessor&#39; in cell &amp;&amp; cell.component === undefined &amp;&amp; row[cell.accessor] !== null&quot;&gt;
{{ row[cell.accessor] }}
&lt;/span&gt;
&lt;component
:is=&quot;cell.component(row[cell.accessor])&quot;
v-else-if=&quot;&#39;accessor&#39; in cell &amp;&amp; cell.component !== undefined&quot;
/&gt;
&lt;span v-else&gt;---&lt;/span&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;tbody v-else class=&quot;bg-white&quot;&gt;
&lt;tr&gt;
&lt;td :colspan=&quot;visibleCols.length&quot;&gt;
&lt;slot name=&quot;empty-state&quot;&gt;
&lt;div class=&quot;flex h-[200px]&quot;&gt;
&lt;span class=&quot;m-auto&quot;&gt;No data to display&lt;/span&gt;
&lt;/div&gt;
&lt;/slot&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;

Also the tailwind hover effect is not showing as well.
I inspected the page and found out the following:

  1. When I tried selected the table body with the element selection tool I was not able to.
  2. When I selected a row from the elements tab and this checked the hover checkbox the hover effect was rendered successfully.
    I'm using macOS Ventura 13.0 with safari version 16.1

答案1

得分: 0

我将以下内容翻译成中文:

我的解决方案如下:
我改变了整个表格结构,使用了div代替。
我使用了以下Tailwind类:

  1. table
  2. table-row
  3. table-row-group
  4. table-cell

最终结果看起来像这样:

<div class="table w-full table-auto" :class="gridCols">
    <!-- 表头 -->
    <div class="sticky top-0 table-row bg-gray-200">
        <div
            v-for="column in visibleCols"
            :key="column.index"
            class="table-cell whitespace-nowrap border-b border-r border-gray-300 p-3 text-left font-medium text-gray-800 last:border-r-0"
            @mouseenter="showSortIcon({ column })"
            @mouseleave="hideSortIcon({ column })"
        >
            <div class="item-center flex gap-3">
                <div>{{ column.displayName || "" }}</div>
                <button
                    v-if="isSortEnabled && (column as Column).isSortable"
                    class="transition-opacity duration-300 ease-in-out hover:text-indigo-600"
                    :class="[
                        activeSortColumn.column == column || (sortIconState.column == column && sortIconState.visible)
                            ? 'opacity-100'
                            : 'opacity-0',
                    ]"
                    @click="onSortIconClicked(column)"
                >
                    <i
                        class="fa-solid fa-arrow-up transition-all duration-300 ease-in-out"
                        :class="[column == activeSortColumn.column && activeSortColumn.isDesc ? 'rotate-180' : 'rotate-0']"
                    ></i>
                </button>
            </div>
        </div>
    </div>
    <!-- 表体 -->
    <div v-if="data?.length && !isLoading" class="table-row-group">
        <div
            v-for="row in data"
            :key="row"
            class="table-row border bg-white last:border-0"
            :class="areRowsClickable ? 'cursor-pointer hover:bg-gray-100' : ''"
            @click="handleRowClick(row)"
        >
            <div
                v-for="cell in visibleCols"
                :key="cell.displayName"
                class="table-cell whitespace-nowrap border-b border-r px-3 py-2 last:border-r-0"
            >
                <!-- 虚拟列 -->
                <component :is="cell.component(row, emit)" v-if="!('accessor' in cell) && cell.component !== undefined"></component>
                <!-- 普通列 -->
                <span v-else-if="'accessor' in cell && cell.component === undefined && row[cell.accessor] !== null">
                    {{ row[cell.accessor] }}
                </span>
                <!-- 自定义列 -->
                <component
                    :is="cell.component(row[cell.accessor])"
                    v-else-if="'accessor' in cell && cell.component !== undefined"
                ></component>
                <!-- 空状态 -->
                <span v-else>---</span>
            </div>
        </div>
    </div>
</div>

不知道为什么这样能行!但似乎Safari不支持<table>标签可点击。

英文:

My solution was the following:
I changed the whole table structure to use divs instead.
I utilized the following Tailwind classes:

  1. table
  2. table-row
  3. table-row-group
  4. table-cell

The final result looked something like this

&lt;div class=&quot;table w-full table-auto&quot; :class=&quot;gridCols&quot;&gt;
&lt;!-- HEADER --&gt;
&lt;div class=&quot;sticky top-0 table-row bg-gray-200&quot;&gt;
&lt;div
v-for=&quot;column in visibleCols&quot;
:key=&quot;column.index&quot;
class=&quot;table-cell whitespace-nowrap border-b border-r border-gray-300 p-3 text-left font-medium text-gray-800 last:border-r-0&quot;
@mouseenter=&quot;showSortIcon({ column })&quot;
@mouseleave=&quot;hideSortIcon({ column })&quot;
&gt;
&lt;div class=&quot;item-center flex gap-3&quot;&gt;
&lt;div&gt;{{ column.displayName || &quot;&quot; }}&lt;/div&gt;
&lt;button
v-if=&quot;isSortEnabled &amp;&amp; (column as Column).isSortable&quot;
class=&quot;transition-opacity duration-300 ease-in-out hover:text-indigo-600&quot;
:class=&quot;[
activeSortColumn.column == column || (sortIconState.column == column &amp;&amp; sortIconState.visible)
? &#39;opacity-100&#39;
: &#39;opacity-0&#39;,
]&quot;
@click=&quot;onSortIconClicked(column)&quot;
&gt;
&lt;i
class=&quot;fa-solid fa-arrow-up transition-all duration-300 ease-in-out&quot;
:class=&quot;[column == activeSortColumn.column &amp;&amp; activeSortColumn.isDesc ? &#39;rotate-180&#39; : &#39;rotate-0&#39;]&quot;
/&gt;
&lt;/button&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;!-- BODY --&gt;
&lt;div v-if=&quot;data?.length &amp;&amp; !isLoading&quot; class=&quot;table-row-group&quot;&gt;
&lt;div
v-for=&quot;row in data&quot;
:key=&quot;row&quot;
class=&quot;table-row border bg-white last:border-0&quot;
:class=&quot;areRowsClickable ? &#39;cursor-pointer hover:bg-gray-100&#39; : &#39;&#39;&quot;
@click=&quot;handleRowClick(row)&quot;
&gt;
&lt;div
v-for=&quot;cell in visibleCols&quot;
:key=&quot;cell.displayName&quot;
class=&quot;table-cell whitespace-nowrap border-b border-r px-3 py-2 last:border-r-0&quot;
&gt;
&lt;!-- VIRTUAL COLUMN --&gt;
&lt;component :is=&quot;cell.component(row, emit)&quot; v-if=&quot;!(&#39;accessor&#39; in cell) &amp;&amp; cell.component !== undefined&quot; /&gt;
&lt;!-- NORMAL COLUMN --&gt;
&lt;span v-else-if=&quot;&#39;accessor&#39; in cell &amp;&amp; cell.component === undefined &amp;&amp; row[cell.accessor] !== null&quot;&gt;
{{ row[cell.accessor] }}
&lt;/span&gt;
&lt;!-- CUSTOM COLUMN --&gt;
&lt;component
:is=&quot;cell.component(row[cell.accessor])&quot;
v-else-if=&quot;&#39;accessor&#39; in cell &amp;&amp; cell.component !== undefined&quot;
/&gt;
&lt;!-- EMPTY STATE --&gt;
&lt;span v-else&gt;---&lt;/span&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;

Don't know why that worked tho! but it seems like Safari doesn't support &lt;table&gt; tags to be clickable

huangapple
  • 本文由 发表于 2023年6月12日 15:32:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/76454453.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定