PHPCMS V9 新增通用性强,不限列数、不限文本组个数的高级文本组(信息表格)字段类型

为什么说是在PHPCMS V9上新增了通用性强,不限列数、不限文本组个数的多文本字段类型?

由于网上目前搜到的PHPCMS V9 多文本字段,字段名称在设计字段的时候已经定义了,这样局限性就比较大。原因如下:

其一:一篇文章中想添加一个有两列的多文本字段,就需要再去重新设计;

其二:另一模型中的文章需要添加一个 三列的多文本字段,需要再重新设计一个字段;

其三:一篇 文章中需要多个文本组,会有各种冲突。

下面来看看新增的通用性强,不限列数、不限文本组个数的多文本字段(我这里给它命名为“信息表格”)前后台的效果:

字段添加:

字段修改(相关参数中表格列名必填,列名为表格每列的名称,一行代表一个列名)

文章内容添加:

文章内容编辑:

前台文章显示:

下面来看看新增的“信息表格”字段类型添加方法:

一、打开phpcms\modules\content\fields\fields.inc.php文件,增加字段类型:'tabletext'=>'信息表格',

二、在phpcms\modules\content\fields目录下,新建文件夹tabletext

三、在tabletext目录下,新建PHP文件config.inc.php,代码如下:

<?php
defined('IN_ADMIN') or exit('No permission resources.');

$field_type				= 'mediumtext'; //字段数据库类型	
$field_basic_table		= 0; //是否允许作为主表字段
$field_allow_index		= 0; //是否允许建立索引
$field_minlength		= 0; //字符长度默认最小值
$field_maxlength		= ''; //字符长度默认最大值
$field_allow_search		= 0; //作为搜索条件
$field_allow_fulltext	= 0; //作为全站搜索信息
$field_allow_isunique	= 0; //是否允许值唯一
?>

四、在tabletext目录下,新建PHP文件field_add_form.inc.php,代码如下:

<table cellpadding="2" cellspacing="1" width="98%">
	<tr>
    <td width="100">表格列名</td>
    <td><textarea name="setting[column]" cols="20" id="column" style="height:100px;width:200px;"><?php echo $setting['column'];?></textarea>表格每列的名称,一行代表一个列名</td>
  </tr>
</table>

五、在tabletext目录下,新建PHP文件field_edit_form.inc.php,代码如下:

<?php defined('IN_PHPCMS') or exit('No permission resources.');?>
<table cellpadding="2" cellspacing="1" width="98%">
	<tr>
    <td width="100">表格列名</td>
    <td><textarea name="setting[column]" cols="20" id="column" style="height:100px;width:200px;"><?php echo $setting['column'];?></textarea>表格每列的名称,一行代表一个列名</td>
  </tr>
</table>

六、在tabletext目录下,新建PHP文件form.inc.php,代码如下:

function tabletext($field, $value, $fieldinfo) {
extract(string2array($fieldinfo['setting']));
$columns = explode("\n",$this->fields[$field]['column']);
$list_str = '';
if($value) {
$value = string2array(html_entity_decode($value,ENT_QUOTES));
if(is_array($value)) {
foreach($value as $_k=>$_v) {
    $list_str .= "<tr>";
    for ($x=1; $x<=count($columns); $x++) {
        $list_str .="<td><input type='text' name='".$field."_".$x."[]' value='".$_v[$field."_".$x]."' class='input-text' style='width:100%; padding:0; height:22px;'></td>";
    }
    $list_str .= "<td><input type='button' class='button' value='删除' onclick='delThisAttr(this)'> <input type='button' class='button' value='↑上移' onclick='moveUp(this)'> <input type='button' class='button' value='↓下移' onclick='moveDown(this)'></td></tr>";
}
}
}

$string ='<script type=text/javascript>
function add'.$field.'(id){
    var html = "<tr>';
for($cols=1; $cols<=count($columns); $cols++){
    $string .='<td><input type=\'text\' name=\''.$field.'_'.$cols.'[]\' value=\'\' class=\'input-text\' style=\'width:100%; padding:0; height:22px;\'></td>';
}
$string .='<td><input type=\'button\' class=\'button\' value=\'删除\' onclick=\'delThisAttr(this)\'> <input type=\'button\' class=\'button\' value=\'↑上移\' onclick=\'moveUp(this)\'> <input type=\'button\' class=\'button\' value=\'↓下移\' onclick=\'moveDown(this)\'></td></tr>";
$("#"+id).before(html);
}
</script>';
$string .= '<input name="info['.$field.']" type="hidden" value="1">
<fieldset class="blue pad-10">
<legend>列表</legend><div class="table-list"><table width="100%" cellspacing="0"><thead><tr align="left"> ';

foreach($columns as $column){
    $string .="<th align='left' style='text-align:left; padding:0 0 0 12px; border-bottom: 1px solid #d5dfe8;'>".$column."</th>";
}
$string .="<th align='left' style='text-align:left; padding:0 0 0 12px; border-bottom: 1px solid #d5dfe8; width:150px;'>操作</th></tr></thead><tbody>";

$string .= $list_str;

$string .= "<tr id='".$field."'></tr></tbody>
        </table></div>
</fieldset>
<div class='bk10'></div>";
$string .= "<input type=\"button\" class=\"button\" value=\"添加一行\" onclick=\"add".$field."('".$field."')\">";
return $string;
}

七、在tabletext目录下,新建PHP文件input.inc.php,代码如下:

function tabletext($field, $value) {
    $setting = string2array($this->fields[$field]['setting']);
    $columns = explode("\n",$this->fields[$field]['column']);

    $array = array();
    if(!empty($_POST[$field.'_1'])) {
        foreach($_POST[$field.'_1'] as $key=>$val) {
            for ($x=1; $x<=count($columns); $x++) {
                $array[$key][$field.'_'.$x] = $_POST[$field.'_'.$x][$key];
            }
        }
    }
    $array = array2string($array);
    return $array;
}

八、在tabletext目录下,新建PHP文件output.inc.php,代码如下:

function tabletext($field, $value) {
    return string2array($value);
}

九、修改statics\js\content_addtop.js文件,添加上移、下移排序、删除本行功能函数,代码如下:

/*文本组字段添加上移、下移排序、删除本行功能 WY ADD AT 2018-11-29*/
function moveUp(obj){
	var current=$(obj).parent().parent();
	var prev=current.prev();
	if(prev)
	{
	current.insertBefore(prev);
	}
}
function moveDown(obj){
	var current=$(obj).parent().parent();
	var next=current.next();
	if(next)
	{
	current.insertAfter(next);
	}
}
function delThisAttr(self){
  if (!confirm("确认要删除么?")) {
    return false;
  }
  $(self).parent().parent().remove();
}

十、前台内容页模板,代码如下:

<?php
//表头信息从模型缓存中调取,当然你也可以写成固定的
$modelsinfo = getcache('model_field_'.$modelid, 'model');
$params_colname = explode("\n",$modelsinfo['params']['column']);
$proinfo_colname = explode("\n",$modelsinfo['proinfo']['column']);
?>

{if !empty($params)}
<table border="1">
    <tr>
        {loop $params_colname $cols}
        <th>{$cols}</th>
        {/loop}
    </tr>
    {loop $params $pv}
    <tr>
        {loop $pv $v}
        <td>{$v}</td>
        {/loop}
    </tr>
    {/loop}
</table>
{/if}

<br>

{if !empty($proinfo)}
<table border="1">
    <tr>
        {loop $proinfo_colname $cols}
        <th>{$cols}</th>
        {/loop}
    </tr>
    {loop $proinfo $pv}
    <tr>
        {loop $pv $v}
        <td>{$v}</td>
        {/loop}
    </tr>
    {/loop}
</table>
{/if}

  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 6
    评论
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

大海哪蓝

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值