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

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

演明
演明Original
2021-09-08 07:46:491110browse

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(){
}

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);
}

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

public function getTree($data){
dd($data);
}

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];
}
       }
      }
   }

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];
}
}
}
}

Modifications in the controller:

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

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(){});

The basic format for writing jquery

$(function(){
alert();
});

3) I need to use JS to send asynchronous

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

<input type="text" onchange="onchangeOrder()" value="{{$v->cate_order}}">  //加一个事件onchange,请求方法onchangeOrder方法

4) (2) is for testing,

function onangeOrder(){
alert();
}

5) will be sent next Asynchronous request

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

6) Assign address

function onchangeOrder(){
$.post("{{url(&#39;admin/cate/changeorder&#39;)}}",{},function($data){});
}

7) Assign route

Route::post(&#39;admin/changeorder&#39;,&#39;CategoryController@changeorder&#39;);

8) Create a new controller CategoryController.php

public function changeorder(){
      echo 123;
   }

9) Put The token value is passed to the background

function onchangeOrder(){
$.post("{{url(&#39;admin/cate/changeorder&#39;)}}",{&#39;_token&#39;:&#39;{{csrf_token()}}&#39;},function($data){});
}

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

<input type="text" onchange="onchangeOrder({{$v->cate_id}})" value="{{$v->cate_order}}">

How much to change this classification information to

<input type="text" onchange="onchangeOrder(this,{{$v->cate_id}})" value="{{$v->cate_order}}"> //能找到当前输入的值到底多少

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(&#39;home/cate/changeorder&#39;)}}",{&#39;_token&#39;:&#39;{{csrf_token()}}&#39;},function($data){});
}

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(&#39;home/cate/changeorder&#39;)}}",{&#39;_token&#39;:&#39;{{csrf_token()}}&#39;},function($data){});
}

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(&#39;home/cate/changeorder&#39;)}}",{&#39;_token&#39;:&#39;{{csrf_token()}}&#39;,&#39;cate_id&#39;:cate_id,&#39;cate_order&#39;:cate_order},function($data){});
}

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

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

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[&#39;cate_id&#39;]);
      //然后改变order参数
   $cate->cate_order=$input[&#39;cate_order&#39;];
    //更新数据库
    $res = $cate->update();
}

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[&#39;cate_id&#39;]);
//然后改变order参数
$cate->cate_order=$input[&#39;cate_order&#39;];
//执行更新操作
$res=$cate->update();
 if($res){
        $data=[
            &#39;status&#39;=> 0,
            &#39;msg&#39;=>&#39;更新成功!&#39;,
        ];
    }else{
        $data=[
            &#39;status&#39;=> 1,
            &#39;msg&#39;=>&#39;更新失败!&#39;,
        ];
    }
   return $data;   //别忘了返回值
}

17) Get the value of the callback function

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

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!

Statement:
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