Javascript-форум (https://javascript.ru/forum/)
-   jQuery (https://javascript.ru/forum/jquery/)
-   -   Как найти ближайший предыдущий элемент с искомым классом? (https://javascript.ru/forum/jquery/75472-kak-najjti-blizhajjshijj-predydushhijj-ehlement-s-iskomym-klassom.html)

giwuf 12.10.2018 19:08

Как найти ближайший предыдущий элемент с искомым классом?
 
Есть календарь с фильтром по ивентам. Если выбираем первый в списке за день, то все поля отображаются так, как надо, но если ивент идет 2,3 и т.д. начинаются проблемы, съедается шапка, строка в таблице с датой.

Как добраться до искомых элементов с классом fc-list-heading (в другие дни) и скрывать только их, а непосредственно идущий в тот же день, что и сам ивент оставлять? фидл
$(document).on('change', '#method_selector', function() {
  $('.fc-list-item').show();
  $('.fc-list-heading').show();
  var method = $(this).val();
  if (method.length) {
    $('.fc-list-item').hide();
    $('.fc-list-item').prev('.fc-list-heading').hide();
    $('.fc-list-item.' + method).show();
    $('.fc-list-item.' + method).prev('.fc-list-heading').show();
  }
});
$('#calendar').fullCalendar({
  header: {
    left: 'prev,next today',
    center: 'title',
    right: 'listDay,listWeek,month'
  },

  // customize the button names,
  // otherwise they'd all just say "list"
  views: {
    listDay: {
      buttonText: 'list day'
    },
    listWeek: {
      buttonText: 'list week'
    }
  },

  defaultView: 'listWeek',
  defaultDate: '2016-09-12',
  navLinks: true, // can click day/week names to navigate views
  editable: true,
  eventLimit: true, // allow "more" link when too many events
  events: [{
      title: 'Conference (website)',
      start: '2016-09-11',
      end: '2016-09-14',
      url: "https://www.ted.com/talks",
      className: "Conference"
    },
    {
      title: 'Meeting (download document)',
      start: '2016-09-12T10:30:00',
      end: '2016-09-12T12:30:00',
      url: "http://storage.ted.com/tedx/manuals/tedx_speaker_guide.pdf",
      className: "Meeting_download"
    },
    {
      title: 'Lunch',
      start: '2016-09-12T12:00:00',
      className: "Lunch"
    },
    {
      title: 'Meeting (members only)',
      start: '2016-09-12T14:30:00',
      url: "http://www.dictionary.com/browse/restricted",
      className: "Meeting_memebers"
    },
    {
      title: 'Happy Hour',
      start: '2016-09-12T17:30:00',
      className: "Happy_Hour"
    },
    {
      title: 'Dinner',
      start: '2016-09-12T20:00:00',
      className: "Dinner"
    }
  ]
});

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://fullcalendar.io/js/fullcalendar-3.0.1/lib/moment.min.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/fullcalendar/3.0.1/fullcalendar.css" rel="stylesheet" />
<link href="https://cdnjs.cloudflare.com/ajax/libs/fullcalendar/3.0.1/fullcalendar.min.css" rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/fullcalendar/3.0.1/fullcalendar.js"></script>
<select id="method_selector">
  <option value="">All</option>
  <option value="Conference">Conference (website)</option>
  <option value="Meeting_download">Meeting (download document)</option>
  <option value="Lunch">Lunch</option>
  <option value="Meeting_memebers">Meeting (members only)</option>
  <option value="Happy_Hour">Happy Hour</option>
  <option value="Dinner">Dinner</option>
</select>
<div>
  <div id='calendar'></div>
</div>

body {
  margin: 40px 10px;
  padding: 0;
  font-family: "Lucida Grande", Helvetica, Arial, Verdana, sans-serif;
  font-size: 14px;
}

#calendar {
  max-width: 900px;
  margin: 0 auto;
}

.fc-list-item-title a:link,
.fc-list-item-title a:visited {
  color: #447af8;
}

.fc-list-item-title a:hover {
  color: #000000;
  text-decoration: underline;
}

рони 12.10.2018 20:33

giwuf,
if( method.length ) {	
      $('.fc-list-item').hide();
      $('.fc-list-heading').hide();
      $('.fc-list-item.' + method).show()
     .each(function() {
     $(this).prevAll('.fc-list-heading:first').show();
     })
}


https://jsfiddle.net/xc5ody5m/111/

giwuf 12.10.2018 22:28

рони,

Ух ты! Крутое очень решение! Спасибо большое!


Часовой пояс GMT +3, время: 15:14.