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

              
                <div class="container">
  <div class="row">
    <div class="col-md-10 mx-auto">
      <div class="card">
        <div class="card-header bg-white fs-5">
          All Products
        </div>
        <div class="card-body">
          <input type="search" class="form-control" id="search-input" placeholder="Search by name">
          <table id="product-table" class="table mt-3"></table>
        </div>
      </div>
    </div>
  </div>
</div>
              
            
!

CSS

              
                
$primary: #2d4263;
$body: #f6f6f6;
$pending: #f5e4c3;
$success: #c5f5d6;
$danger: #eed2ce;

body {
  background: $body;
  margin: 0;
  padding-top: 25px;
}
.badge {
  text-align: center;
  font-size: 14px;
  padding: 4px 8px;
  display: block;
  font-weight: 400;
  color: initial;
  &.success {
    background: $success;
  }
  &.pending {
    background: $pending;
  }
  &.danger {
    background: $danger;
    color: initial;
  }
}
.table {
  thead {
    th {
      background: $primary;
      color: #ffffff;
      font-weight: 600;
    }
  }
}
.table > :not(:last-child) > :last-child > * {
  border-color: transparent;
}
input {
  max-width: 300px;
}
.out-of-stock{
  td{
    color: #8d8787;
  }
}

              
            
!

JS

              
                const data = [
  {
    name: "Smartphone",
    description: "A powerful device for communication and entertainment.",
    price: 399.99,
    quantity: 70,
    status: "In Stock"
  },
  {
    name: "Laptop",
    description: "A portable computer for work and play.",
    price: 799.99,
    quantity: 0,
    status: "Out of Stock"
  },
  {
    name: "Gaming Console",
    description: "Next-gen gaming experience.",
    price: 499.99,
    quantity: 21,
    status: "In Transit"
  },
  {
    name: "Smart TV",
    description: "Immersive viewing experience.",
    price: 799.99,
    quantity: 103,
    status: "In Stock"
  },
  {
    name: "Wireless Headphones",
    description: "High-quality audio on the go.",
    price: 199.99,
    quantity: 85,
    status: "In Stock"
  }
];

const searchInput = document.getElementById("search-input");
const productTable = document.getElementById("product-table");

const createTable = () => {
  productTable.innerHTML = "";

  const tableBody = document.createElement("tbody");
  productTable.appendChild(tableBody);

  const tableHeader = document.createElement("thead");
  const headerRow = document.createElement("tr");

  ["Name", "Description", "Qty", "Status", "Price"].forEach((headerText) => {
    const headerCell = document.createElement("th");
    headerCell.textContent = headerText;
    headerRow.appendChild(headerCell);
  });

  tableHeader.appendChild(headerRow);
  productTable.appendChild(tableHeader);
};

const renderProductRow = (product) => {
  const row = document.createElement("tr");
  row.innerHTML = `
    <td>${product.name}</td>
    <td>${product.description}</td>
    <td>${product.quantity}</td>
    <td>${format.status(product.status)}</td>
    <td>${format.currency(product.price)}</td>
  `;
  product.quantity === 0 && row.classList.add("out-of-stock");
  return row;
};

const renderProducts = (filteredProducts) => {
  createTable();

  if (filteredProducts.length === 0) {
    const noProductsRow = document.createElement("tr");
    const noProductsCell = document.createElement("td");
    noProductsCell.colSpan = 3;
    noProductsCell.textContent = "No products found.";
    noProductsRow.appendChild(noProductsCell);
    productTable.querySelector("tbody").appendChild(noProductsRow);
  } else {
    filteredProducts.forEach((product) =>
      productTable.querySelector("tbody").appendChild(renderProductRow(product))
    );
  }
};

const filterProducts = (query) => {
  const lowercaseQuery = query.toLowerCase();
  return data.filter((product) =>
    product.name.toLowerCase().includes(lowercaseQuery)
  );
};

const format = {
  status: (status) => {
    switch (status) {
      case "In Stock":
        return `<span class='badge success'>${status}</span>`;
      case "Out of Stock":
        return `<span class='badge danger'>${status}</span>`;
      case "In Transit":
        return `<span class='badge pending'>${status}</span>`;
      default:
        return status;
    }
  },
  currency: (value) => {
    const formatter = new Intl.NumberFormat("en-US", {
      style: "currency",
      currency: "USD"
    });
    return formatter.format(value);
  }
};

searchInput.addEventListener("input", () => {
  const query = searchInput.value.trim().toLowerCase();
  const filteredProducts = filterProducts(query);
  renderProducts(filteredProducts);
});

renderProducts(data);

              
            
!
999px

Console