当前位置: 首页 > article >正文

jEasyUI 创建 CRUD 应用

jEasyUI 创建 CRUD 应用

引言

CRUD(Create, Read, Update, Delete)是数据库操作中常见的一组基本操作,即创建、读取、更新和删除数据。jEasyUI 是一款流行的 jQuery UI 组件库,它提供了丰富的界面组件和功能,可以帮助开发者快速构建丰富的 Web 应用。本文将介绍如何使用 jEasyUI 创建一个基本的 CRUD 应用。

环境准备

在开始之前,请确保您的开发环境已经安装了以下工具:

  • jQuery: jEasyUI 是基于 jQuery 的,因此需要先引入 jQuery 库。
  • jEasyUI: 下载并引入 jEasyUI 库。
  • 数据库: 本文以 MySQL 为例,创建一个简单的数据库和表。

创建数据库和表

以下是一个简单的示例,用于创建一个名为 users 的数据库表,包含 idnameemail 三个字段。

CREATE DATABASE users;

USE users;

CREATE TABLE users (
    id INT AUTO_INCREMENT PRIMARY KEY,
    name VARCHAR(50) NOT NULL,
    email VARCHAR(100) NOT NULL
);

引入 jEasyUI 库

在您的 HTML 文件中引入 jQuery 和 jEasyUI 库:

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>jEasyUI CRUD 应用</title>
    <link rel="stylesheet" type="text/css" href="https://www.jeasyui.com/easyui/themes/default/easyui.css">
    <script type="text/javascript" src="https://www.jeasyui.com/easyui/jquery.min.js"></script>
    <script type="text/javascript" src="https://www.jeasyui.com/easyui/jquery.easyui.min.js"></script>
</head>
<body>
    <!-- CRUD 应用代码 -->
</body>
</html>

创建 CRUD 应用

以下是使用 jEasyUI 创建 CRUD 应用的步骤:

1. 创建表单

创建一个表单,用于添加和编辑用户信息。

<div id="editForm" style="padding:10px;">
    <form id="userForm">
        <table>
            <tr>
                <td>ID:</td>
                <td>
                    <input id="id" class="easyui-numberbox" data-options="required:true">
                </td>
            </tr>
            <tr>
                <td>Name:</td>
                <td>
                    <input id="name" class="easyui-textbox" data-options="required:true">
                </td>
            </tr>
            <tr>
                <td>Email:</td>
                <td>
                    <input id="email" class="easyui-textbox" data-options="required:true, validType:'email'">
                </td>
            </tr>
        </table>
    </form>
</div>

2. 创建 CRUD 控件

创建一个表格,用于显示用户信息。

<div id="userGrid" style="padding:10px;">
    <table id="dg" title="User List" class="easyui-datagrid" style="width:100%;height:250px"
        url="data/users.datagrid" pagination="true" rownumbers="true">
        <thead>
            <tr>
                <th field="id" width="50">ID</th>
                <th field="name" width="100">Name</th>
                <th field="email" width="150">Email</th>
            </tr>
        </thead>
    </table>
</div>

3. 添加 CRUD 功能

为 CRUD 控件添加添加、编辑、删除和搜索功能。

$(function(){
    $('#dg').datagrid({
        toolbar: [{
            text: 'Add',
            iconCls: 'icon-add',
            handler: function(){
                $('#editForm').form('clear');
                $('#editForm').dialog('open');
            }
        }, {
            text: 'Edit',
            iconCls: 'icon-edit',
            handler: function(){
                var row = $('#dg').datagrid('getSelected');
                if (row){
                    $('#editForm').form('load', row);
                    $('#editForm').dialog('open');
                } else {
                    $.messager.show({
                        title: 'Error',
                        msg: 'Please select a row to edit.'
                    });
                }
            }
        }, {
            text: 'Delete',
            iconCls: 'icon-remove',
            handler: function(){
                var row = $('#dg').datagrid('getSelected');
                if (row){
                    $.messager.confirm('Confirm', 'Are you sure you want to delete this user?', function(r){
                        if (r){
                            $.ajax({
                                type: 'POST',
                                url: 'data/users.delete',
                                data: {id: row.id},
                                success: function(data){
                                    if (data.success){
                                        $('#dg').datagrid('reload');
                                    } else {
                                        $.messager.show({
                                            title: 'Error',
                                            msg: data.message
                                        });
                                    }
                                }
                            });
                        }
                    });
                } else {
                    $.messager.show({
                        title: 'Error',
                        msg: 'Please select a row to delete.'
                    });
                }
            }
        }, {
            text: 'Search',
            iconCls: 'icon-search',
            handler: function(){
                var name = $('#searchName').val();
                $('#dg').datagrid('load', {name: name});
            }
        }]
    });

    $('#editForm').dialog({
        title: 'Edit User',
        width: 300,
        closed: true,
        buttons: [{
            text: 'Save',
            iconCls: 'icon-ok',
            handler: function(){
                $('#userForm').form('submit', {
                    url: 'data/users.save',
                    onSubmit: function(){
                        return $(this).form('validate');
                    },
                    success: function(data){
                        var data = $.parseJSON(data);
                        if (data.success){
                            $('#editForm').dialog('close');
                            $('#dg').datagrid('reload');
                        } else {
                            $.messager.show({
                                title: 'Error',
                                msg: data.message
                            });
                        }
                    }
                });
            }
        }, {
            text: 'Cancel',
            iconCls: 'icon-cancel',
            handler: function(){
                $('#editForm').dialog('close');
            }
        }]
    });
});

4. 数据处理

创建 data/users.datagriddata/users.savedata/users.delete 文件,用于处理 CRUD 操作。

// data/users.datagrid
{
    "total": 2,
    "rows": [
        {"id": 1, "name": "John", "email": "john@example.com"},
        {"id": 2, "name": "Jane", "email": "jane@example.com"}
    ]
}

// data/users.save
{
    "success": true,
    "message": "User saved successfully."
}

// data/users.delete
{
    "success": true,
    "message": "User deleted successfully."
}

总结

通过以上步骤,您可以使用 jEasyUI 创建一个基本的 CRUD 应用。在实际项目中,您可以根据需求扩展功能,如添加图片上传、多表关联等。希望本文对您有所帮助!


http://www.kler.cn/a/527453.html

相关文章:

  • DBeaver连接MySQL提示Access denied for user ‘‘@‘ip‘ (using password: YES)的解决方法
  • 毕业设计--具有车流量检测功能的智能交通灯设计
  • 内外网文件摆渡企业常见应用场景和对应方案
  • 模型I/O
  • 一文掌握ADB的安装及使用
  • 知识管理系统塑造企业文化与学习型组织的变革之路
  • 安卓安全访问配置说明network-security-config —未来之窗跨平台操作
  • 【搞定offer】远程医疗:健康科技领域,搞定医疗offer
  • 2501,编写dll
  • 大语言模型(LLM)模拟金融市场参与者行为
  • 离线大模型-通义千问
  • 栈和队列特别篇:栈和队列的经典算法问题
  • ### 2024 江西省赛题解(A,C,D,G,H,J,K,L) BEFI待补
  • qt-Quick3D笔记之官方例程Runtimeloader Example运行笔记
  • 合并2个排序的链表
  • DeepSeek是什么,最近到底经历了什么?它能干什么?
  • 注册谷歌账号
  • Linux:多线程[2] 线程控制
  • 10.5 LangChain Model I/O 深度解析:如何用标准化接口打通大模型开发的“任督二脉”?
  • 2 MapReduce
  • 【思维导图】并发编程
  • 答疑解惑:如何监控EMC unity存储系统磁盘重构rebuild进度
  • 实战:利用百度站长平台加速网站收录
  • Agent 高频知识汇总:查漏补缺参考大全
  • 大模型本地化部署(Ollama + Open-WebUI)
  • 《TCP 网络编程实战:开发流程、缓冲区原理、三次握手与四次挥手》