HTML preprocessors can make writing HTML more powerful or convenient. For instance, Markdown is designed to be easier to write and read for text documents and you could write a loop in Pug.
In CodePen, whatever you write in the HTML editor is what goes within the <body>
tags in a basic HTML5 template. So you don't have access to higher-up elements like the <html>
tag. If you want to add classes there that can affect the whole document, this is the place to do it.
In CodePen, whatever you write in the HTML editor is what goes within the <body>
tags in a basic HTML5 template. If you need things in the <head>
of the document, put that code here.
The resource you are linking to is using the 'http' protocol, which may not work when the browser is using https.
CSS preprocessors help make authoring CSS easier. All of them offer things like variables and mixins to provide convenient abstractions.
It's a common practice to apply CSS to a page that styles elements such that they are consistent across all browsers. We offer two of the most popular choices: normalize.css and a reset. Or, choose Neither and nothing will be applied.
To get the best cross-browser support, it is a common practice to apply vendor prefixes to CSS properties and values that require them to work. For instance -webkit-
or -moz-
.
We offer two popular choices: Autoprefixer (which processes your CSS server-side) and -prefix-free (which applies prefixes via a script, client-side).
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.
You can apply CSS to your Pen from any stylesheet on the web. Just put a URL to it here and we'll apply it, in the order you have them, before the CSS in the Pen itself.
You can also link to another Pen here (use the .css
URL Extension) and we'll pull the CSS from that Pen and include it. If it's using a matching preprocessor, use the appropriate URL Extension and we'll combine the code before preprocessing, so you can use the linked Pen as a true dependency.
JavaScript preprocessors can help make authoring JavaScript easier and more convenient.
Babel includes JSX processing.
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.
You can apply a script from anywhere on the web to your Pen. Just put a URL to it here and we'll add it, in the order you have them, before the JavaScript in the Pen itself.
If the script you link to has the file extension of a preprocessor, we'll attempt to process it before applying.
You can also link to another Pen here, and we'll pull the JavaScript from that Pen and include it. If it's using a matching preprocessor, we'll combine the code before preprocessing, so you can use the linked Pen as a true dependency.
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.
Using packages here is powered by esm.sh, which makes packages from npm not only available on a CDN, but prepares them for native JavaScript ESM usage.
All packages are different, so refer to their docs for how they work.
If you're using React / ReactDOM, make sure to turn on Babel for the JSX processing.
If active, Pens will autosave every 30 seconds after being saved once.
If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.
If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.
Visit your global Editor Settings.
<div class="container">
<div class="row" id="content">
</div>
<div class="d-flex justify-content-center">
<nav aria-label="Page navigation example">
<ul class="pagination" id="pageid">
</ul>
</nav>
</div>
</div>
const jsonUrl = 'https://raw.githubusercontent.com/hsiangfeng/JSHomeWork/master/JSON/datastore_search.json';
const content = document.getElementById('content');
const pageid = document.getElementById('pageid');
let jsonData = {};
fetch(jsonUrl, {method: 'get'})
.then((response) => {
return response.json();
}).then((data) => {
jsonData = data.result.records;
pagination(jsonData, 1);
})
function pagination(jsonData, nowPage) {
console.log(nowPage);
// 取得全部資料長度
const dataTotal = jsonData.length;
// 設定要顯示在畫面上的資料數量
// 預設每一頁只顯示 5 筆資料。
const perpage = 5;
// page 按鈕總數量公式 總資料數量 / 每一頁要顯示的資料
// 這邊要注意,因為有可能會出現餘數,所以要無條件進位。
const pageTotal = Math.ceil(dataTotal / perpage);
// 當前頁數,對應現在當前頁數
let currentPage = nowPage;
// 因為要避免當前頁數筆總頁數還要多,假設今天總頁數是 3 筆,就不可能是 4 或 5
// 所以要在寫入一個判斷避免這種狀況。
// 當"當前頁數"比"總頁數"大的時候,"當前頁數"就等於"總頁數"
// 注意這一行在最前面並不是透過 nowPage 傳入賦予與 currentPage,所以才會寫這一個判斷式,但主要是預防一些無法預期的狀況,例如:nowPage 突然發神經?!
if (currentPage > pageTotal) {
currentPage = pageTotal;
}
// 由前面可知 最小數字為 6 ,所以用答案來回推公式。
const minData = (currentPage * perpage) - perpage + 1 ;
const maxData = (currentPage * perpage) ;
// 先建立新陣列
const data = [];
// 這邊將會使用 ES6 forEach 做資料處理
// 首先必須使用索引來判斷資料位子,所以要使用 index
jsonData.forEach((item, index) => {
// 獲取陣列索引,但因為索引是從 0 開始所以要 +1。
const num = index + 1;
// 這邊判斷式會稍微複雜一點
// 當 num 比 minData 大且又小於 maxData 就push進去新陣列。
if ( num >= minData && num <= maxData) {
data.push(item);
}
})
// 用物件方式來傳遞資料
const page = {
pageTotal,
currentPage,
hasPage: currentPage > 1,
hasNext: currentPage < pageTotal,
}
displayData(data);
pageBtn(page);
}
function displayData(data) {
let str = '';
data.forEach((item) => {
str += `<div class="col-md-6 py-2 px-1">
<div class="card">
<div class="card bg-dark text-white text-left">
<img class="card-img-top bg-cover" height="155px" src="${item.Picture1}">
<div class="card-img-overlay d-flex justify-content-between align-items-end p-0 px-3" style="background-color: rgba(0, 0, 0, .2)">
<h5 class="card-img-title-lg">${item.Name}</h5><h5 class="card-img-title-sm">${item.Zone}</h5>
</div>
</div>
<div class="card-body text-left">
<p class="card-p-text"><i class="far fa-clock fa-clock-time"></i> ${item.Opentime}</p>
<p class="card-p-text"><i class="fas fa-map-marker-alt fa-map-gps"></i> ${item.Add}</p>
<div class="d-flex justify-content-between align-items-end">
<p class="card-p-text"><i class="fas fa-mobile-alt fa-mobile"></i> ${item.Tel}</p>
<p class="card-p-text"><i class="fas fa-tags text-warning"></i> ${item.Ticketinfo}</p>
</div>
</div>
</div>
</div>`;
});
content.innerHTML = str;
}
function pageBtn (page){
let str = '';
const total = page.pageTotal;
if(page.hasPage) {
str += `<li class="page-item"><a class="page-link" href="#" data-page="${Number(page.currentPage) - 1}">Previous</a></li>`;
} else {
str += `<li class="page-item disabled"><span class="page-link">Previous</span></li>`;
}
for(let i = 1; i <= total; i++){
if(Number(page.currentPage) === i) {
str +=`<li class="page-item active"><a class="page-link" href="#" data-page="${i}">${i}</a></li>`;
} else {
str +=`<li class="page-item"><a class="page-link" href="#" data-page="${i}">${i}</a></li>`;
}
};
if(page.hasNext) {
str += `<li class="page-item"><a class="page-link" href="#" data-page="${Number(page.currentPage) + 1}">Next</a></li>`;
} else {
str += `<li class="page-item disabled"><span class="page-link">Next</span></li>`;
}
pageid.innerHTML = str;
}
function switchPage(e){
e.preventDefault();
if(e.target.nodeName !== 'A') return;
const page = e.target.dataset.page;
pagination(jsonData, page);
}
pageid.addEventListener('click', switchPage);
Also see: Tab Triggers