- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我一直在处理 AlpineJS 的一些代码示例。起初,代码只会处理 2020 年,不会超过 2020 年 12 月。
我现在已经设法让代码超过 2020 年,但是日历 View 中的日期没有更新,我看不到我遗漏了什么。
我的代码在 JSFiddle 上,任何人都可以看到我哪里出错了,因为您可以过去 2020 年 12 月,然后选择一个日期,然后可以很好地填充输入字段。
HTML
<div class="flex items-center justify-center">
<div class="antialiased sans-serif">
<div x-data="app()" x-init="[initDate(), getNoOfDays()]" x-cloak>
<div class="container mx-auto px-4 py-2 md:py-10">
<div class="mb-5 w-64">
<label for="datepicker" class="font-bold mb-1 text-gray-700 block">Select Date</label>
<div class="relative">
<input type="hidden" name="date" x-ref="date">
<input type="text" readonly x-model="datepickerValue" @click="showDatepicker = !showDatepicker" @keydown.escape="showDatepicker = false" class="w-full pl-4 pr-10 py-3 leading-none rounded-lg shadow-sm focus:outline-none focus:shadow-outline text-gray-600 font-medium" placeholder="Select date">
<div class="absolute top-0 right-0 px-3 py-2">
<svg class="h-6 w-6 text-gray-400" fill="none" viewBox="0 0 24 24" stroke="currentColor">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M8 7V3m8 4V3m-9 8h10M5 21h14a2 2 0 002-2V7a2 2 0 00-2-2H5a2 2 0 00-2 2v12a2 2 0 002 2z" />
</svg>
</div>
<!-- <div x-text="no_of_days.length"></div>
<div x-text="32 - new Date(year, month, 32).getDate()"></div>
<div x-text="new Date(year, month).getDay()"></div> -->
<div class="bg-white mt-12 rounded-lg shadow p-4 absolute top-0 left-0" style="width: 17rem" x-show.transition="showDatepicker" @click.away="showDatepicker = false">
<div class="flex justify-between items-center mb-2">
<div>
<span x-text="MONTH_NAMES[month]" class="text-lg font-bold text-gray-800"></span>
<span x-text="year" class="ml-1 text-lg text-gray-600 font-normal"></span>
</div>
<div>
<button type="button" class="transition ease-in-out duration-100 inline-flex cursor-pointer hover:bg-gray-200 p-1 rounded-full" @click="month--; getNoOfDays()">
<svg class="h-6 w-6 text-gray-500 inline-flex" fill="none" viewBox="0 0 24 24" stroke="currentColor">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M15 19l-7-7 7-7" />
</svg>
</button>
<button type="button" class="transition ease-in-out duration-100 inline-flex cursor-pointer hover:bg-gray-200 p-1 rounded-full" @click="month++; getNoOfDays()">
<svg class="h-6 w-6 text-gray-500 inline-flex" fill="none" viewBox="0 0 24 24" stroke="currentColor">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 5l7 7-7 7" />
</svg>
</button>
</div>
</div>
<div class="flex flex-wrap mb-3 -mx-1">
<template x-for="(day, index) in DAYS" :key="index">
<div style="width: 14.26%" class="px-1">
<div x-text="day" class="text-gray-800 font-medium text-center text-xs"></div>
</div>
</template>
</div>
<div class="flex flex-wrap -mx-1">
<template x-for="blankday in blankdays">
<div style="width: 14.28%" class="text-center border p-1 border-transparent text-sm"></div>
</template>
<template x-for="(date, dateIndex) in no_of_days" :key="dateIndex">
<div style="width: 14.28%" class="px-1 mb-1">
<div @click="getDateValue(date)" x-text="date" class="cursor-pointer text-center text-sm leading-none rounded-full leading-loose transition ease-in-out duration-100" :class="{'bg-blue-500 text-white': isToday(date) == true, 'text-gray-700 hover:bg-blue-200': isToday(date) == false }"></div>
</div>
</template>
</div>
</div>
</div>
</div>
</div>
</div>
Alpine 代码
const MONTH_NAMES = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];
const DAYS = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'];
function app() {
return {
showDatepicker: false,
datepickerValue: '',
month: '',
year: '',
no_of_days: [],
blankdays: [],
days: ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'],
initDate() {
let today = new Date();
this.month = today.getMonth();
this.year = today.getFullYear();
this.datepickerValue = new Date(this.year, this.month, today.getDate()).toDateString();
},
isToday(date) {
const today = new Date();
const d = new Date(this.year, this.month, date);
return today.toDateString() === d.toDateString() ? true : false;
},
getDateValue(date) {
let selectedDate = new Date(this.year, this.month, date);
this.datepickerValue = selectedDate.toDateString();
this.$refs.date.value = selectedDate.getFullYear() +"-"+ ('0'+ selectedDate.getMonth()).slice(-2) +"-"+ ('0' + selectedDate.getDate()).slice(-2);
console.log(this.$refs.date.value);
this.showDatepicker = false;
},
getNoOfDays() {
let daysInMonth = new Date(this.year, this.month + 1, 0).getDate();
// find where to start calendar day of week
let dayOfWeek = new Date(this.year, this.month).getDay();
let blankdaysArray = [];
for ( var i=1; i <= dayOfWeek; i++) {
blankdaysArray.push(i);
}
let daysArray = [];
for ( var i=1; i <= daysInMonth; i++) {
daysArray.push(i);
}
this.blankdays = blankdaysArray;
this.no_of_days = daysArray;
}
}
}
最佳答案
当您到达 12 月时,您需要设置增加年份而不是月份,并将月份变量设置回 0。当您减少月份并到达 1 月时,情况类似。
<div>
<button type="button" class="transition ease-in-out duration-100 inline-flex cursor-pointer hover:bg-gray-200 p-1 rounded-full" @click="if(month == 0) {month=11; year--} else {month--}; getNoOfDays()">
<svg class="h-6 w-6 text-gray-500 inline-flex" fill="none" viewBox="0 0 24 24" stroke="currentColor">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M15 19l-7-7 7-7" />
</svg>
</button>
<button type="button" class="transition ease-in-out duration-100 inline-flex cursor-pointer hover:bg-gray-200 p-1 rounded-full" @click="if (month == 11) {month=0; year++} else {month++}; getNoOfDays()">
<svg class="h-6 w-6 text-gray-500 inline-flex" fill="none" viewBox="0 0 24 24" stroke="currentColor">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 5l7 7-7 7" />
</svg>
</button>
</div>
类似于 https://jsfiddle.net/a0726tjw/3/ .
它仍然返回有关日期未定义的奇怪错误,这会破坏发生时的日期顺序。其他地方可能还有另一个错误,但我还没有弄清楚在哪里。
关于tailwind-css - 使用 AlpineJS 的日期选择器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62003419/
我已经得到了一个表,其中某些列/单元格是根据使用alPine:CLASS指令的切换器显示或隐藏的:。单元格内容。这些单元格也可以使用jQuery插件导出,该插件提供了一个可以添加到元素中以便导出器忽略
在 AlpineJS docs有这个例子: Open // Dropdown function dropdown() { re
初始化我的商店 document.addEventListener('alpine:init', () => { Alpine.store('selectedInput', 0) }) 但是,当我
我试图将两个 :class 绑定(bind)附加到 x-for 循环中的单个元素。通常这可以通过传入具有多个键值对的单个对象来实现。然而,在这种情况下,一个是条件,另一个是循环的属性: 条件: :cl
Max Res 我在 alpinejs 中有一个元素,我想在单击该链接时获取数据属性的值。 最佳答案 您需要使用 6 个“魔法属性”之一,在您的情况下是 $event 一: window.
我在这个元素中使用 TailwindCSS 和 AlpineJS。有两个按钮可以切换选项卡,第一个按钮具有自动对焦功能。切换选项卡时,另一个按钮变为事件状态: 我希望该按钮仅在单击另一个按钮时才变为非
我想在 AlpineJS 的循环中设置隐藏输入字段的 name 属性。我试过 x-bind:name 但这不起作用。 我认为这不起作用,因为 x-model 如何添加待办事项: Add
希望能够使用 Alpine.js 进行下拉选择表单。当您从选择菜单中选择特定选项时,页面会插入一组相关记录。我将如何用 Alpine.js 完成这样的事情 例如 从选择菜单中选择美国、加拿大和墨西哥。
有谁知道如何在 TailwindCSS/AlpineJS 中构建“下拉菜单”?我知道如何构建下拉菜单,但无法制作下拉菜单。 我的下拉列表:
有谁知道如何在 TailwindCSS/AlpineJS 中构建“下拉菜单”?我知道如何构建下拉菜单,但无法制作下拉菜单。 我的下拉列表:
我开始在我的项目中使用 alpine JS 并遇到了这种情况,我真的不知道解决这个问题的最佳方法。我有一个输入复选框,我需要限制用户只能选择其中的 12 个。我可能可以用普通的 javascript
应该如何使用 NPM 导入 AlpineJS? AlpineJS 文档说: From npm: Install the package from npm. npm i alpinejs Include
我一直在处理 AlpineJS 的一些代码示例。起初,代码只会处理 2020 年,不会超过 2020 年 12 月。 我现在已经设法让代码超过 2020 年,但是日历 View 中的日期没有更新,我看
我正在使用 Alpine 显示将更改的项目列表。但是我不知道如何告诉 Alpine 在新的项目从服务器返回后刷新项目列表: 第一个“批次”的项目很好,因为它们
我有一个使用 AlpineJS 隐藏的警报组件,但我希望它在 Livewire 重新呈现后再次可见。 使用 Livewire 组件显示警报 @if(Session::has('success'))
我有一个由 AlpineJS 提供的 Datatable 表: 在 x-for 中,我有 user.Id 的值,可以在 SPAN 字段中列出,并带有指令 x-text: 我需要在我的 HREF
这应该很简单,但我不明白。我想复制一个按钮的功能,当按下按钮时转到 API(这可能需要大约一分钟来获取数据并处理它),它会被禁用,并在加载数据后被启用。 我正在使用 Laravel/Livewire/
我有一个使用 AlpineJS 提交的表单。 使用 x-init 我想使用从 cookie 中读取的两个字母字符串将下拉列表默认设置为特定国家/地区。 如何使用在 initForm() 中获得的 2
加载 alpinejs 时,它会取消选中所有复选框,我不知道为什么会这样,但确实如此。 Colors: orange,red,yellow 所以当加载上面的代
我有一个使用 AlpineJS 提交的表单。 使用 x-init 我想使用从 cookie 中读取的两个字母字符串将下拉列表默认设置为特定国家/地区。 如何使用在 initForm() 中获得的 2
我是一名优秀的程序员,十分优秀!