zoukankan      html  css  js  c++  java
  • TP6框架--EasyAdmin学习笔记:实现数据库增删查改

    这是我写的学习EasyAdmin的第三章,这一章我给大家分享下如何进行数据库的增删查改

    上一章链接:点击这里前往

    上一章我们说到,我仿照官方案例,定义了一条路由goodsone和创建了对应数据库,我们可以看到view复制goodsone的文件夹中又这么几个文件

    这些文件中,index.html是我们看到的列表页面,因为easyadmin前端采用的是layui,所有我们看到的内容是这样的

     我们可以看到很明显的layui痕迹,这里中增删改查已经又框架默认方法,路由的格式如上图设置即可

    页面效果如下:

     这里没有layui知识的小伙伴会有一个疑问,页面中的数据和按钮是怎么出来的,上章定义路由的过程中,每一个路由都需要一个对应的js文件,这里的表单和按钮就是在哪里设置的,内容如下:

    define(["jquery", "easy-admin"], function ($, ea) {
     
        var init = {
            table_elem: '#currentTable',
            table_render_id: 'currentTableRenderId',
            index_url: 'mall.goodsone/index',
            add_url: 'mall.goodsone/add',
            edit_url: 'mall.goodsone/edit',
            delete_url: 'mall.goodsone/delete',
            export_url: 'mall.goodsone/export',
            modify_url: 'mall.goodsone/modify',
            stock_url: 'mall.goodsone/stock',
        };
     
        var Controller = {
     
            index: function () {
                ea.table.render({
                    init: init,
                    toolbar: ['refresh',
                        [{
                            text: '添加',
                            url: init.add_url,
                            method: 'open',
                            auth: 'add',
                            class: 'layui-btn layui-btn-normal layui-btn-sm',
                            icon: 'fa fa-plus ',
                            extend: 'data-full="true"',
                        }],
                        'delete', 'export'],
                    cols: [[
                        {type: "checkbox"},
                        {field: 'id',  80, title: 'ID'},
                        {field: 'sort',  80, title: '排序', edit: 'text'},
                        {field: 'cate.title', minWidth: 80, title: '商品分类'},
                        {field: 'title', minWidth: 80, title: '商品名称'},
                        {field: 'logo', minWidth: 80, title: '分类图片', search: false, templet: ea.table.image},
                        {field: 'market_price',  100, title: '市场价', templet: ea.table.price},
                        {field: 'discount_price',  100, title: '折扣价', templet: ea.table.price},
                        {field: 'total_stock',  100, title: '库存统计'},
                        {field: 'stock',  100, title: '剩余库存'},
                        {field: 'virtual_sales',  100, title: '虚拟销量'},
                        {field: 'sales',  80, title: '销量'},
                        {field: 'status', title: '状态',  85, search: 'select',selectList: {0: '禁用', 1: '启用'}, templet: ea.table.switch},
                        {field: 'create_time', minWidth: 80, title: '创建时间'},
                        {
                             250,
                            title: '操作',
                            templet: ea.table.tool,
                            operat: [
                                [{
                                    text: '编辑',
                                    url: init.edit_url,
                                    method: 'open',
                                    auth: 'edit',
                                    class: 'layui-btn layui-btn-xs layui-btn-success',
                                    extend: 'data-full="true"',
                                },
                                // {
                                //     text: '入库',
                                //     url: init.stock_url,
                                //     method: 'open',
                                //     auth: 'stock',
                                //     class: 'layui-btn layui-btn-xs layui-btn-normal',
                                // }
                                ],
                                'delete']
                        }
                    ]],
                });
     
                ea.listen();
            },
            add: function () {
                ea.listen();
            },
            edit: function () {
                ea.listen();
            },
            stock: function () {
                ea.listen();
            },
        };
        return Controller;
    });

    上方的代码大家可以清晰的看到各个增删查改的路由,直接照抄即可,layui大佬可以直接根据项目来修改,而对应的路由代码是放在controller层,代码如下大家而可以参考:

    <?php
     
     
    namespace appadmincontrollermall;
     
     
    use appadminmodelMallGoodsOne;
    use appadmin	raitsCurd;
    use appcommoncontrollerAdminController;
    use EasyAdminannotationControllerAnnotation;
    use EasyAdminannotationNodeAnotation;
    use thinkFacadeDb;
    use thinkApp;
     
    /**
     * Class Goods
     * @package appadmincontrollermall
     * @ControllerAnnotation(title="商城商品管理")
     */
    class GoodsOne extends AdminController
    {
     
        use Curd;
     
        protected $relationSearch = true;
     
        public function __construct(App $app)
        {
            parent::__construct($app);
            $this->model = new MallGoodsOne();
        }
     
        /**
         * @NodeAnotation(title="列表")
         */
        public function index()
        {
            //var_dump($this->request->isAjax());exit();
            if ($this->request->isAjax()) {
                if (input('selectFields')) {
                    return $this->selectList();
                }
                list($page, $limit, $where) = $this->buildTableParames();
                $count = $this->model
                    ->withJoin('cate', 'LEFT')
                    ->where($where)
                    ->count();
                $list = $this->model
                    ->withJoin('cate', 'LEFT')
                    ->where($where)
                    ->page($page, $limit)
                    ->order($this->sort)
                    ->select();
                $data = [
                    'code'  => 0,
                    'msg'   => '',
                    'count' => $count,
                    'data'  => $list,
                ];
                return json($data);
            }
            return $this->fetch();
        }
    }

    如果本文对你有所帮助,麻烦你点个赞,下一章讲下如何在EasyAdmin中用php来实现excel导入表中。

  • 相关阅读:
    .NET 开源工作流: Slickflow流程引擎基础介绍(六)--模块化架构设计和实践
    .NET 开源工作流: Slickflow流程引擎基础介绍(五) -- 会签加签高级特性介绍
    vue实现Excel文件的上传与下载
    库存商品计算成本的几种方法
    如果有一天不做程序员了,还能入什么行业?
    C#使用EF连接PGSql数据库
    回顾2018,展望2019
    Git命令使用大全
    使用VSCode配置简单的vue项目
    SqlServer的两种插入方式效率对比
  • 原文地址:https://www.cnblogs.com/smileZAZ/p/14784088.html
Copyright © 2011-2022 走看看