const sum = Array.prototype.reduce.call(
$('input[type="checkbox"]:checked'),
(acc, n) => acc + +n.value,
0
);
$.fn.customSelect = function() {
return this.each(function() {
const $this = $(this).hide();
const $wrapper = $this.wrap('<div class="select_wrapper"></div>').parent();
$wrapper.append(`
<span>${$this.find(':selected').text()}</span>
<ul class="select_inner">${$this.children().get().map(n => `
<li data-value="${n.value}">${n.innerText}</li>`).join('')}
</ul>
`);
$wrapper.on('click', 'li', function() {
$wrapper.children('span').text(this.innerText);
$this.val(this.dataset.value).trigger('change');
});
$wrapper.on('click', function() {
$('ul', this).slideToggle('fast');
});
});
};
$('#materialColor, #kantcolor').customSelect().change(function() {
console.log(`#${this.id}: "${this.value}"`);
});
$('.datepicker-input').datepicker({
minDate: 0,
onClose(dateText, inst) {
$(this).parent().next('.date').text([
inst.selectedDay,
inst.selectedMonth + 1,
inst.selectedYear
].join('-'));
}
});
$('.date').click(function() {
$(this).prev('div').find('.datepicker-input').focus();
});
import './local_copies/xd_connection.js';
import './local_copies/rbadman-html5.min.js';
// ну и т.д.const context = require.context('./local_copies', false, /\.js$/);
context.keys().forEach(key => context(key));
const ancestor = 'селектор элементов, которые надо получить';
const descendant = 'селектор вложенных элементов';Array.prototype.filter.call(
document.querySelectorAll(ancestor),
n => n.querySelector(descendant)
)Array.from(
document.querySelectorAll(`${ancestor} ${descendant}`),
n => n.closest(ancestor)
)[...document.querySelectorAll(descendant)].reduce(
(acc, n) => ((n = n.closest(ancestor)) && acc.push(n), acc),
[]
)document.querySelectorAll(`${ancestor}:has(${descendant})`)
tr, а выше - на tbody, или саму таблицу, если элементов tbody несколько или надо также слушать клики и на строках внутри thead/tfoot. А в обработчике уже пытаться подняться от event.target к элементу строки:table.addEventListener('click', e => {
const tr = e.target.closest('tbody tr');
if (tr) {
// ...
}
});
class App extends React.Component {
state = {
classes: [ 'bullshit' ],
}
onFocus = () => {
this.setState(({ classes }) => ({
classes: [ ...classes, 'focus' ],
}));
}
onBlur = () => {
this.setState(({ classes }) => ({
classes: classes.filter(n => n !== 'focus'),
}));
}
render() {
return (
<div className={this.state.classes.join(' ')}>
<input onFocus={this.onFocus} onBlur={this.onBlur} />
</div>
);
}
}
[
[ 'region_id', 'regionName' ],
[ 'district_id', 'districtName' ],
[ 'area_id', 'areaName' ],
[ 'city_id', 'cityName' ],
[ 'place_id', 'cityName' ],
[ 'name', 'streetName' ],
].forEach(([ del, search ]) => {
if (this.tableParam[del]) {
delete this.tableParam[del];
this.searchForm.get(search).patchValue(null);
}
});
state отказывается обновляться
onImportantClick = (e) => {
e.stopPropagation();
this.setState(({ important }) => ({
important: !important
}));
};
const containerSelector = '#menu_left';
const itemSelector = `${containerSelector} li`;
const className = 'active';$(itemSelector).click(function(e) {
e.stopPropagation();
$(this).addClass(className);
});
// или
document.querySelectorAll(itemSelector).forEach(function(n) {
n.addEventListener('click', this);
}, e => {
e.stopPropagation();
e.currentTarget.classList.add(className);
});$(containerSelector).click(e => {
$(e.target).closest(itemSelector).addClass(className);
});
// или
document.querySelector(containerSelector).addEventListener('click', e => {
const li = e.target.closest(itemSelector);
if (li) {
li.classList.add(className);
}
});
const parent = document.querySelector('.box').parentNode;
const iMin = 2;
const iMax = 5;
const wrapper = document.createElement('div');
wrapper.classList.add('wrapper');if (parent.children[iMin]) {
const elems = Array.prototype.slice.call(parent.children, iMin, iMax);
elems[0].before(wrapper);
wrapper.append(...elems);
}
// или
const elems = parent.querySelectorAll(`:nth-child(n + ${-~iMin}):not(:nth-child(n + ${-~iMax}))`);
if (elems.length) {
parent.insertBefore(wrapper, elems[0]);
elems.forEach(n => wrapper.appendChild(n));
}
// или
if (parent.children.length > iMin) {
parent.children[iMin].insertAdjacentElement('beforebegin', wrapper);
for (let i = iMax - iMin, n = null; i-- && (n = wrapper.nextElementSibling);) {
wrapper.insertAdjacentElement('beforeend', n);
}
}
const result = await Promise.all([ 1, 2, 3 ].map((n, i) => {
return new Promise(resolve => {
setTimeout(() => {
console.log(`timeout #${i}`);
resolve(n * 10);
}, Math.random() * 3000 | 0);
});
}));
console.log('result:', result);