抱歉,您的浏览器无法访问本站

本页面需要浏览器支持(启用)JavaScript


了解详情 >

碎片知识

js里怎么取到config里的数据

以token为例,这里先在后端把数据写入config,然后在前端js里就会有我们设置的token了

1.后端php部分
$this->assignconfig("token", $this->auth->token);
2.前端js部分
console.log(Config.token)

通过变量Config可以取到你想要的东西

行内编辑 editable

注意在使用editable配置时,formatter渲染请删除
文档地址:https://vitalets.github.io/x-editable/docs.html

1.首先需要安装一下插件

https://www.fastadmin.net/store/editable.html

2.js头部追加一个editable

define(['jquery', 'bootstrap', 'backend', 'table', 'form', 'editable'], function ($, undefined, Backend, Table, Form, undefined) {

3.在columns的字段里加上 editable:true 即可生效

4.editable的更多配置

  • 普通文本,并能限制必填,成功回调
    20211117100621
editable: {
    type: 'text',
    title: '这里是标题',
    // showbuttons:false,
    // clear:false,
    // placeholder:'提示输入点什么',
    validate: function (value) {
        if (!$.trim(value)) {
                return '不能为空';
        }
    },
    success: function(response, newValue) {
        console.log('newValue: ', newValue);
        console.log('response: ', response);                                
    }
  },
  • textarea 文本域
editable: {
    type: 'textarea',
    rows:3
}
  • select下拉菜单,并且支持子菜单
    20211117100655
    editable: {
    type: 'select',
    pk: 1,
    source: [
        {value: '1', text: '显示'},
        {value: '2', text: '隐藏'},
    ],
    // source:[
    //     {text: "group1", children: [{value: 1, text: "text1"}, {value: 2, text: "text2"}]},
    //     {text: "group2", children: [{value: 2, text: "text2"}, {value: 3, text: "text3"}]},
    // ]
    }
    
  • 日期时间

    weekStart: 1 星期几为第一天

    editable:{
    type: 'date',
    format: 'yyyy-mm-dd',    
    viewformat: 'dd/mm/yyyy',    
    datepicker: {
      weekStart: 1
    }
    }
    
  • 可指定任意元素为可编辑项
    $('[data-field="admin_idss"] .th-inner ').editable({
      type: 'text',
      title: '测试金额',
      success: function(response, newValue) {
          $("#chuancan").text(newValue);
      }
    });
    

关联模型(视频整理)

一对一

文档地址:https://doc.fastadmin.net/doc/controller.html#toc-4

hasOne('关联模型名','本表外键名','本表主键名',['模型别名定义'],'join类型');
belongsTo('关联模型名','本表外键名','关联表主键名',['模型别名定义'],'join类型');

样例:4个表 fa_stuclass fa_student fa_studentmoney fa_stuscore
主表为:fa_student
实现:操作学生主表,增删改查 学费表和成绩表,查询班级表

关联查询

控制器里

protected $relationSearch = true;

/**
 * 快速搜索时执行查找的字段
 */
protected $searchFields = 'id,admin.username';

关联写入(新增)

 $this->model->stuscore()->save(["score"=>111,"student_id"=>$this->model->id]);
 $this->model->studentmoney()->save(["money"=>12,"student_id"=>$this->model->id]);

关联修改

$row->stuscore->score="xx";
$row->studentmoney->money="xxx";
$row->together(['stuscore','studentmoney'])->save();

关联删除

$v->together(['stuscore','studentmoney'])->delete();
// 或者
$v->stuscore()->delete();
$v->studentmoney()->delete();

一对多

hasMany('关联模型名','本表外键名','本表主键名',['模型别名定义']);

关联统计

withCount()
也可在前端做统计

$this->model->withCount('score')->where($where)->select();

关联查询

貌似是tp5.0的问题,没找到更好的办法

 $total = $this->model
  ->alias('student')
          ->with(['stuclass','stuscore'])
          ->join('stuscore','stuscore.student_id = student.id','LEFT')
        
          ->where($where)
          ->group('student.id')
          ->count();
  

  $list = $this->model
  ->alias('student')
          ->with(['stuclass','stuscore'])
          ->join('stuscore','stuscore.student_id = student.id','LEFT')
          ->distinct(true)
          ->where($where)
          ->order($sort, $order)
          ->limit($offset, $limit)
          // ->fetchSql(true)                    
          ->select();

注意with和join同时用可能会有问题,可以改写为alisa join写法

$total = $this->model
        ->alias('student')
        ->join('stuscore','stuscore.student_id = student.id','left')
        ->join('studentmoney','studentmoney.student_id = student.id','left')
    
        ->field('student.*')
        ->where($where)
        ->group('student.id')
        ->count();

$list = $this->model
->alias('student')
->join('stuscore','stuscore.student_id = student.id','left')
->join('studentmoney','studentmoney.student_id = student.id','left')

->field('student.*')
->where($where)
->order($sort, $order)
->limit($offset, $limit)
->select();

foreach ($list as $row) {
    $row->stuscore = model('stuscore')->where('student_id',$row->id)->select();
    $row->studentmoney = model('studentmoney')->where('student_id',$row->id)->select();
}

关联新增

$this->model->stuscore()->saveAll([
  ["score"=>111,"student_id"=>$this->model->id],
  ["score"=>333,"student_id"=>$this->model->id]
]);

关联修改

感觉没有这个场景吧,together()貌似不支持,我也不会~
如果真要批量改的话,就这样吧:

$this->model->stuscore()->where('student_id',$ids)->update(["score"=>"888"]);

关联删除

foreach ($list as $k => $v) {
  $count += $v->delete();
  $v->stuscore()->delete();
}

评论