Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Auto Save

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <header>
	jquery-data-api
	<span>para harcama örneği</span>
</header>

<div class="container">

	<div class="total-money" data-class="[$state.money - $state.total === 0, 'empty']" data-block>
		<div data-show="$state.money - $state.total !== 0">
			Harcamak için {moneyFormat($state.money - $state.total)} paranız var!
		</div>
		<div data-show="$state.money - $state.total === 0">
			Paran bitti, parasız insan boş insandır!
		</div>
	</div>

	<ul data-for="items" data-as="item" class="items">
		<template>
			<li>
				<div class="title">
					{item.name}
				</div>
				<div class="price">
					{moneyFormat(item.price)}
				</div>
				<div class="actions">
					<button data-disabled="!$state.basket.find(i => i.id === {item.id})" onclick="removeBasket({item})" class="sell-btn">Sat</button>
					<div class="amount" data-expression="$state.basket.find(item => item.id === {item.id})?.amount || 0"></div>
					<button data-disabled="$state.total + {item.price} > $state.money" onclick="addBasket({item})" class="buy-btn">Al</button>
				</div>
			</li>
		</template>
	</ul>

	<div class="basket" data-show="$state.basket && $state.basket.length">
		<h3>Alışveriş Detayları</h3>
		<ul data-for="basket" data-as="item">
			<template>
				<li class="basket-item">
					{getProduct(item.id).name} <span>x {item.amount}</span>
				</li>
			</template>
		</ul>
		<div class="total" data-block>
			Toplam: {moneyFormat($state.total)}
		</div>
		<button class="basket-reset-btn" onClick="resetBasket()">Sepeti Sıfırla</button>
	</div>

</div>

<script>
const moneyFormat = (price) => {
		return new Intl.NumberFormat('tr-TR', { style: 'currency', currency: 'TRY' }).format(price);
}
</script>
              
            
!

CSS

              
                * {
    padding: 0;
    margin: 0;
    list-style: none;
    border: 0;
    box-sizing: border-box;
    text-decoration: none;
}

body {
    background: #eee;
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif
}

header {
    height: 60px;
    background: #fff;
    border-bottom: 1px solid #ddd;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 18px;
    font-weight: 600;
    margin-bottom: 20px;
}
header span {
    margin-left: 15px;
    padding-left: 15px;
    border-left: 1px solid #ddd;
    color: rgb(39, 74, 230);
}

.container {
    width: 800px;
    max-width: 100%;
    margin: 0 auto;
    background: #fff;
    padding: 15px;
    border-radius: 3px;
    box-shadow: 3px 5px 10px 0 rgba(0,0,0,.1);
}

.items {
    display: flex;
    flex-wrap: wrap;
    gap: 15px;
}
.items li {
    width: calc(50% - 15px);
}
.items li .title {
    font-size: 16px;
    font-weight: 600;
    margin-bottom: 5px;
}
.items li .price {
    color: rgb(39, 74, 230);
}

.items li .actions {
    display: flex;
    margin-top: 10px;
    align-items: center;
}
.items li .actions .amount {
    min-width: 50px;
    text-align: center;
}
.items li .actions button {
    flex: 1;
    height: 30px;
    border-radius: 4px;
    font-size: 14px;
    cursor: pointer;
}
.items li .actions button.sell-btn {
    background: #ccc;
}
.items li .actions button.buy-btn {
    background: #61dafb;
}

[disabled] {
    opacity: .4;
    pointer-events: none;
}

.total-money {
    background: linear-gradient(to bottom, #20b820, #14be2a);
    padding: 15px;
    text-align: center;
    color: #fff;
    margin-bottom: 25px;
    border-radius: 4px;
}
.total-money.empty {
    background: red
}

.basket {
    padding-top: 20px;
    margin-top: 20px;
    border-top: 1px solid #ddd;
}
.basket h3 {
    margin-bottom: 15px;
}
.basket ul {
    padding-bottom: 15px;
    margin-bottom: 15px;
    border-bottom: 1px solid #ddd;
}
.basket ul li {
    margin-bottom: 5px;
}
.basket ul li span {
    color: #999;
}
.basket .total {
    font-size: 18px;
    font-weight: bold;
    text-align: right;
    color: #179b17;
}
.basket .basket-reset-btn {
    background: #61dafb;
    height: 40px;
    padding: 0 20px;
    font-size: 16px;
    font-weight: 500;
    cursor: pointer;
}
              
            
!

JS

              
                const getAmount = (id) => {
    const find = $state.basket.find(item => item.id === id)
    if (find) {
        return find.amount;
    }
    return 0;
}

const addBasket = (product) => {
    const checkBasket = $state.basket.find(item => item.id === product.id)

    // ürün daha önce eklenmiş
    if (checkBasket) {
        checkBasket.amount += 1
        updateState('basket', [...$state.basket.filter(item => item.id !== product.id), checkBasket])
    } else {
        updateState('basket', [...$state.basket, {
            id: product.id,
            amount: 1
        }])
    }
}

const removeBasket = (product) => {
    const currentBasket = $state.basket.find(item => item.id === product.id)
    const basketWithoutCurrent = $state.basket.filter(item => item.id !== product.id)
    currentBasket.amount -= 1
    if (currentBasket.amount === 0) {
        updateState('basket', [...basketWithoutCurrent])
    } else {
        updateState('basket', [...basketWithoutCurrent, currentBasket])
    }
}

const resetBasket = () => {
    updateState('basket', []);
}

const getProduct = (id) => {
    return $state.items.find(p => p.id === id)
}

setState('basket', []);
setState('total', 0);
setState('money', 750000);
const items = [
	{
		id: 1,
		name: 'Ekmek',
		price: 1
	},
	{
		id: 2,
		name: 'Ayakkabı',
		price: 50
	},
	{
		id: 3,
		name: 'Tişört',
		price: 25
	},
	{
		id: 4,
		name: 'Motor',
		price: 5500
	},
	{
		id: 5,
		name: 'Araba',
		price: 100000
	}
];
setState('items', items);

stateEffect((basket) => {
	updateState(
		'total',
		basket.reduce((acc, item) => {
			return acc + (item.amount * ($state.items.find(product => product.id === item.id).price))
		}, 0)
	)
}, ['basket'])

// eğer etkilenecek state varsa diğer stateEffect'ten önce tanımlamak gerekiyor
// bu örnekte basket stateini güncelleyip yukarıda ise güncellenen değeri alabiliyoruz
stateEffect(() => {
	const basket = [
		{
			id: 3,
			amount: 2
		}
	];
	updateState('basket', basket);
});
              
            
!
999px

Console