const text = [
'hello, world!!',
'fuck the world',
'fuck everything',
];$('table').on('click', 'td', function() {
const $this = $(this);
const index = ($this.data('index') + 1) % text.length;
$this.text(text[index]).data('index', index);
}).find('td').data('index', -1);document.querySelector('table').addEventListener('click', ({ target: t }) => {
if (t = t.closest('td')) {
t.textContent = text[-~text.indexOf(t.textContent) % text.length];
}
});
SELECT
question_id,
SUM(points < 1) AS 'Incorrect',
SUM(points >= 1) AS 'Correct'
FROM
`cp_question_student`
GROUP BY
question_id
document.body.appendChild(anchor). А потом уже делать click.
function removeWords($str, $n) {
return implode(' ', array_slice(explode(' ', $str), 0, $n - 1));
}
echo removeWords("Бухгалтерский учет и анализ слонов", 3);
function createTree(data, levelKey, childrenKey) {
const tree = [];
const rootLevel = data.length && data[0][levelKey];
for (const n of data) {
let arr = tree;
for (let level = rootLevel; n[levelKey] > level++;) {
arr = arr[arr.length - 1][childrenKey];
}
arr.push(Object.assign({ [childrenKey]: [] }, n));
}
return tree;
}
const tree = createTree(arr, 'level', 'nodes');function createTree(
data,
{
levelKey = 'level',
childrenKey = 'children',
} = {}
) {
const rootLevel = data.length && data[0][levelKey];
return data.reduce((acc, {...n}) => (
acc[n[levelKey] + 1] = n[childrenKey] = [],
acc[n[levelKey]].push(n),
acc
), { [rootLevel]: [] })[rootLevel];
}
const tree = createTree(arr, { childrenKey: 'nodes' });
data: () => ({
maxSum: 1000,
values: {
v1: 69,
v2: 187,
v3: 666,
v4: -100,
},
},computed: {
valuesCopy() {
return { ...this.values };
},
sum() {
return Object.values(this.values).reduce((acc, n) => acc + n, 0);
},
},watch: {
valuesCopy(newVal, oldVal) {
if (!Number.isFinite(this.sum) || this.sum > this.maxSum) {
this.values = oldVal;
}
},
},Инпуты могут быть в разных компонентах, связаны через Vuex
mutations: {
setValue(state, [ key, val ]) {
const sum = Object
.entries(state.values)
.reduce((acc, n) => acc + (n[0] === key ? val : n[1]), 0);
if (sum <= state.maxSum) {
state.values[key] = val;
} else {
state.values = Object.assign({}, state.values);
}
},
},Vue.component('v-input', {
props: [ 'storeKey' ],
template: `
<input
:value="$store.state.values[storeKey]"
@input="$store.commit('setValue', [ storeKey, +$event.target.value ])"
>`,
});<div v-for="(v, k) in $store.state.values">
{{ k }}: <v-input :store-key="k"></v-input>
</div>
<div>
ещё раз v3: <v-input store-key="v3"></v-input>
</div>
<div>
и v2 тоже: <v-input store-key="v2"></v-input>
</div>
for (int j = i + i;..., наверное всё-таки должно быть i + 1.
const containerSelector = '.calculator_price';
const itemSelector = '.calculator_price__item';
const count = 6;
const key = 'display';
const val = 'flex';$(containerSelector)
.find(`${itemSelector}:lt(${count})`)
.css({ [key]: val });$(`${containerSelector} ${itemSelector}`)
.filter((i, n) => $(n).index() < count)
.css(key, val);for (const n of document.querySelectorAll(containerSelector)) {
for (const m of [...n.querySelectorAll(itemSelector)].slice(0, count)) {
m.style.setProperty(key, val);
}
}document.querySelectorAll(containerSelector).forEach(n => {
n.querySelectorAll(itemSelector).forEach((m, i) => {
m.style[key] = i < count ? val : '';
});
});
v-if / v-else следует добавить элементам массива свойство, которое будет указывать на необходимость назначения класса. Так пойдёт?
<div id="inputs">
<input maxlength="5">
</div>const $inputs = $('#inputs').on('input', 'input', function() {
const $this = $(this);
const maxlen = +$this.attr('maxlength');
if ($this.val().length === maxlen) {
let $next = $this.next();
if (!$next.length) {
$next = $(`<input maxlength="${maxlen}">`).appendTo($inputs);
}
$next.focus();
}
});
// или
document.querySelector('#inputs').addEventListener('input', function(e) {
const input = e.target;
const maxlen = +input.getAttribute('maxlength');
if (input.value.length === maxlen) {
if (!input.nextElementSibling) {
this.insertAdjacentHTML('beforeend', `<input maxlength="${maxlen}">`);
}
input.nextElementSibling.focus();
}
});