laravel-admin select框默认选中的方法

吾爱主题 阅读:139 2021-08-25 15:52:00 评论:0

laravel-admin后台框架的select选择框的默认选中,结合select2的js插件;

文档http://laravel-admin.org/docs/#/zh/model-form-fields?id=select%E9%80%89%E6%8B%A9%E6%A1%86;

文档里面没有看到这个默认选中的属性,也可能是我没有找到;查阅了select2的文档看到了selected属性,通过曲线的方式实现了这个功能;

?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 protected function form() {    return Admin::form(Member:: class , function (Form $form ) {        // $form->display('id', 'ID');      $form ->text( 'mac' , '终端mac' );      $form ->text( 'phone' , '手机号' );      $form ->text( 'password' , '密码' );      //判断是不是edit      $id = Request::route( 'member' );      $place_id = '' ;      $rule_id = '' ;      if ( $id )      {        $model = $form ->model()->find( $id );        $place_id = $model ->place_id;        $rule_id = $model ->rule_id;      }      $form ->select( 'place_id' , '所属场所' )->options( "/api/getPlace/$place_id" );      $form ->select( 'rule_id' , '规则' )->options( "/api/getRule/$rule_id" );    }); }   //api的实现 public function getPlace(Request $request ) {    $name = $request ->get( 'p' );    $id = $request ->id;    $where = [];    if ( $name )    {      $where = [ 'name' , 'like' , "%$name%" ];    }    $places = Place::where( $where )->get();    $result = [];    foreach ( $places as $key => $value )    {      $result [ $key ][ 'id' ] = $value ->Id;      $result [ $key ][ 'text' ] = $value ->name;      if ( $value ->Id == $id )      {        $result [ $key ][ 'selected' ] = true;      }    }      if (! $id )      {        array_unshift ( $result ,[ 'id' => ' ' , 'text' => '请选择场所' , 'selected' =>true]);      } else {        array_unshift ( $result ,[ 'id' => ' ' , 'text' => '请选择场所' ]);      }    return response()->json( $result ); }

注意;

返回的时候一定要有response()->json()来返回;否则的话会返回的类型是 text/html类型

以上这篇laravel-admin select框默认选中的方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持服务器之家。

原文链接:http://www.xiegaosheng.com/post/view?id=98

可以去百度分享获取分享代码输入这里。
声明

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

【腾讯云】云服务器产品特惠热卖中
搜索
标签列表
    关注我们

    了解等多精彩内容