FineUI 官方论坛
标题:
Grid列头名称动态显示方式
[打印本页]
作者:
游泳的鱼
时间:
2013-12-7 01:06
标题:
Grid列头名称动态显示方式
本帖最后由 游泳的鱼 于 2013-12-7 10:22 编辑
string script = String.Format("var colmodel = X('{0}').getColumnModel();",gridDetail.ClientID);;
string headerScript ="colmodel.setColumnHeader(colmodel.getIndexById('{0}'), '{1}');";
foreach (var detail in dtconfig)
{
#region 控制列显示
switch (detail.FieldName.ToLower())
{
case "cpid":
colCPName.Hidden = false;
colCPName.HeaderText = detail.ChinaName;
script += String.Format(headerScript, colCPName.ClientID, detail.ChinaName);
break;
}
}
#endregion
}
PageContext.RegisterStartupScript(script);
复制代码
在程序中有时对自定义的表列,我们需要根据用户自定义的此列显示名称进行显示。
比如列头为“
自定义1
“,显示名称用户定义为”
备注
“。
在FineUI的Grid中我们可以在后端代码通过
Hidden
属性设置列的显示和隐藏,但是不能通过
HeadText
属性修改显示名称。
只能通过脚本来进行控制,以上是实现的cs代码,供大家参考。
注意列字段定义了
ColumnID
,因为后端存储的都是列的字段名称,便于按照名称进行对应,
按照索引号对应对于代码的阅读性比较差,调整起来会比较麻烦。
重点是设置列头的脚本:
var colmodel = x(gridid).getColumnModel();
colmodel.setColumnHeader(colmodel.getIndexById(columnid),'中文名称');
string script = String.Format("var colmodel = X('{0}').getColumnModel();",gridDetail.ClientID);;
string headerScript ="colmodel.setColumnHeader(colmodel.getIndexById('{0}'), '{1}');";
在此方式前,受黑冰建议,在此谢谢他:)
他建议使用X(x-grid3-hd-{gridColumnClientId}).text('NewColumnName'),
{gridColumnClientId}为每列的客户端生成的ID值。
在使用中总是提示X(x-grid3-hd-{gridColumnClientId})为空,后来查看页面元素,在列头的元素如下结构:
<td class='{otherclass} x-grid3-hd-{gridColumnClientId}'>
<div class='{otherclass} x-grid3-hd-{gridColumnClientId}'>列名<img src='...'/><div>
<td>
作者:
erp8@live.cn
时间:
2013-12-7 11:46
不错,感谢分享,太需要了
作者:
GardeniaHong
时间:
2013-12-30 11:28
有例子更好
作者:
游泳的鱼
时间:
2013-12-30 21:29
页面代码:
<x:Grid runat="server" ID="gridDetail" BoxFlex="1" Title="申请项" OnRowCommand="gridDetail_OnRowCommand">
<Toolbars>
<x:Toolbar runat="server">
<Items>
<x:ToolbarFill runat="server"></x:ToolbarFill>
<x:Button runat="server" ID="btnNew" Text="新增项" Icon="New" EnablePostBack="true" OnClick="btnNew_OnClick"></x:Button>
</Items>
</x:Toolbar>
</Toolbars>
<Columns>
<x:BoundField ID="colCPName" runat="server" DataField="CPName" Hidden="True" HeaderText="费用项目"/>
<x:BoundField ID="colInvCCode" runat="server" DataField="InvCCode" Hidden="True" HeaderText="存货分类"/>
<x:BoundField ID="colInventoryCode" runat="server" DataField="InventoryCode" Hidden="True" HeaderText="存货代码"/>
<x:BoundField ID="colInventoryName" runat="server" DataField="InventoryName" Hidden="True" HeaderText="存货名称"/>
<x:BoundField ID="colComUnitName" runat="server" DataField="ComUnitName" Hidden="True" HeaderText="计量单位"/>
<x:BoundField ID="colInvStd" runat="server" DataField="InvStd" Hidden="True" HeaderText="规格"/>
<x:BoundField ID="colInvSPrice" runat="server" DataField="InvSPrice" Hidden="True" HeaderText="出厂价"/>
<x:BoundField ID="colDNumber" runat="server" DataField="DNumber" Hidden="True" HeaderText="数量"/>
<x:BoundField ID="colDPrice" runat="server" DataField="DPrice" Hidden="True" HeaderText="单价"/>
<x:BoundField ID="colDMoney" runat="server" DataField="DMoney" Hidden="True" HeaderText="金额"/>
<x:BoundField ID="colUsage" runat="server" DataField="Usage" Hidden="True" HeaderText="用途"/>
<x:BoundField ID="colDefault1" runat="server" DataField="Default1" Hidden="True" HeaderText="自定义1"/>
<x:BoundField ID="colDefault2" runat="server" DataField="Default2" Hidden="True" HeaderText="自定义2"/>
<x:BoundField ID="colDefault3" runat="server" DataField="Default3" Hidden="True" HeaderText="自定义3"/>
<x:BoundField ID="colDefault4" runat="server" DataField="Default4" Hidden="True" HeaderText="自定义4"/>
<x:BoundField ID="colDefault5" runat="server" DataField="Default5" Hidden="True" HeaderText="自定义5"/>
<x:BoundField ID="colDefault6" runat="server" DataField="Default6" Hidden="True" HeaderText="自定义6"/>
<x:BoundField ID="colDefault7" runat="server" DataField="Default7" Hidden="True" HeaderText="自定义7"/>
<x:BoundField ID="colDefault8" runat="server" DataField="Default8" Hidden="True" HeaderText="自定义8"/>
<x:BoundField ID="colDefault9" runat="server" DataField="Default9" Hidden="True" HeaderText="自定义9"/>
<x:BoundField ID="colDefault10" runat="server" DataField="Default10" Hidden="True" HeaderText="自定义10"/>
<x:BoundField ID="colDefault11" runat="server" DataField="Default11" Hidden="True" HeaderText="自定义11" />
<x:BoundField ID="colDefault12" runat="server" DataField="Default12" Hidden="True" HeaderText="自定义12"/>
<x:BoundField ID="colMemo" runat="server" DataField="Memo" Hidden="True" HeaderText="备注"/>
<x
inkButtonField runat="server" ID="deleteField" HeaderText="删除" Text="删除" ConfirmText="确定删除此记录?"
ConfirmTitle="提示信息" CommandName="delete" Width="40px"></x
inkButtonField>
</Columns>
</x:Grid>
作者:
游泳的鱼
时间:
2013-12-30 21:30
cs代码:
//从表
IEnumerable<BCostProjectAppConfigDetail> dtconfig =
lists.Where(c => c.TableName == BCostProjectInventoryApplyDetail.Schema.TableName);
string script = String.Format("var colmodel = X('{0}').getColumnModel();",gridDetail.ClientID);;
string headerScript ="colmodel.setColumnHeader(colmodel.getIndexById('{0}'), '{1}');";
foreach (var detail in dtconfig)
{
#region 控制列显示
switch (detail.FieldName.ToLower())
{
case "cpid":
colCPName.Hidden = false;
colCPName.HeaderText = detail.ChinaName;
script += String.Format(headerScript, colCPName.ClientID, detail.ChinaName);
break;
case "inventoryid":
colInvCCode.Hidden = false;
colInvCCode.HeaderText = detail.ChinaName;
script += String.Format(headerScript, colInvCCode.ClientID, detail.ChinaName);
break;
case "inventorycode":
colInventoryCode.Hidden = false;
colInventoryCode.HeaderText = detail.ChinaName;
script += String.Format(headerScript, colInventoryCode.ClientID, detail.ChinaName);
break;
case "inventoryname":
colInventoryName.Hidden = false;
colInventoryName.HeaderText = detail.ChinaName;
script += String.Format(headerScript, colInventoryName.ClientID, detail.ChinaName);
break;
case "comunitname":
colComUnitName.Hidden = false;
colComUnitName.HeaderText = detail.ChinaName;
script += String.Format(headerScript, colComUnitName.ClientID, detail.ChinaName);
break;
case "invstd":
colInvStd.Hidden = false;
colInvStd.HeaderText = detail.ChinaName;
script += String.Format(headerScript, colInvStd.ClientID, detail.ChinaName);
break;
case "invsprice":
colInvSPrice.Hidden = false;
colInvSPrice.HeaderText = detail.ChinaName;
script += String.Format(headerScript, colInvSPrice.ClientID, detail.ChinaName);
break;
case "dnumber":
colDNumber.Hidden = false;
colDNumber.HeaderText = detail.ChinaName;
script += String.Format(headerScript, colDNumber.ClientID, detail.ChinaName);
break;
case "dprice":
colDPrice.Hidden = false;
colDPrice.HeaderText = detail.ChinaName;
script += String.Format(headerScript, colDPrice.ClientID, detail.ChinaName);
break;
case "dmoney":
colDMoney.Hidden = false;
colDMoney.HeaderText = detail.ChinaName;
script += String.Format(headerScript, colDMoney.ClientID, detail.ChinaName);
break;
case "usage":
colUsage.Hidden = false;
colUsage.HeaderText = detail.ChinaName;
script += String.Format(headerScript, colUsage.ClientID, detail.ChinaName);
break;
case "memo":
colMemo.Hidden = false;
colMemo.HeaderText = detail.ChinaName;
script += String.Format(headerScript, colMemo.ClientID, detail.ChinaName);
break;
case "default1":
colDefault1.Hidden = false;
colDefault1.HeaderText = detail.ChinaName;
script += String.Format(headerScript, colDefault1.ClientID, detail.ChinaName);
break;
case "default2":
colDefault2.Hidden = false;
colDefault2.HeaderText = detail.ChinaName;
script += String.Format(headerScript, colDefault2.ClientID, detail.ChinaName);
break;
case "default3":
colDefault3.Hidden = false;
colDefault3.HeaderText = detail.ChinaName;
script += String.Format(headerScript, colDefault3.ClientID, detail.ChinaName);
break;
case "default4":
colDefault4.Hidden = false;
colDefault4.HeaderText = detail.ChinaName;
script += String.Format(headerScript, colDefault4.ClientID, detail.ChinaName);
break;
case "default5":
colDefault5.Hidden = false;
colDefault5.HeaderText = detail.ChinaName;
script += String.Format(headerScript, colDefault5.ClientID, detail.ChinaName);
break;
case "default6":
colDefault6.Hidden = false;
colDefault6.HeaderText = detail.ChinaName;
script += String.Format(headerScript, colDefault6.ClientID, detail.ChinaName);
break;
case "default7":
colDefault7.Hidden = false;
colDefault7.HeaderText = detail.ChinaName;
script += String.Format(headerScript, colDefault7.ClientID, detail.ChinaName);
break;
case "default8":
colDefault8.Hidden = false;
colDefault8.HeaderText = detail.ChinaName;
script += String.Format(headerScript, colDefault8.ClientID, detail.ChinaName);
break;
case "default9":
colDefault9.Hidden = false;
colDefault9.HeaderText = detail.ChinaName;
script += String.Format(headerScript, colDefault9.ClientID, detail.ChinaName);
break;
case "default10":
colDefault10.Hidden = false;
colDefault10.HeaderText = detail.ChinaName;
script += String.Format(headerScript, colDefault10.ClientID, detail.ChinaName);
break;
case "default11":
colDefault11.Hidden = false;
colDefault11.HeaderText = detail.ChinaName;
script += String.Format(headerScript, colDefault11.ClientID, detail.ChinaName);
break;
case "default12":
colDefault12.Hidden = false;
colDefault12.HeaderText = detail.ChinaName;
script += String.Format(headerScript, colDefault12.ClientID, detail.ChinaName);
break;
}
#endregion
}
PageContext.RegisterStartupScript(script);
欢迎光临 FineUI 官方论坛 (https://fineui.com/BBS/)
Powered by Discuz! X3.4