{this.state.ad.map(n => (
<div key={n.id}>
<h2>{n.title}</h2>
<div>{n.images.map(src => <img src={src} />)}</div>
</div>
))}
const html = [...Array(3)].map((n, i) => `<div class="count">${++i}</div>`).join('');
document.querySelectorAll('.counts').forEach(n => n.insertAdjacentHTML('beforeend', html));
const counts = document.querySelectorAll('.counts');
for (let i = 0; i < counts.length; i++) {
for (let j = 0; j < 3; j++) {
const el = document.createElement('div');
el.className = 'count';
el.innerText = j + 1;
counts[i].appendChild(el);
}
}
const div = document.createElement('div');
div.classList.add('count');
const fragment = document.createDocumentFragment();
fragment.append(...Array.from({ length: 3 }, (n, i) => (
n = div.cloneNode(),
n.textContent = -~i,
n
)));
for (const n of document.querySelectorAll('.counts')) {
n.append(fragment.cloneNode(true));
}
state: {
page: 0,
perPage: 5,
total: 0,
posts: [],
loading: false,
},
getters: {
numPages: state => Math.ceil(state.total / state.perPage),
},
mutations: {
updateLoading: (state, loading) => state.loading = loading,
updatePosts: (state, { posts, total, page }) => Object.assign(state, { posts, total, page }),
},
actions: {
async fetchPosts({ state, commit }, page) {
commit('updateLoading', true);
const start = (page - 1) * state.perPage;
const end = page * state.perPage;
const url = `https://jsonplaceholder.typicode.com/posts?_start=${start}&_end=${end}`;
try {
const response = await fetch(url);
const posts = await response.json();
const total = response.headers.get('x-total-count');
commit('updatePosts', { posts, total, page });
} catch (e) {
console.error(e);
}
commit('updateLoading', false);
},
},
props: [ 'page', 'numPages' ],
methods: {
goTo(page) {
this.$emit('paginate', page);
},
next(step) {
this.goTo(this.page + step);
},
},
computed: {
isFirst() {
return this.page <= 1;
},
isLast() {
return this.page >= this.numPages;
},
},
<button @click="goTo(1)" :disabled="isFirst"><<</button>
<button @click="next(-1)" :disabled="isFirst"><</button>
{{ page }} / {{ numPages }}
<button @click="next(+1)" :disabled="isLast">></button>
<button @click="goTo(numPages)" :disabled="isLast">>></button>
v-model
:model: {
prop: 'page',
event: 'paginate',
},
computed: {
page: {
get() {
return this.$store.state.page;
},
set(page) {
this.$store.dispatch('fetchPosts', page);
},
},
<компонент-пагинации
v-model="page"
:num-pages="$store.getters.numPages"
/>
created() {
this.page = 1;
},
<div v-if="$store.state.loading">данные загружаются, ждите</div>
<компонент-для-отображения-данных v-else :данные="$store.state.posts" />
const siblings = Array.prototype.filter.call(
this.parentNode.children,
n => n !== this
);
const siblings = [];
for (let el = this; (el = el.previousElementSibling); siblings.unshift(el)) ;
for (let el = this; (el = el.nextElementSibling); siblings.push(el)) ;
$('.openFormPopup').click(e => $('#btn-clicked').val(e.target.dataset.title));
Здесь только регуляркой или как?
function incrementNumberInString(str) {
for (let i = 0, iNum = -1; i < str.length; i++) {
if ('0123456789'.includes(str[i])) {
if (iNum === -1 && str[i - 1] === '[') {
iNum = i;
}
} else if (iNum !== -1 && str[i] === ']') {
return str.slice(0, iNum) + (Number(str.slice(iNum, i)) + 1) + str.slice(i);
} else {
iNum = -1;
}
}
return str;
}
str.replace(/(?<=\[)\d+(?=\])/, m => ++m)
// или
str.replace(/\[\d+\]/, m => '[' + (m.slice(1, -1) - -1) + ']')
// или
str.replace(/\[(\d+)\]/, (m, g1) => `[${-~g1}]`)
как лучше реализовать функционал, чтобы иконка была одна, а в нужных компонентах мы докидывали цвет?
выводит побуквенно
v-for
так и перебирает - "побуквенно". То есть, значениями user являются строки единичной длины. Ну а свойств id, name, price и т.д. у строк нет, отсюда пустота там, где вы выводите свойства элементов users. почему не удаляется item, а добавляется?
slice(0, -1)
) плюс полная копия (slice(0)
).deleteItem = id => {
this.setState(({ todoData }) => ({
todoData: todoData.filter(n => n.id !== id),
}));
}