使用Telerik RadGrid如何从客户端隐藏GridHyperLinkColumn?

时间:2012-10-05 23:13:18

标签: c# javascript asp.net telerik telerik-grid

我正在使用Telerik RadGrid而我正在做客户端数据绑定。这是我的网格:

<telerik:RadGrid ID="radGrid_Assignments" runat="server" AllowPaging="True" AutoGenerateColumns="false" 
                     AllowSorting="true" AllowFilteringByColumn="true" >
        <PagerStyle Mode="NextPrevAndNumeric" />
        <MasterTableView NoMasterRecordsText="No assigments found for the search criteria.">
            <Columns>
                <telerik:GridHyperLinkColumn HeaderText="Customer Name" DataTextField="CustomerName" Target="_customer"
                                             DataNavigateUrlFormatString="~/Modules/DataEntry/ProviderContact/Edit.aspx?id={0}"
                                             DataNavigateUrlFields="CustomerID" SortExpression="cust.Name" UniqueName="cust.Name"
                                             HeaderStyle-Width="200px" />
                <telerik:GridBoundColumn DataField="JobTitle" HeaderText="Title" HeaderStyle-Width="250px" />
                <telerik:GridBoundColumn DataField="ProviderName" HeaderText="Provider" HeaderStyle-Width="250px" />
                <telerik:GridHyperLinkColumn DataNavigateUrlFields="SurveyType,EvaluationID,SurveyID" DataNavigateUrlFormatString="~/Modules/AssignmentPortal/Evaluation/Enter{0}.aspx?EvalID={1}&sid={2}&menu=false" DataTextField="SurveyName" HeaderStyle-Width="200px" HeaderText="Survey Name" SortExpression="s.Description" Target="_survey" UniqueName="s.Description" />
                <telerik:GridBoundColumn DataField="ProductName" HeaderText="Product" HeaderStyle-Width="150px" />
                <telerik:GridBoundColumn DataField="WorkPhone" UniqueName="cust.WorkPhone" SortExpression="cust.WorkPhone" HeaderText="Work Phone"
                                         HeaderStyle-Width="150px" />
                <telerik:GridHyperLinkColumn HeaderText="Email" DataTextField="Email" UniqueName="cust.Email"
                                             DataNavigateUrlFormatString="mailto:{0}" DataNavigateUrlFields="Email" SortExpression="cust.Email"
                                             HeaderStyle-Width="300px" />
                <telerik:GridBoundColumn DataField="NoteDateTime" HeaderText="Last Note Date" DataFormatString="{0:MM/dd/yyyy hh:mm tt}"
                                         HeaderStyle-Width="150px" />
                <telerik:GridBoundColumn DataField="NoteText" HeaderText="Last Note" UniqueName="cln.NoteText"
                                         HeaderStyle-Width="400px" />
            </Columns>
        </MasterTableView>
        <ClientSettings>
            <DataBinding Location="/AjaxServices/SearchService" SelectMethod="GetAssignmentsSearchResults" />
            <ClientEvents OnCommand="showLoadingPanel" OnDataBound="OnClientDataBound" OnDataBinding="OnClientDataBinding" />
        </ClientSettings>
    </telerik:RadGrid>

当SpatialCoordinatesKLASID的DataNavigateUrlFields为NULL时,我希望能够使用HeaderText =“KLAS Maps”隐藏GridHyperLinkColumn。我只想要一个空列。

我无法使用服务器端OnItemDataBound事件的传统方式。因为这是在客户端加载。

我该怎么做?提前感谢您的帮助。

2 个答案:

答案 0 :(得分:3)

请尝试使用以下代码/演示。

 function DataBound(sender, args) {

            var grid = $find("<%= RadGrid1.ClientID %>");
            if (grid) {

                var IsColumnHaveData = false;
                var MasterTable = grid.get_masterTableView();
                var Rows = MasterTable.get_dataItems();
                for (var i = 0; i < Rows.length; i++) {
                    var row = Rows[i];


                    if (row.get_dataItem().EmployeeName != null) {
                        IsColumnHaveData = true;
                    }

                }


                var Columns = MasterTable.get_columns();
                for (var i = 0; i < Columns.length; i++) {
                    var column = Columns[i];
                    if (column.get_uniqueName() == "EmployeeName") {
                        column.set_visible(IsColumnHaveData);
                    }
                }

            }


        }



<ClientSettings>
            <DataBinding Location="http://localhost/WcfService1/Service1.svc" SelectMethod="GetData"
                SortParameterType="Linq" FilterParameterType="Linq">
            </DataBinding>
            <ClientEvents OnDataBound="DataBound" />
        </ClientSettings>

请在以下链接代码/演示中应用以上代码。

RadGrid with WCF Rest Service

隐藏特定链接。

function DataBound(sender,args){

            var grid = $find("<%= RadGrid1.ClientID %>");
            if (grid) {

                var IsColumnHaveData = false;
                var MasterTable = grid.get_masterTableView();
                var Rows = MasterTable.get_dataItems();
                for (var i = 0; i < Rows.length; i++) {
                    var row = Rows[i];
                    row.get_cell("EmployeeName").getElementsByTagName("a")[0].style.display = 'none';
                }
            }
        }

答案 1 :(得分:0)

我会通过jQuery的onload使用不引人注目的JavaScript。看看这个jsfiddle

您需要分析生成的HTML才能生成正确的功能。