PHP+jqGrid实现自定义列文本:基于MariaDB表数据生成动态消息
解决方案
核心修改思路
要实现仅显示自定义消息列,需手动控制列模型,在服务端为每条数据生成对应消息,再让jqGrid只展示该列。
具体修改步骤
替换自动列模型为手动定义
删除自动生成列的$grid->setColModel();,手动指定列结构:只保留用于展示消息的description列,其余字段设置为隐藏(但需在SQL查询中保留,用于生成消息)。更新自定义消息生成函数
调整函数参数,传入生成消息所需的所有字段(如unit、action、weight_transferred等),根据action值编写对应的消息逻辑。在数据返回前注入自定义消息
通过jqGrid的beforeProcessing回调,在数据返回前端前为每一行添加description字段,赋值为生成的自定义消息。
修改后的完整代码片段
<?php // (...) lines that call the database $userUnit = $_SESSION['deployment']; // SQL Query - 保留所有需要的字段 $grid->SelectCommand = "SELECT ActionId, id, date, unit, weight_transferred, w_proc, wi_material, wi_product, buyer, idconverted, action FROM loginglobal.tblactionlog WHERE unit = '$userUnit'"; $grid->table = 'tblactionlog'; $grid->dataType = 'json'; $grid->setUrl('adv-logger_grid.php'); // 手动设置列模型,不再自动生成 $grid->setColModel(array( array( "name" => "description", "label" => "Logging Message", "width" => 600, "align" => "left", "sortable" => false ), array("name" => "ActionId", "hidden" => true), array("name" => "date", "hidden" => true), array("name" => "id", "hidden" => true), array("name" => "unit", "hidden" => true), array("name" => "weight_transferred", "hidden" => true), array("name" => "w_proc", "hidden" => true), array("name" => "wi_material", "hidden" => true), array("name" => "wi_product", "hidden" => true), array("name" => "buyer", "hidden" => true), array("name" => "idconverted", "hidden" => true), array("name" => "action", "hidden" => true) )); // 网格基础配置保持不变 $grid->setGridOptions(array( "caption" => "Logging History", "hoverrows" => true, "autowidth" => true, "shrinkToFit" => true, "rowNum" => 100, "sortorder" => "desc", "altRows" => true, "multiselect" => false, "rowList" => array(100,200,500,1000,5000,10000), "userDataOnFooter" => true, "gridview" => true )); // 自定义消息生成函数,可扩展action 2-9的逻辑 function generateCustomMessage($rowData) { $action = $rowData['action']; $unit = $rowData['unit']; $weight = $rowData['weight_transferred']; $buyer = $rowData['buyer']; $material = $rowData['wi_material']; $date = $rowData['date']; $id = $rowData['id']; switch ($action) { case 0: return "$unit gave {$weight}kg of {$material} to {$buyer}"; case 1: return "At {$date}, added entry ID {$id}: {$unit} received {$weight}kg of {$material}"; // 继续添加action 2到9的自定义消息逻辑 default: return "Unknown action (ID: {$action}) for entry {$id}"; } } // 处理数据,为每行添加自定义消息 $grid->setDbGridEvent("beforeProcessing", function($data) { foreach ($data['rows'] as &$row) { $row['cell']['description'] = generateCustomMessage($row['cell']); } return $data; }); // 其他配置保持不变 $PDF_HEADER_LOGO_WIDTH = 0; $PDF_HEADER_TITLE = $dataCompany['CompanyName']; $grid->setGridEvent('onSortCol', $sorcol); $grid->setGridEvent('loadComplete', $loadcomplete); $grid->renderGrid('#grid', '#pager', true, null, null, true, true); $conn = null; mysql_close($glogin_mysql); mysql_close($mysql); ?>
关键说明
- 手动定义
ColModel确保只有description列可见,其余列仅作为数据来源隐藏。 beforeProcessing回调会在jqGrid处理返回数据前触发,为每一行生成并注入自定义消息。generateCustomMessage函数接收整行数据,可灵活调用所有字段生成对应action的消息,你可以根据需求扩展case 2到case 9的逻辑。
内容的提问来源于stack exchange,提问作者Scarlet D.
相关产品推荐
相关产品推荐

