const elements = document.querySelectorAll('input[type="button"]');
const wrapperTag = 'div';
const wrapperClass = 'block';
elements.forEach(n => {
const wrapper = document.createElement(wrapperTag);
n.replaceWith(wrapper);
wrapper.classList.add(wrapperClass);
wrapper.append(n);
});
for (const n of elements) {
n.after(document.createElement(wrapperTag));
n.nextSibling.className = wrapperClass;
n.nextSibling.appendChild(n);
}
for (let i = 0; i < elements.length; i++) {
const n = elements[i];
n.outerHTML = `<${wrapperTag} class="${wrapperClass}">${n.outerHTML}</${wrapperTag}>`;
}
(function wrap(i, n = elements[i]) {
if (n) {
const wrapper = document.createElement(wrapperTag);
n.parentNode.replaceChild(wrapper, n);
wrapper.classList.value = wrapperClass;
wrapper.insertAdjacentElement('afterbegin', n);
wrap(-~i);
}
})(0);
const values = [ '1', '2', '3' ];
.[...select].forEach(n => values.includes(n.value) && n.remove());
for (const n of select.querySelectorAll(values.map(n => `[value="${n}"]`))) {
select.removeChild(n);
}
Array.prototype.reduceRight.call(
select.options,
(_, n) => ~values.indexOf(n.value) && (n.outerHTML = ''),
null
);
for (let i = select.children.length; i--;) {
const n = select.children[i];
for (let j = 0; j < values.length; j++) {
if (values[j] === n.value) {
n.replaceWith();
break;
}
}
}
Обычный img у меня не сработал.
product.amount = 1;
на Vue.set(product, 'amount', 1);
.product.amount = 1;
state.items.push(product);
state.items.push({
...product,
amount: 1,
});
const filterArrByObj = (arr, obj) =>
Object.entries(obj).reduce((arr, [ k, v ]) => {
return arr.filter(Array.isArray(v)
? n => v.includes(n[k])
// !v.length || v.includes(n[k]), если при пустом массиве допустимо любое значение
: n => v === n[k]
// n[k].includes(v), если точное соответствие не нужно
);
}, arr);
computed: {
filteredData() {
return filterArrByObj(this.data, this.filters);
},
},
<tr v-for="n in filteredData">
...
calc__price
и calc__payment
заменить на общий calc
.const $price = $('#price');
const $firstPayment = $('#first-payment');
function update($elem) {
$elem.closest('.calc').find('output').text($elem.val());
$('.payment-percent').html(Math.round($firstPayment.val() * 100 / $price.val()) + '%');
}
$price.closest('.calc').on('input', function() {
const val = $price.val();
$firstPayment
.val((i, v) => Math.min(v, val))
.attr('max', val)
.rangeslider('update', true);
update($firstPayment);
});
$price.add($firstPayment)
.rangeslider({ polyfill: false })
.closest('.calc')
.on('input', e => update($(e.target)))
.end()
.trigger('input');
function walkNested(val, callback) {
callback(val);
if (val instanceof Object) {
Object.values(val).forEach(n => walkNested(n, callback));
}
}
walkNested(obj, n => n?.touched === true && (n.touched = false));
function walkNested(val, callback) {
for (const stack = [ val ]; stack.length;) {
const n = stack.pop();
callback(n);
stack.push(...Object.values(n ?? {}));
}
}
const keys = [ 'city', 'country' ];
.const newArr = arr.map(n => Object.fromEntries(Object.values(n).map((v, i) => [ keys[i], v ])));
arr.forEach(n => Object.entries(n).forEach(([ k, v ], i) => (delete n[k], n[keys[i]] = v)));
str.replace(/\t.*/s, '')
// или
str.match(/^[^\t]*/)[0]
// или
str.slice(0, str.search(/\t|$/))
// или
str.split('\t', 1).pop()
// или
[...str].reduceRight((acc, n) => n === '\t' ? '' : n + acc, '')
const $menuItems = $('#menu a');
$('#submenu > ul').width(i => $menuItems.eq(i).width());
const min = 7;
const max = min + 21;
function update(value) {
value = Math.max(min, Math.min(max, value | 0));
$('#spinner2').val(value);
$('#slider2').slider('value', value);
const date = new Date();
date.setDate(date.getDate() + value);
$('#amount').val(date.toLocaleString('en-US', {
year: 'numeric',
month: 'long',
day: 'numeric',
}));
}
$('#slider2').slider({
range: 'min',
min,
max,
step: 1,
slide: (e, ui) => update(ui.value),
});
$('#spinner2').spinner({
min,
max,
spin: (e, ui) => update(ui.value),
}).on('input', e => update(e.target.value)).trigger('input');
Мои текущие знания и умения:
const sort = {
number: (a, b) => a - b,
string: (a, b) => a.localeCompare(b),
};
function sortTable(table, colIndex) {
if (typeof table === 'string') {
table = document.querySelector(table);
}
const head = table.tHead.rows[0];
const compare = sort[head.cells[colIndex].dataset.type] ?? sort.string;
const value = row => row.cells[colIndex].innerText;
const tbody = table.tBodies[0];
tbody.append(...[...tbody.rows].sort((a, b) => compare(value(a), value(b))));
[...head.cells].forEach((n, i) => n.classList.toggle('sorted', i == colIndex));
}
.sorted {
background: #ccc;
}
.sorted::after {
content: " \2193";
}
// просто дёргаем сортировку
sortTable('#grid', 0);
sortTable(document.querySelector('table'), 1);
// или, сортируем по клику на заголовки столбцов
document.querySelectorAll('#grid th').forEach(function(n) {
n.addEventListener('click', this);
}, ({ target: t }) => sortTable(t.closest('table'), t.cellIndex));
А можно как-то присваивать свой класс?
computed: {
activeLink() {
return this.links.find(n => n.url === this.$route.path);
},
},
:class="{ active: activeLink === link }"