zoukankan      html  css  js  c++  java
  • 重用思想,要有识别出好代码的眼睛,识别出腐朽代码的眼睛

    通用思想!
    很多状态,只有两种,是,否!
    这个时候,就可以整一个通用的功能!

     $('.change_status').on('click',function(){
                var _this = $(this);
                var id = $(this).parent().data('id');
                var type = $(this).data('type');
                var q = $(this).data('q');
                var table = 'home_image_text';
                var set_val;
    
                if(q == '1'){
                    set_val = 0;
                }
                if(q == '0'){
                    set_val = 1;
                }
                $.ajax({
                    type:'POST',
                    url:'__APP__/Common/set',
                    data:{'id':id,'type':type,'set_val':set_val,'table':table},
                    dataType:'json',
                    success:function(data){
                        if(data.errno == 0){
                            _this.data('q',set_val);
                            if(set_val ==1){
                                _this.html('<i class="fa fa-check erbi-color-green"></i>');
                            }else{
                                _this.html('<i class="fa fa-times erbi-color-red"></i>');
                            }
                        }
                    },
                    error:function(data){
                        alert("网络错误");
                    }
                });
    });
    

    后台写在Common里,根据传入的表,进行设置。

    public function set(){
            vendor('Func.Json');
            $json = new Json();
            $id = (int)$_POST['id'];
            $type = trim($_POST['type']);
            $set_val = (int)$_POST['set_val'];
            $table = trim($_POST['table']);
    
            if (!$id || !$type){
                $json->setErr('10001','缺少参数');
                $json->Send();
            }
            $model_table = M($table);
            if(!$model_table->find()){
                $json->setErr('10002','缺少正确的表格信息');
                $json->Send();
            }
            $flag = $model_table->where(array('id'=>$id))->find();
            if (!$flag){
                $json->setErr('10003','没有该选项');
                $json->Send();
            }
            $data[$type] = $set_val;
            $save_flag = $model_table->where(array('id'=>$id))->save($data);
            if ($save_flag || $save_flag === 0){
                $json->setErr(0,'更新成功');
                $json->Send();
            }else{
                $json->setErr('10004','编辑失败');
                $json->Send();
            }
    }
    

    这种思想很赞!思想!就是少写代码,多重用一些代码。

  • 相关阅读:
    [Notes] 如何使用abode audition录歌
    [Tips] matlab save load
    [Tips] matlab csv格式文件读写
    [Tips] 随机数 随机序列 随机排列生成
    [Tips] csv 读写
    [record] 初入android
    HTML中表格table边框border(1px还嫌粗)的解决方案:
    CSS颜色代码大全
    ie9下面的console的bug
    js 性能优化 篇一
  • 原文地址:https://www.cnblogs.com/jiqing9006/p/9457328.html
Copyright © 2011-2022 走看看