• 第15课 栏目的排序处理(组件化) Thinkphp5商城第四季


    目录

    要实现的功能

    用表单里的提交过来的sort数据,批量修改表里的排序值
    界面效果:

    思路:

    1. 视图层表单提交数据主键=>sort值
    2. 控制器调用扩展类里的 public function cateSort($data, $obj) 方法
      //处理栏目排序 $data数组,$obj是实例化的表的对象
    3. cateSort验证数据并修改表里sort的值,如果提交的不是大于0的整数,就报错.

    视图层

    提交的数据排序数据是要 主键=>sort值

    <input type="text" style="60px; text-align:center;" name="sort[{$cate.id}]" value="{$cate.sort}" />

     <form action="" method="post">
                        <table class="table table-bordered table-hover">
                            <thead class="">
                                <tr>
                                    <th class="text-center" width="8%">ID</th>
                                    <th class="text-center">分类名称</th>
                                    <th class="text-center" width="10%">类型</th>
                                    <th width="8%">显示到导航栏</th>
                                    <th class="text-center" width="8%">排序</th>
                                    <th class="text-center" width="14%">操作</th>
                                </tr>
                            </thead>
                            <tbody>
                                    {volist name="cateRes" id="cate"}
                                    <tr>
                                        <td align="center">{$cate.id}</td>
                                        <td><?php echo str_repeat('-', $cate['level']*8)?>{$cate.cate_name}</td>
                                        <td align="center">
                                        {if condition="$cate['cate_type'] eq 1"}
                                        系统分类
                                        {elseif condition="$cate['cate_type'] eq 2" /}
                                        帮助分类
                                        {elseif condition="$cate['cate_type'] eq 3" /}
                                        网店帮助
                                        {elseif condition="$cate['cate_type'] eq 4" /}
                                        网店信息
                                        {else /}
                                        普通分类
                                        {/if}
                                        </td>
                                        <td align="center">
                                        {if condition="$cate['show_nav'] eq 1"}
                                        <img src="__admin__/images/right.png" height="30"> 
                                        {else /}
                                            <img src="__admin__/images/wrong.png" height="25"> 
                                        {/if}
                                        </td>
                                        <td align="center">
                                        <input type="text" style="60px; text-align:center;" name="sort[{$cate.id}]" value="{$cate.sort}" />
                                        </td>
                                        <td align="center">
                                            <a href="{:url('Cate/edit',array('id'=>$cate['id']))}" class="btn btn-primary btn-sm shiny">
                                                <i class="fa fa-edit"></i> 编辑
                                            </a>
                                            {notin name="$cate['id']" value="1,2,3"}
                                            <a href="#" onClick="warning('确实要删除吗', '{:url('Cate/del',array('id'=>$cate['id']))}')"  class="btn btn-danger btn-sm shiny">
                                                <i class="fa fa-trash-o"></i> 删除
                                            </a>
                                            {else /}
                                            <a href="#" disabled="disabled" class="btn btn-danger btn-sm shiny">
                                                <i class="fa fa-trash-o"></i> 删除
                                            </a>
                                            {/notin}
                                        </td>
                                    </tr>
                                    {/volist}
                                    <tr><td colspan="6" align="right" style="padding-right:16.5%;"><input class="btn btn-primary btn-sm shiny" type="submit" value="提交" /></td></tr>
                            </tbody>
                        </table>
                        </form>
    

    打印提交的数据:

    D:xampphtdocsshop hinkphplibrary hinkDebug.php:165:
    array (size=1)
    'sort' =>
    array (size=17)
    20 => string '4' (length=1)
    22 => string '3' (length=1)
    21 => string '3' (length=1)
    1 => string '2' (length=1)
    3 => string '12' (length=2)
    2 => string '11' (length=2)
    4 => string '55' (length=2)
    5 => string '54' (length=2)
    7 => string '53' (length=2)
    16 => string '52' (length=2)
    17 => string '51' (length=2)
    23 => string '2' (length=1)
    25 => string '50' (length=2)
    24 => string '50' (length=2)
    19 => string '1' (length=1)
    18 => string '1' (length=1)
    26 => string '1' (length=1)

    控制器里:

    public function lst()
        {
            $Category = new Catetree();
            $CategoryObj = db('Category');
            if (request()->isPost()) {
                $data = input('post.');
                $cateres = $Category->cateSort($data['sort'], $CategoryObj);
                if ($cateres === false) {
                    $this->error('只能填写大于0的数字');
                }
                $this->success('排序成功!', url('lst'));
            }
            $CategoryRes = $CategoryObj->order('sort DESC')->select();
            $CategoryRes = $Category->Catetree($CategoryRes);
            $this->assign([
                'CategoryRes' => $CategoryRes,
            ]);
            return view('list');
        }
    

    扩展函数里

    D:xampphtdocsshopextendcatetreeCatetree.php

    <?php
    namespace catetree;
    
    use thinkValidate;
    
    class Catetree
    {
       //处理栏目排序 $data数组,$obj是实例化的表的对象
        public function cateSort($data, $obj)
        {
            foreach ($data as $k => $v) {
                 //数组里的排序值只能填写大于0的数字
                $is_num = (!empty(intval($v)) && $v > 0 && Validate::is(intval($v), 'integer')) ? 1 : 0;
                // dump($is_num);
                if ($is_num == 0) {
                    return false;
                }
                $obj->update(['id' => $k, 'sort' => $v]);
            }
        }
    
  • 相关阅读:
    [bzoj 2460]线性基+贪心+证明过程
    [Wc2011] Xor
    [BZOJ2844]线性基+xor本质不同第K大
    洛谷3857 [TJOI2008]彩灯
    HDU3949 异或线性基
    hdu3062 party --2-sat
    KM算法详解+模板
    Hopcroft-Karp算法
    bzoj 1135: [POI2009]Lyz
    hall定理的证明
  • 原文地址:https://www.cnblogs.com/haima/p/9672400.html
Copyright © 2020-2023  润新知