thinkphp教程:内置标签volist 控制换行

2019-03-3020:39:58后端程序开发Comments2,204 views字数 1487阅读模式

volist标签通常用于查询数据集(select方法)的结果输出,通常模型的select方法返回的结果是一个二维数组,可以直接使用volist标签进行输出。在控制器中首先对模版赋值$User = M('User');文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/10862.html

$list = $User->limit(10)->select();文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/10862.html

$this->assign('list',$list);文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/10862.html

在模版定义如下,循环输出用户的编号和姓名:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/10862.html

<volistname="list"id="vo">文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/10862.html

{$}:{$}<br/>文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/10862.html

</volist>文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/10862.html

Volist标签的name属性表示模板赋值的变量名称,因此不可随意在模板文件中改变。id表示当前的循环变量,可以随意指定,但确保不要和name属性冲突,例如:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/10862.html

<volistname="list"id="data">文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/10862.html

{$}:{$}<br/>文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/10862.html

</volist>文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/10862.html

支持输出查询结果中的部分数据,例如输出其中的第5~15条记录文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/10862.html

<volistname="list"id="vo"offset="5"length='10'>文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/10862.html

{$}文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/10862.html

</volist>文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/10862.html

输出偶数记录文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/10862.html

<volistname="list"id="vo"mod="2">文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/10862.html

<eqname="mod"value="1">{$}</eq>文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/10862.html

</volist>文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/10862.html

Mod属性还用于控制一定记录的换行,例如:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/10862.html

<volistname="list"id="vo"mod="5">文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/10862.html

{$}文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/10862.html

<eqname="mod"value="4"><br/></eq>文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/10862.html

</volist>文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/10862.html

为空的时候输出提示:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/10862.html

<volistname="list"id="vo"empty="暂时没有数据">文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/10862.html

{$}|{$}文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/10862.html

</volist>文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/10862.html

empty属性不支持直接传入html语法,但可以支持变量输出,例如:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/10862.html

$this->assign('empty','<span class="empty">没有数据</span>');文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/10862.html

$this->assign('list',$list);文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/10862.html

然后在模板中使用:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/10862.html

<volistname="list"id="vo"empty="$empty">文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/10862.html

{$}|{$}文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/10862.html

</volist>文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/10862.html

输出循环变量文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/10862.html

<volistname="list"id="vo"key="k">文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/10862.html

{$k}.{$}文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/10862.html

</volist>文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/10862.html

如果没有指定key属性的话,默认使用循环变量i,例如:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/10862.html

<volistname="list"id="vo">文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/10862.html

{$i}.{$}文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/10862.html

</volist>文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/10862.html

如果要输出数组的索引,可以直接使用key变量,和循环变量不同的是,这个key是由数据本身决定,而不是循环控制的,例如:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/10862.html

<volistname="list"id="vo">文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/10862.html

{$key}.{$}文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/10862.html

</volist>文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/10862.html

因为我的数据key是时间戳所以只能用mod文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/10862.html

mod 是从0 开始计数的文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/10862.html

我的代码文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/10862.html

<tr><volist name="more" id="vo4" mod="5"><td ><a class="ercode modalA" href="#" data-data="__ROOT__/<{$}>" data-toggle="modal" data-target="#myModal" data-href="<{:U('Index/modal',array('id'=>$file['id']))}>"><i><font><img src="__PUBLIC__/img/ico/<{$}>.ico" style="width:2em;"></font></i><{$vo4.name}><{$mod}></a></td>
<eq name="mod" value="4" ></tr></eq>
文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/10862.html
  • 本站内容整理自互联网,仅提供信息存储空间服务,以方便学习之用。如对文章、图片、字体等版权有疑问,请在下方留言,管理员看到后,将第一时间进行处理。
  • 转载请务必保留本文链接:https://www.cainiaoxueyuan.com/bc/10862.html

Comment

匿名网友 填写信息

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定