The sorting implementation code of infinite columns in thinkphp

不言
Release: 2023-04-03 20:36:01
Original
1717 people have browsed it

The content of this article is about the sorting implementation code of unlimited columns in thinkphp. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

We did not specify in the question whether it is the infinite sorting of tp5 or the infinite sorting of tp3, just to let the novices understand that the implementation of these functions has nothing to do with the framework you use, no matter you are tp5 It doesn't matter whether it is TP3, laravel or yii framework. What we emphasize is the idea and the method to solve the problem. During the demonstration, it doesn't matter because I am using TP3.

The sorting of unlimited columns is very simple. We have explained it many times. This time we will share with you the solution in the form of a blog post.

Above picture:

The sorting implementation code of infinite columns in thinkphp

The above picture is the infinite level classification we have implemented. We should pay attention to two fields, id and sort field. Currently, the sort field The values ​​are all 50, which is the default value. Then I will take a screenshot of the data table structure for everyone

The sorting implementation code of infinite columns in thinkphp

The sort in the above picture is used to achieve sorting pid is used to achieve infinite classification

The key to achieving infinite classification is us To write the sorting field, we get the entire code, but only one line is used:

[php]
<form method="post" action="">
<table class="table table-bordered table-hover">
<thead class="">
<tr>
<th width="6%" class="text-center">ID</th>
<th width="6%" class="text-center">pid</th>
<th width="8%" class="text-center">排序</th>
<th>栏目名称</th>
<th width="16%" class="text-center">操作</th>
</tr>
</thead>
<tbody>
<volist name="cateRes" id="cate">
<tr>
<td align="center">{$cate.id}</td>
<td align="center">{$cate.pid}</td>
<td align="center">
<input type="text" name="sort[{$cate.id}]" value="{$cate.sort}" /></td>
<td><?php echo str_repeat(‘-‘, $cate[‘level’]*8);?>{$cate.cate_name}</td>
<td align="center">
<a href="" class="btn btn-primary btn-sm shiny">
<i class="fa fa-edit"></i> 编辑
</a>
<a href="#" onClick="warning(‘确实要删除吗’, ”)" class="btn btn-danger btn-sm shiny">
<i class="fa fa-trash-o"></i> 删除
</a>
</td>
</tr>
</volist>
<tr>
<td colspan="4">
<button
 type="button" tooltip="排序" style="margin-left:225px; width:50px;" 
class="btn btn-sm btn-azure btn-addon">排序</button>
</td>
</tr>
</tbody>
</table>
</form>
[/php]
Copy after login

From the above code, we can see that the entire table is wrapped in a form, because we want to submit the sorting field. So a form is needed.

The core code for our implementation of infinite column sorting:

[php]
<input type="text" name="sort[{$cate.id}]" value="{$cate.sort}" />
[/php]
Copy after login

This is the sentence. In fact, we assembled a sort[] array. The key of each element of the entire array is the current column. The id and value are the sorted values ​​of the current column, so that once we submit the array, we can modify the sort according to the id

Full code:

[php]
public function lst(){
$cate=D(‘Cate’);
if(IS_POST){//排序
$data=I(‘sort’);
foreach ($data as $k => $v) {
$cate->where(array(‘id’=>$k))->save([‘sort’=>$v]);
}
return;
}
$cateRes=$cate->cateTree();//无限级分类树
$this->assign([
‘cateRes’=>$cateRes,
]);
$this->display(‘list’);
}
[/php]
Copy after login

Related recommendations:

How to use recursion to achieve infinite grading under the TP5 framework (code example)

What are the methods of intercepting strings in thinkphp5? (Two methods to achieve)

The above is the detailed content of The sorting implementation code of infinite columns in thinkphp. 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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template