Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

The creation of multi-level classification list after the blog project in laravel and the classification and sorting of Ajax asynchronous modification

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

Editor to share with you about the laravel blog project after the Taiwan multi-level classification list creation and Ajax asynchronous modification classification sorting, I believe that most people do not know much, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!

First, the background article classification page multi-level classification list

1) add multi-level classification note that cate_pid is the same as cate_id to prove which category it is under.

2) add a method to deal with 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 below to see if the above parameters are passed in

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

5) first, filter out the cate_pid that is 0, and then cycle through the equality of cate_pid and cate_id. In order to make the method stronger, we want to pass parameters.

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) {/ / A newly defined field defines the subordinates of the category $data [$k] ["_ cata_name"] ='-->'. $data [$k] ["cate_name"] $arr [] = $data [$v];}}

Better optimized code:

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

Public static function tree () {/ / uses the static method $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 = > $if ($value- > $file_pid==$v- > $file_id) {/ / A newly defined field defines the subordinates of the classification $data [$k] ["_ cata_name"] ='- >'. $data [$k] ["cate_name"] $arr [] = $data [$v];}

Modifications in the controller:

Public function index () {/ / $data= CategroyModel::Tree (); not static method cannot call static method $data= (new CategroyModel)-> Tree (); call is not static method return view ('home/categroy/index')-> with (' data',$data);}

2. Background article classification page Ajax asynchronously modifies classification and sorting

1) check whether jquery is introduced into the main template

2) write JS

$(function () {})

Write the basic format of jquery

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

3) use JS to send async

Because I send an event when I want to manipulate this input form

Cate_order}} "> / / add an event onchange and request the method onchangeOrder method

4) (2) is tested

Function onangeOrder () {alert ();}

5) next send the asynchronous request

Function onchangeOrder () {$.post ("", {}); / / the first url, the second argument, and the third is the callback function function ($data) {} callback function in which we use $data to receive}

6) assign address

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

7) assign routes

Route::post ('admin/changeorder','CategoryController@changeorder')

8) create a new controller CategoryController.php

Public function changeorder () {echo 123;}

9) pass the token value to the background

Function onchangeOrder () {$.post ("{{url ('admin/cate/changeorder')}}", {' _ token':' {{csrf_token ()}}'}, function ($post) {});}

10) pass a few more parameters. The first parameter is the _ token parameter, the second parameter is the information of which parameter to modify, and the third parameter is how much to change the classification information.

Modify the information of which item

Cate_id}}) "value=" {{$v-> cate_order} ">

How much is the classification information changed to?

Cate_id}}) "value=" {{$v-> cate_order}} "> / / how many values are currently entered?

11) the following accept parameters, the first is the object, and the second is cate_id

Function onchangeOrder (obj,cate_id) {$.post ("{url ('home/cate/changeorder')}}", {' _ token':' {{csrf_token ()}'}, function ($data) {});}

12) use obj to read the value we currently enter.

So let's define a variable, cate_order equals $(), 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) {});}

And cate_id is the parameter we passed, so we don't have to deal with it.

13) here are a few parameters to deal with

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

14) how to accept it after passing it to controller

Public function changeorder () {/ / accept the value $input= Input::all (); print_r ($input);} from the foreground using the input method

15) then take the database corresponding cate_id data, change the value of the corresponding cate_order, and then follow the new data

Public function changeorder () {/ / accept the value $input= Input::all (); $cate=CategroyModel::find ($input ['cate_id']) from the foreground by input method; / / then change the order parameter $cate- > cate_order=$input [' cate_order']; / / update the database $res = $cate- > update ();}

16) when updating, give the receptionist a hint that we send a $data [] to the receptionist.

Public function changeorder () {/ / accept the value from the foreground with the input method $input= Input::all (); $cate=CategroyModel::find ($input ['cate_id']); / / then change the order parameter $cate- > cate_order=$input [' cate_order']; / / perform the update operation $res=$cate- > update (); if ($res) {$data= ['status'= > 0,' msg'= > 'updated!' ,];} else {$data= ['status'= > 1,' msg'= > 'update failed!' ,];} return $data; / / Don't forget to return a value}

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);}) } the above is all the contents of the article "creation of multi-level classification list after blog project in laravel and asynchronous modification of classification and sorting by Ajax". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report