以下是以资深软件开发工程师的视角,对“jQuery EasyUI 拖放 – 创建拖放的购物车”的专业回答。我将详细讲解如何使用 jQuery EasyUI 实现一个拖放功能的购物车,包括核心代码、实现步骤和注意事项,结合 HTML5,包含目录、小标题、表格、代码示例和内部链接,确保内容清晰、技术准确且易于理解。回答基于截至 2025 年 3 月 17 日的最新信息,以 jQuery EasyUI v1.10.x(最新稳定版)为基准。


jQuery EasyUI 拖放 – 创建拖放的购物车

目录

  1. 引言
  2. jQuery EasyUI 拖放概述
  1. 实现拖放购物车
  1. 完整示例
  2. 最佳实践与注意事项
  3. 结论

1. 引言

jQuery EasyUI 是一个基于 jQuery 的 UI 框架,提供丰富的组件和交互功能,其中拖放(Drag and Drop)功能可用于创建直观的购物车界面。本教程将引导您使用 jQuery EasyUI 的 draggabledroppable 组件,构建一个支持拖放的购物车,帮助用户选择商品并更新购物篮内容和总价。


2. jQuery EasyUI 拖放概述

2.1 什么是 jQuery EasyUI 拖放?

  • 定义:jQuery EasyUI 的拖放功能通过 draggable(可拖动)和 droppable(可放置)组件实现,允许用户将 HTML 元素从一处拖动到另一处。
  • 核心特性
  • 支持代理(proxy)元素(如克隆效果)。
  • 提供拖动开始、结束和放置事件的回调。
  • 相关组件:结合 datagrid 可展示购物车内容。

2.2 购物车应用场景

  • 目标:用户从商品列表拖动商品到购物车区域,购物车自动更新商品数量和总价。
  • 效果:直观交互,提升用户体验。

3. 实现拖放购物车

3.1 准备工作

  • 依赖
  • jQuery(如 v3.6.0)。
  • jQuery EasyUI(如 v1.10.x)。
  • 引入文件
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script src="https://www.jeasyui.com/easyui/jquery.easyui.min.js"></script>
<link rel="stylesheet" href="https://www.jeasyui.com/easyui/themes/default/easyui.css">
<link rel="stylesheet" href="https://www.jeasyui.com/easyui/themes/icon.css">

3.2 HTML 结构

  • 商品列表:使用 <ul><li> 表示商品。
  • 购物车:使用 datagrid 显示购物篮内容。
<ul class="products">
  <li class="item">
    <img src="shirt1.png" alt="Shirt 1">
    <div>
      <p>衬衫 1</p>
      <p>价格: $25</p>
    </div>
  </li>
  <li class="item">
    <img src="shirt2.png" alt="Shirt 2">
    <div>
      <p>衬衫 2</p>
      <p>价格: $30</p>
    </div>
  </li>
</ul>

<div class="cart">
  <h1>购物车</h1>
  <table id="cartcontent" class="easyui-datagrid" style="width:300px;height:auto;"
         data-options="fitColumns:true,singleSelect:true">
    <thead>
      <tr>
        <th data-options="field:'name',width:140">名称</th>
        <th data-options="field:'quantity',width:60,align:'right'">数量</th>
        <th data-options="field:'price',width:60,align:'right'">价格</th>
      </tr>
    </thead>
  </table>
  <p class="total">总计: $0</p>
  <h2>拖放到此处添加商品</h2>
</div>

3.3 JavaScript 逻辑

  • 初始化数据
var data = { "total": 0, "rows": [] };
var totalCost = 0;
  • 商品可拖动
$('.item').draggable({
  revert: true,         // 拖动失败时返回原位
  proxy: 'clone',      // 使用克隆效果
  onStartDrag: function() {
    $(this).draggable('options').cursor = 'not-allowed';
    $(this).draggable('proxy').css('z-index', 10);
  },
  onStopDrag: function() {
    $(this).draggable('options').cursor = 'move';
  }
});
  • 购物车可放置
$('.cart').droppable({
  onDragEnter: function(e, source) {
    $(source).draggable('options').cursor = 'auto';
  },
  onDragLeave: function(e, source) {
    $(source).draggable('options').cursor = 'not-allowed';
  },
  onDrop: function(e, source) {
    var name = $(source).find('p:eq(0)').html();
    var price = parseFloat($(source).find('p:eq(1)').html().split('$')[1]);
    addProduct(name, price);
  }
});
  • 添加商品逻辑
function addProduct(name, price) {
  for (var i = 0; i < data.total; i++) {
    var row = data.rows[i];
    if (row.name === name) {
      row.quantity += 1;
      $('#cartcontent').datagrid('loadData', data);
      totalCost += price;
      $('div.cart .total').html('总计: $' + totalCost);
      return;
    }
  }
  data.total += 1;
  data.rows.push({ name: name, quantity: 1, price: price });
  totalCost += price;
  $('#cartcontent').datagrid('loadData', data);
  $('div.cart .total').html('总计: $' + totalCost);
}

3.4 样式调整

.products { list-style: none; padding: 0; }
.products li { float: left; margin: 10px; text-align: center; }
.products img { width: 80px; height: 80px; }
.cart { margin-top: 20px; padding: 10px; border: 1px solid #ccc; }
.total { font-weight: bold; margin-top: 10px; }

4. 完整示例

<!DOCTYPE html>
<html lang="zh">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>jQuery EasyUI 拖放购物车</title>
  <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
  <script src="https://www.jeasyui.com/easyui/jquery.easyui.min.js"></script>
  <link rel="stylesheet" href="https://www.jeasyui.com/easyui/themes/default/easyui.css">
  <link rel="stylesheet" href="https://www.jeasyui.com/easyui/themes/icon.css">
  <style>
    .products { list-style: none; padding: 0; }
    .products li { float: left; margin: 10px; text-align: center; }
    .products img { width: 80px; height: 80px; }
    .cart { margin-top: 20px; padding: 10px; border: 1px solid #ccc; }
    .total { font-weight: bold; margin-top: 10px; }
  </style>
</head>
<body>
  <ul class="products">
    <li class="item">
      <img src="shirt1.png" alt="Shirt 1">
      <div>
        <p>衬衫 1</p>
        <p>价格: $25</p>
      </div>
    </li>
    <li class="item">
      <img src="shirt2.png" alt="Shirt 2">
      <div>
        <p>衬衫 2</p>
        <p>价格: $30</p>
      </div>
    </li>
  </ul>

  <div class="cart">
    <h1>购物车</h1>
    <table id="cartcontent" class="easyui-datagrid" style="width:300px;height:auto;"
           data-options="fitColumns:true,singleSelect:true">
      <thead>
        <tr>
          <th data-options="field:'name',width:140">名称</th>
          <th data-options="field:'quantity',width:60,align:'right'">数量</th>
          <th data-options="field:'price',width:60,align:'right'">价格</th>
        </tr>
      </thead>
    </table>
    <p class="total">总计: $0</p>
    <h2>拖放到此处添加商品</h2>
  </div>

  <script>
    var data = { "total": 0, "rows": [] };
    var totalCost = 0;

    $('.item').draggable({
      revert: true,
      proxy: 'clone',
      onStartDrag: function() {
        $(this).draggable('options').cursor = 'not-allowed';
        $(this).draggable('proxy').css('z-index', 10);
      },
      onStopDrag: function() {
        $(this).draggable('options').cursor = 'move';
      }
    });

    $('.cart').droppable({
      onDragEnter: function(e, source) {
        $(source).draggable('options').cursor = 'auto';
      },
      onDragLeave: function(e, source) {
        $(source).draggable('options').cursor = 'not-allowed';
      },
      onDrop: function(e, source) {
        var name = $(source).find('p:eq(0)').html();
        var price = parseFloat($(source).find('p:eq(1)').html().split('$')[1]);
        addProduct(name, price);
      }
    });

    function addProduct(name, price) {
      for (var i = 0; i < data.total; i++) {
        var row = data.rows[i];
        if (row.name === name) {
          row.quantity += 1;
          $('#cartcontent').datagrid('loadData', data);
          totalCost += price;
          $('div.cart .total').html('总计: $' + totalCost);
          return;
        }
      }
      data.total += 1;
      data.rows.push({ name: name, quantity: 1, price: price });
      totalCost += price;
      $('#cartcontent').datagrid('loadData', data);
      $('div.cart .total').html('总计: $' + totalCost);
    }
  </script>
</body>
</html>
  • 运行方法:保存为 shopping-cart.html,替换图片路径后在浏览器中打开。
  • 效果:拖动商品到购物车,表格更新商品数量和总价。

5. 最佳实践与注意事项

  • 图片资源:确保图片路径有效,或使用占位图(如 <img src="https://via.placeholder.com/80">)。
  • 数据持久化:当前示例为前端实现,生产环境需结合后端保存数据。
  • 性能:避免过多商品导致 DOM 操作频繁,可分页加载。
  • 兼容性:测试不同浏览器,确保拖放功能一致。
  • 用户体验:添加移除商品功能(如按钮),增强交互性。

6. 结论

通过 jQuery EasyUI 的 draggabledroppable 组件,您可以轻松实现一个拖放购物车。本教程提供了完整的实现步骤和代码,帮助您理解拖放逻辑。如需扩展功能(如移除商品或后端集成),可参考 jQuery EasyUI 官方文档(jeasyui.com)或进一步提问!


回答特点

  • 结构:包含目录、带锚点的小标题、代码示例,逻辑清晰。
  • 实用性:从基础到实战,覆盖实现购物车所需知识。
  • 内部链接:通过 <a href="#ID"> 跳转,如 实现拖放购物车
  • 出站链接:嵌入正文,指向权威资源。