英文:
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
效果也没有显示出来。
我检查了页面并发现了以下情况:
- 当我尝试使用元素选择工具选择表格主体时,我无法选择。
- 当我从元素选项卡中选择一行并选中了 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
<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']"
/>
</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" />
<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"
/>
<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>
Also the tailwind hover
effect is not showing as well.
I inspected the page and found out the following:
- When I tried selected the table body with the element selection tool I was not able to.
- 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类:
- table
- table-row
- table-row-group
- 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:
- table
- table-row
- table-row-group
- table-cell
The final result looked something like this
<div class="table w-full table-auto" :class="gridCols">
<!-- HEADER -->
<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']"
/>
</button>
</div>
</div>
</div>
<!-- BODY -->
<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"
>
<!-- VIRTUAL COLUMN -->
<component :is="cell.component(row, emit)" v-if="!('accessor' in cell) && cell.component !== undefined" />
<!-- NORMAL COLUMN -->
<span v-else-if="'accessor' in cell && cell.component === undefined && row[cell.accessor] !== null">
{{ row[cell.accessor] }}
</span>
<!-- CUSTOM COLUMN -->
<component
:is="cell.component(row[cell.accessor])"
v-else-if="'accessor' in cell && cell.component !== undefined"
/>
<!-- EMPTY STATE -->
<span v-else>---</span>
</div>
</div>
</div>
</div>
Don't know why that worked tho! but it seems like Safari doesn't support <table>
tags to be clickable
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论