简体   繁体   English

jquery 中的响应单元 select

[英]Responsive cell select in jquery

I'm playing around with an idea for a responsive calendar.我正在玩一个响应式日历的想法。

Here's a fiddle that goes some way to illustrating what I'm thinking about...这是一个小提琴,可以在某种程度上说明我在想什么......

http://jsfiddle.net/7fsyaowh/ http://jsfiddle.net/7fsyaowh/

The intended behaviour is as follows:预期的行为如下:

A user is presented with a row of dates.向用户呈现一行日期。 Some are class 'a' ('available'), and some are class 'b' ('booked').有些是 class 'a' ('available'),有些是 class 'b' ('booked')。

The user can Select a start date and, optionally, an end date.用户可以 Select 开始日期,也可以选择结束日期。 The user can only select a date if it's class a.用户只能 select 一个日期,如果它是 class 一个。 (If a user doesn't select an end date, that means the booking is for one day only). (如果用户没有 select 结束日期,这意味着预订仅限一天)。

When a user selects a start date, all the dates prior to that date become unavailable.当用户选择开始日期时,该日期之前的所有日期均不可用。 Additionally all the dates after the next class b date also become unavailable, so the only available dates are those immediately following the first selected date.此外,下一个 class b 日期之后的所有日期也将不可用,因此唯一可用的日期是紧随第一个选定日期之后的日期。

I just can't think how to convey this behaviour in js (other than perhaps by assigning a distinct class id to each group of consecutive 'class a' dates).我只是想不出如何在 js 中传达这种行为(除了可能通过为每组连续的“a 类”日期分配不同的 class id 之外)。

Any thoughts appreciated.任何想法表示赞赏。

There are other considerations, like what happens if a user selects the start date again, or a date between the start and end date, and some sort of prompt so the user knows what they're supposed to be doing at any given point, but I'll come to that later.还有其他考虑因素,例如如果用户再次选择开始日期或开始日期和结束日期之间的日期会发生什么,以及某种提示,以便用户知道他们在任何给定时间应该做什么,但是稍后我会谈到这一点。

Contrary to what the fiddle suggests, there should be no drag-select behaviour.与小提琴所暗示的相反,不应该有拖动选择行为。

html html

<table cellpadding="0" cellspacing="0" id="our_table">
  <tr>
  <td class="a">1</td>
  <td class=a>2</td>
  <td class=a>3</td>
  <td class=a>4</td>
  <td class=a>5</td>
  <td class=b>6</td>
  <td class=a>7</td>
  <td class=b>8</td>
  <td class=a>9</td>
  <td class=a>10</td>
  <td class=a>11</td>
  <td class=a>12</td>
  <td class=b>13</td>
  <td class=a>14</td>
  <td class=a>15</td>
  <td class=b>16</td>
  <td class=a>17</td>
  <td class=a>18</td>
  <td class=a>19</td>
  <td class=a>20</td>
  <td class=a>21</td>
  <td class=b>22</td>
  <td class=a>23</td>
  <td class=a>24</td>
  <td class=a>25</td>
  <td class=a>26</td>
  <td class=a>27</td>
  <td class=b>28</td>
  <td class=a>29</td>
  <td class=a>30</td>
  </tr>
</table>
Submit
Clear/Reset

js/jquery js/jquery

$(function () {
  var isMouseDown = false,
    isHighlighted;
  $("#our_table td")
    .mousedown(function () {
      isMouseDown = true;
      $(this).toggleClass("a");
      isHighlighted = $(this).hasClass("a");
      return false; // prevent text selection
    })
    .mouseover(function () {
      if (isMouseDown) {
        $(this).toggleClass("a", isHighlighted);
      }
    });

  $(document)
    .mouseup(function () {
      isMouseDown = false;
    });
});

css css

table td {
  width:100px;
  height:100px;
  text-align:center;
  vertical-align:middle;
  background-color:#def;
  border:1px solid #fff;
}

table td.a {
  background-color:#ddd;
}
table td.b {
  background-color:#bbc;
}

You can use .prevAll() and .nextAll() method of jquery to disable dates previous and date after the b class.您可以使用.prevAll().nextAll()方法禁用b class 之前和之后的日期。 Also, below code is just for selecting start date and it highlights date available in yellow so here you need to write logic for selecting end dates from available dates.此外,下面的代码仅用于选择开始日期,并以yellow突出显示可用日期,因此您需要在此处编写逻辑以从available日期中选择结束日期。

Demo Code :演示代码

 //click on a class and unselect it to see how this works.. $("td.a").click(function() { $(this).toggleClass("selected") //see if has class selected.. if ($(this).hasClass("selected")) { //get all previous date disable them $(this).prevAll(".a:not('.b')").addClass("disabled") //get all date after `b` disable them $(this).nextAll(".b:first").nextAll(".a ").addClass("disabled") //added class for available dates.. $(this).nextAll(".a:not('.disabled')").addClass("avaible") } else { $("tr td").removeClass("disabled").removeClass("avaible") } })
 table td { width: 100px; height: 100px; text-align: center; vertical-align: middle; background-color: #def; border: 1px solid #fff; } table td.a { background-color: #ddd; } table td.b { background-color: #bbc; }.selected { background-color: red;important. }:disabled { pointer-events; none: background-color; grey.important: };avaible { background-color: yellow !important; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <table cellpadding="0" cellspacing="0" id="our_table"> <tr> <td class="a">1</td> <td class="a">2</td> <td class="a">3</td> <td class="a">4</td> <td class="a">5</td> <td class="b">6</td> <td class="a">7</td> <td class="b">8</td> <td class="a">9</td> <td class="a">10</td> <td class="a">11</td> <td class="a">12</td> <td class="b">13</td> <td class="a">14</td> <td class="a">15</td> <td class="b">16</td> <td class="a">17</td> <td class="a">18</td> <td class="a">19</td> <td class="a">20</td> <td class="a">21</td> <td class="b">22</td> <td class="a">23</td> <td class="a">24</td> <td class="a">25</td> <td class="a">26</td> <td class="a">27</td> <td class="b">28</td> <td class="a">29</td> <td class="a">30</td> </tr> </table>

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

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