我需要根据存储在数据库中的值为行的背景着色。我正在使用TriRand jqgrid。我需要从POLineStatus获取值,如果它等于2,我需要将背景颜色设置为红色。我在css文件中设置了一个类,它将获得正确的background-color属性。不幸的是,我无法弄清楚如何在每列的基础上做到这一点。
$mycellattr = <<<CELLATTR
function (rowid, value, rawObject, colModel, arraydata){
if(value=="Rejected"){
return {class:'colorClass'};
}
}
CELLATTR;
$grid->setGridEvent('rowattr', $mycellattr);
gridViewSubGrid.php --------完整代码:
<?php
if(!isset($_SESSION)) {
session_start();
}
$user = $_SESSION['loggedInUser'];
require_once "model.php";
require_once "jq-configSub.php";
$conn = sqlsrv_connect($serverName1, $connectionInfo1);
// include the jqGrid Class
require_once "php/jqGrid.php";
// include the SQLSRV driver class
require_once "php/jqGridSqlsrv.php";
// include the autocomplete class
require_once "php/jqAutocomplete.php";
// include the datepicker class
require_once "php/jqCalendar.php";
if(isset ($_REQUEST["CustPOID"])){
$rowid = jqGridUtils::Strip($_REQUEST["CustPOID"]);
}
else{
$rowid = "";
}
$grid = new jqGridRender($conn);
$grid->SelectCommand = "SELECT [CustPOLineID]
,[Qty]
,[Price]
,[POLineComment]
,[Cost]
,[Origin]
,[POLineLoadDate]
,[ApptNum]
,[CreateDate]
,[VendRefNum]
,[HapcoPONum]
,[PriceAlt]
,[VendRefNumAlt]
,[MiscCharge]
,[PHDSCharge]
,[VBRCharge]
,[CostAlt]
,[VendAlt]
,[LineGM]
,[ModifyDate]
,[ModifiedBy]
,[CreatedBy]
,[VendPurchPONum]
,[CustPOID]
,cpl.VendId
,vn.VendDesc
,cpl.ProdID
,pd.ProdDesc
,cpl.POLineStatusID
,posl.POLineStatusDesc
,cpl.ChargeTypeID
,ct.ChargeTypeDesc
,cpl.AlertReasonID
,ar.AlertReasonCode
FROM [HapcoSales].[dbo].[CustPOLine] as cpl
LEFT OUTER JOIN Vendor as vn
on cpl.VendId = vn.VendId
LEFT OUTER JOIN Product as pd
on cpl.ProdID = pd.ProdID
LEFT OUTER JOIN POLineStatus as posl
on cpl.POLineStatusID = posl.POLineStatusID
LEFT OUTER JOIN ChargeType as ct
on cpl.ChargeTypeID = ct.ChargeTypeID
LEFT OUTER JOIN AlertReason as ar
on cpl.AlertReasonID = ar.AlertReasonID
WHERE CustPOID= ?";
// set the ouput format to json
$grid->dataType = 'json';
$grid->table ="CustPOLine";
//Set primary key
$grid->setPrimaryKeyId("CustPOLineID");
$grid->setDbDate('Y-m-d');
$grid->setDbTime('Y-m-d H:i:s');
$grid->setUserDate('m/d/Y');
$grid->setUserTime('m/d/Y');
$grid->datearray = array('POLineLoadDate');
$Model = array(
array("name"=>"CustPOLineID","editable"=>false,"hidden"=>true),
array("name"=>"Qty", "label"=>"Qty:", "width"=>"50","editoptions"=>array("tabindex"=>1)),
array("name"=>"ProdDesc", "label"=>"Item:","width"=>"350","editoptions"=>array("tabindex"=>2)),
array("name"=>"ProdID","label"=>"Product ID:","hidden"=>true, "editrules"=>array("edithidden"=>true),"editoptions"=>array("readonly"=>"readonly")),
array("name"=>"Price", "label"=>"Price:","width"=>"75","formatter"=>"currency","formatoptions"=>array("decimalPlaces"=>2,"thousandsSeparator"=>",","prefix"=>"$"),"editoptions"=>array("tabindex"=>3)),
array("name"=>"Cost", "label"=>"Cost:","width"=>"75","formatter"=>"currency","formatter"=>"currency","formatoptions"=>array("decimalPlaces"=>2,"thousandsSeparator"=>",","prefix"=>"$"),"editoptions"=>array("tabindex"=>4)),
array("name"=>"VendDesc", "label"=>"Vendor:","width"=>"300","editoptions"=>array("tabindex"=>5)),
array("name"=>"VendId", "label"=>"Vendor ID:","hidden"=>true, "editrules"=>array("edithidden"=>true),"editoptions"=>array("readonly"=>"readonly")),
array("name"=>"MiscCharge","formatter"=>"currency","formatter"=>"currency","formatoptions"=>array("decimalPlaces"=>2,"thousandsSeparator"=>",","prefix"=>"$"),"editoptions"=>array("tabindex"=>6)),
array("name"=>"PHDSCharge","formatter"=>"currency","formatter"=>"currency","formatoptions"=>array("decimalPlaces"=>2,"thousandsSeparator"=>",","prefix"=>"$"),"editoptions"=>array("tabindex"=>7)),
array("name"=>"VBRCharge","formatter"=>"currency","formatter"=>"currency","formatoptions"=>array("decimalPlaces"=>2,"thousandsSeparator"=>",","prefix"=>"$"),"editoptions"=>array("tabindex"=>8)),
array("name"=>"POLineLoadDate","label"=>"Load Date:","width"=>"125","editable"=>true,"editoptions"=>array("tabindex"=>9)),
array("name"=>"POLineComment","search"=>false,"width"=>"300","formatter"=>"textarea","label"=>"Comments","edittype"=>"textarea","editoptions"=>array("tabindex"=>10)),
array("name"=>"PriceAlt","label"=>"Alt Price:", "formatter"=>"currency","formatter"=>"currency","formatoptions"=>array("decimalPlaces"=>2,"thousandsSeparator"=>",","prefix"=>"$"),"editoptions"=>array("tabindex"=>11)),
array("name"=>"CostAlt", "label"=>"Alt Cost:","formatter"=>"currency","formatter"=>"currency","formatoptions"=>array("decimalPlaces"=>2,"thousandsSeparator"=>",","prefix"=>"$"),"editoptions"=>array("tabindex"=>12)),
array("name"=>"VendAlt", "label"=>"Alt Vendor:","editable"=>true,"hidden"=>true, "editrules"=>array("edithidden"=>true),"editoptions"=>array("tabindex"=>13)),
array("name"=>"ChargeTypeID", "label"=>"Charge Type","editable"=>true,"hidden"=>true, "editrules"=>array("edithidden"=>true),"editoptions"=>array("tabindex"=>14)),
array("name"=>"ModifyDate", "label"=>"Modified:","formoptions"=>array("rowpos"=>2,"colpos"=>4),"editable"=>true,"hidden"=>true, "editrules"=>array("edithidden"=>true),"editoptions"=>array("readonly"=>"readonly")),
array("name"=>"ModifiedBy", "label"=>"Modified By:","formoptions"=>array("rowpos"=>3,"colpos"=>4),"editable"=>true,"hidden"=>true, "editrules"=>array("edithidden"=>true),"editoptions"=>array("readonly"=>"readonly")),
array("name"=>"CreatedBy","label"=>"Created By:","formoptions"=>array("rowpos"=>4,"colpos"=>4),"editable"=>true,"hidden"=>true, "editrules"=>array("edithidden"=>true),"editoptions"=>array("readonly"=>"readonly")),
array("name"=>"HapcoPONum","formoptions"=>array("rowpos"=>2,"colpos"=>3),"editable"=>true,"hidden"=>true, "editrules"=>array("edithidden"=>true),"editoptions"=>array("tabindex"=>21)),
array("name"=>"Origin","formoptions"=>array("rowpos"=>3,"colpos"=>3),"editoptions"=>array("tabindex"=>22)),
array("name"=>"VendRefNumAlt","formoptions"=>array("rowpos"=>4,"colpos"=>3),"editable"=>true,"hidden"=>true, "editrules"=>array("edithidden"=>true),"editoptions"=>array("tabindex"=>23)),
array("name"=>"ApptNum","editable"=>true,"hidden"=>true, "editrules"=>array("edithidden"=>true),"editoptions"=>array("tabindex"=>15)),
array("name"=>"VendRefNum","editable"=>true,"hidden"=>true, "editrules"=>array("edithidden"=>true) ,"editoptions"=>array("tabindex"=>16)),
array("name"=>"VendPurchPONum","editable"=>true,"hidden"=>true, "editrules"=>array("edithidden"=>true),"editoptions"=>array("tabindex"=>17)),
array("name"=>"AlertReasonCode","editoptions"=>array("tabindex"=>18)),
array("name"=>"AlertReasonID","editable"=>true,"hidden"=>true, "editrules"=>array("edithidden"=>true),"editoptions"=>array("tabindex"=>19)),
array("name"=>"POLineStatusDesc"),
array("name"=>"POLineStatusID"),
array("name"=>"LineGM", "label"=>"Gross Margin:", "editable"=>true,"editoptions"=>array("readonly"=>"readonly"),"formatter"=>"currency","formatter"=>"currency","formatoptions"=>array("decimalPlaces"=>2,"thousandsSeparator"=>",","prefix"=>"$")),
array("name"=>"CustPOID","hidden"=>true)
);
$grid->setColModel($Model, array(&$rowid));
$grid->setUrl('gridViewSubGrid.php');
$grid->setGridOptions(array(
"hoverrows"=>true,
"resizable"=>true,
"scroll"=>1,//unlimited scroll
"rowNum"=>100,//100 entries on load
"caption"=>"Detailed view of each sale.",
"rowList"=>array(10,20,30),
"sortname"=>"CustPOLineID",//sort by
"sortorder"=>"desc",
"autowidth"=>true,//determine the users screen resolution, generate grid off of that
"height"=>"175",
"footerrow"=>true,
"userDataOnFooter"=>true
));
$grid->setDatePicker("POLineLoadDate",null,true,false);
$grid->setColProperty("POLineLoadDate",array(
"formatter"=>"date",
"formatoptions"=>array("srcformat"=>"Y-m-d H:i:s","newformat"=>"m/d/Y")
)
);
$grid->setColProperty("CustPOID", array("hidden"=>true));
$grid->setColProperty("CustPOLineID", array("hidden"=>true));
$grid->setAutocomplete("VendDesc","#VendId","SELECT DISTINCT VendDesc, VendDesc as VendDesc2, VendId FROM Vendor WHERE VendDesc LIKE ? ORDER BY VendDesc",null,true,false);
$grid->setAutocomplete("ProdDesc","#ProdID","SELECT ProdDesc, ProdDesc AS ProdDesc2, ProdID FROM Product WHERE ProdDesc LIKE ? ORDER BY ProdDesc",null,true,false);
$grid->setSelect("POLineStatusID", "SELECT DISTINCT POLineStatusID, POLineStatusDesc, POLineStatusID AS POLineStatusDesc FROM POLineStatus ORDER BY 1",false,true,true,array(""=>"All"));
$grid->setSelect("ChargeTypeID", "SELECT DISTINCT ChargeTypeID, ChargeTypeDesc, ChargeTypeID AS ChargeTypeDesc FROM ChargeType ORDER BY 1",false,true,true,array(""=>"All"));
//We select the key:value for the select drop downs
$grid->setSelect("AlertReasonID", "SELECT DISTINCT AlertReasonID,AlertReasonDesc, AlertReasonID AS AlertReasonDesc FROM AlertReason ORDER BY 1",false,true,true,array(""=>"All"));
// Enable toolbar searching
$grid->navigator = true;
$grid->setNavOptions('navigator', array("excel"=>true,"add"=>true,"edit"=>true,"del"=>true,"view"=>true));
$grid->setNavOptions('edit', array("closeAfterEdit"=>true,"height"=>"auto","dataheight"=>"auto", "width"=>"auto"));
$grid->setNavOptions('add', array("height"=>"auto","dataheight"=>"auto","width"=>"auto"));
// disable the CRUD buttons when we initialy load the grid
$initgrid = <<<INIT
jQuery("#add_detail").addClass("ui-state-disabled");
jQuery("#edit_detail").addClass("ui-state-disabled");
jQuery("#del_detail").addClass("ui-state-disabled");
INIT;
// on beforeshow form when add we get the customer id and set it for posting
$beforeshow = <<<BEFORE
function(formid)
{
var srow = jQuery("#grid").jqGrid('getGridParam','selrow');
if(srow) {
var gridrow = jQuery("#grid").jqGrid('getRowData',srow);
$("#CustPOID",formid).val(gridrow.CustPOID);
}
}
BEFORE;
//This function brings up the edit dialog on a double click
$dbClk = <<<ONDBLCLK
function dbClk(rowid, selected)
{
jQuery(this).jqGrid('editGridRow', rowid, {"height" : "100%","width" : "100%", "dataheight" : "auto"});
}
ONDBLCLK;
$mycellattr = <<<CELLATTR
function (e, rd) {
if (rd.POLineStatusID === "2") { // test the value of POLineStatus column
return {"class": "colorClass"};
}
}
CELLATTR;
$grid->setGridEvent('rowattr', $mycellattr);
//$grid->setColProperty('POLineStatusID', array("cellattr"=>"js:".$mycellattr));
//$grid->customFunc = "gmCalc";
$grid->setGridEvent('ondblClickRow', $dbClk);
$grid->setNavEvent('add', 'beforeShowForm', $beforeshow);
$cid = jqGridUtils::GetParam('CustPOLineID'); //we get the CustPOLineID from the SQL query
//$grid->setAfterCrudAction('add', "UPDATE CustPOLine SET LineGM = 3240 WHERE CustPOLineID=?",array($cid));
$dateNow=dateTimeNow(); //goes out to model.php and calls the dateTimeNow function - and returns current value
$grid->setAfterCrudAction('edit', "UPDATE CustPOLine SET ModifiedBy = '".$user."' WHERE CustPOLineID=?",array($cid));
$grid->setAfterCrudAction('edit', "UPDATE CustPOLine SET ModifyDate = '".$dateNow."' WHERE CustPOLineID=?",array($cid));
$summaryrows = array("LineGM"=>array("LineGM"=>"SUM"));
$grid->renderGrid("#detail","#pgdetail", true, $summaryrows, array(&$rowid), true,true);
$conn = null;
function gmCalc($gdata, $conn)
{
// gdata->rows contain the grid data rows
foreach($gdata->rows as $row )
{
//$total = ((($Price * $Qty)-($Cost * $Qty))-(($MiscCharge+$PHDSCharge+$VBRCharge)*$Qty));
$row->LineGM = ((($row->Price*$row->Qty)-($row->Cost*$row->Qty))-(($row->MiscCharge+$row->PHDSCharge+$row->VBRCharge)*$row->Qty));
}
// return the modified
return $gdata;
}
?>
------的index.php
<?php
require_once ('model.php');
loginCheck();
require_once ('header.php');
?>
<div id="main-container">
<div id="mainContainer" class="wrapper clearfix">
<div>
<?php include "gridView.php";?>
</div>
<div>
<?php include "gridViewSubGrid.php"; ?>
</div>
<h2>Gross Margin:</h2>
<div>From: <input type="text" id="from" >
To: <input type="text" id="to" onchange="setFrom()"></div>
<div id="myDiv"></div>
</div>
<div id="griddef"></div>
</div> <!-- #main -->
</div> <br><!-- #main-container -->
<?php require_once ('footer.php'); ?>
------- Olegs修复:
$mycellattr = <<<CELLATTR
function (rd,cur,id){
if(rd.POLineStatusID=="2"){
return {"class":"colorClass"};
}
}
CELLATTR;
$grid->setGridEvent('rowattr', $mycellattr);
答案 0 :(得分:1)
我不是PHP开发人员,但代码中明确错误的是rowattr
的参数。它具有树参数rd
,cur
和id
。在大多数情况下,只需要第一个参数。在the answer中查找代码示例。
或者,您可以使用jqGridRowAttr
jQuery事件。有关代码示例,请参阅the answer。在你的情况下它将是
$("#list").bind("jqGridRowAttr", function (e, rd) {
if (rd.POLineStatus === "Rejected") { // test the value of POLineStatus column
return {"class": "colorClass"};
}
});
使用{"class": "colorClass"}
代替{class: "colorClass"}
与旧版Internet Explorer兼容非常重要。在之前设置bind
非常重要。
The demo演示了“jqGridRowAttr”的用法。