Home> PHP Framework> Laravel> body text

[laravel] blog project practical notes - background multi-level classification list creation and Ajax asynchronous modification of classification sorting

演明
Release: 2021-09-09 16:54:02
Original
1049 people have browsed it

I always want to do a small project by myself, but I don’t know how to start. I always do a certain part of the project when I go to work, and I don’t do a project as a whole. The following is a summary of the videos I watched on the PHP Chinese website Notes for your reference. This is a practical blog project done in a Windows environment. The following articles are part of the project. I shared them section by section. If you want to see more, follow me and I will continue to update.

1. Multi-level classification list on the background article classification page

1) Add multi-level classification. Note that cate_pid and cate_id are the same to prove which category it is in

2) Add a method to process the classification

public function getTree(){ }
Copy after login

3) Adjust the following method in the above method

$data = $this->getTree($categroy); public function index(){ $categroy = CategroyModel::all(); $data = $this->getTree($categroy,'cate_name','cate_pid','cate_id'); return view('home/categroy/index')->with('data',$data); }
Copy after login

4) Print it below to see if the above parameters are passed in

public function getTree($data){ dd($data); }
Copy after login

5) First filter out those whose cate_pid is 0, and then cycle again to filter out those whose cate_pid and cate_id are equal. In order to make the method more powerful, we thought of using parameter passing,

public function getTree($data,'$file_pid=‘pid’,$file_id=‘id’,$pid=0){ $arr = array(); foreach($data as $key =>$value){ if($value->$file_pid==$pid){ $data[$key]["_cate_name"]=$data[$key]["cate_name"]; $arr[] = $data[$key]; foreach($data as $k=>$v{ if($value->$file_pid==$v->$file_id){ //新定义个字段把分类的下级定义出来 $data[$k]["_cata_name"]='-->'.$data[$k]["cate_name"] $arr[]=$data[$v]; } } } }
Copy after login

Better Optimized code:

Put the process of sorting data in the controller into the model

public static function tree(){ //用的是静态方法 $categroy = CategroyModel::all(); return (new CategroyModel)->getTree($categroy,'cate_name','cate_pid','cate_id'); } public function tree(){ $categroy = $this->all(); return $this->getTree($categroy,'cate_name','cate_pid','cate_id'); } public function getTree($data,'$file_pid=‘pid’,$file_id=‘’,$pid=0){ $arr = array(); foreach($data as $key =>$value){ if($value->$file_pid==$pid){ $data[$key]["_cate_name"]=$data[$key]["cate_name"]; $arr[] = $data[$key]; foreach($data as $k=>$v{ if($value->$file_pid==$v->$file_id){ //新定义个字段把分类的下级定义出来 $data[$k]["_cata_name"]='-->'.$data[$k]["cate_name"] $arr[]=$data[$v]; } } } }
Copy after login

Modifications in the controller:

public function index(){ // $data= CategroyModel::Tree(); 不是静态方法不能用静态方法调用 调用静态方法 $data =(new CategroyModel)->Tree(); 调用不是静态方法 return view('home/categroy/index')->with('data',$data); }
Copy after login

2. Ajax asynchronous on the background article classification page Modify the classification sorting

1) Check whether jquery is introduced in the main template

2) Write JS

$(function(){});
Copy after login

The basic format for writing jquery

$(function(){ alert(); });
Copy after login

3) I need to use JS to send asynchronous

because I want to send events when operating this input form,

 //加一个事件onchange,请求方法onchangeOrder方法
Copy after login

4) (2) is for testing,

function onangeOrder(){ alert(); }
Copy after login

5) will be sent next Asynchronous request

function onchangeOrder(){ $.post("",{});//第一个url,第二个参数,第三个是回调函数function($data){}回调函数里我们用$data接收 }
Copy after login

6) Assign address

function onchangeOrder(){ $.post("{{url('admin/cate/changeorder')}}",{},function($data){}); }
Copy after login

7) Assign route

Route::post('admin/changeorder','CategoryController@changeorder');
Copy after login

8) Create a new controller CategoryController.php

public function changeorder(){ echo 123; }
Copy after login

9) Put The token value is passed to the background

function onchangeOrder(){ $.post("{{url('admin/cate/changeorder')}}",{'_token':'{{csrf_token()}}'},function($data){}); }
Copy after login

10) Pass a few more parameters. The first parameter is the _token parameter, the second parameter is the information about which parameter to modify, and the third parameter is the classification information. How much to change

Which piece of information to modify

Copy after login

How much to change this classification information to

 //能找到当前输入的值到底多少
Copy after login

11) The following accept parameters, the first one is the object, the second one is It is cate_id

function onchangeOrder(obj,cate_id){ $.post("{{url('home/cate/changeorder')}}",{'_token':'{{csrf_token()}}'},function($data){}); }
Copy after login

12) Read the value we are currently inputting through obj

Then we define a variable, cate_order is equal to $() and then pass obj in, and then its. val()

function onchangeOrder(obj,cate_id){ var cate_order=$(obj).val(); $.post("{{url('home/cate/changeorder')}}",{'_token':'{{csrf_token()}}'},function($data){}); }
Copy after login

And cate_id is the parameter we passed over, so there is no need to process it

13) Here are a few parameters to process

function onchangeOrder(obj,cate_id){ var cate_order=$(obj).val(); $.post("{{url('home/cate/changeorder')}}",{'_token':'{{csrf_token()}}','cate_id':cate_id,'cate_order':cate_order},function($data){}); }
Copy after login

14) Pass it to the controller How to accept it later

public function changeorder(){ //用input方法接受前台传过来的值 $input= Input::all(); print_r( $input); }
Copy after login

15) Then get the corresponding cate_id data from the database, change the value corresponding to cate_order, and then update this data

public function changeorder(){ //用input方法接受前台传过来的值 $input= Input::all(); $cate=CategroyModel::find($input['cate_id']); //然后改变order参数 $cate->cate_order=$input['cate_order']; //更新数据库 $res = $cate->update(); }
Copy after login

16) When updating, give the front desk a prompt, this is We pass a $data[] to the front desk

public function changeorder(){ //用input方法接受前台传过来的值 $input= Input::all(); $cate=CategroyModel::find($input['cate_id']); //然后改变order参数 $cate->cate_order=$input['cate_order']; //执行更新操作 $res=$cate->update(); if($res){ $data=[ 'status'=> 0, 'msg'=>'更新成功!', ]; }else{ $data=[ 'status'=> 1, 'msg'=>'更新失败!', ]; } return $data; //别忘了返回值 }
Copy after login

17) Get the value of the callback function

function onchangeOrder(obj,cate_id){ var cate_order=$(obj).val(); $.post( "{{url('home/cate/changeorder')}}", {'_token':'{{csrf_token()}}','cate_id':cate_id,'cate_order':cate_order}, function(data){ alert(data.msg); } ); }
Copy after login

Now the whole thing is completed, but the pop-up window is too ugly. The next section will share the pop-up window installation information with everyone. .

The above steps are my study notes. I wrote down the steps or key points to be operated. If you don’t understand anything, you can leave a message. Thank you for your support. I hope it can help Xiaobai. If you want to see more blog project information, follow me and I will continue to share in the next article.

Related recommendations: "laravel tutorial"

The above is the detailed content of [laravel] blog project practical notes - background multi-level classification list creation and Ajax asynchronous modification of classification sorting. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!