Ajax DataGrid Support Product Page

Create the Column names on the fly

Asked 24 Jan 2011 00:19:34
has this question
24 Jan 2011 00:19:34 Seb Adlington posted:
Hi Guys,

i need to modify the datagrid script and build up the columns names and details on the fly.
I have a stored procedure which is grabbing x number of fields based on which user is logged in - so i might get 5 columns back, I might get 20.
I currently just use a table and some while loops to build the table (as below)

 <table width="877" border="1" align="center" cellpadding="1" cellspacing="1" bordercolor="#FFFFFF">
      <tr bordercolor="#666666" class="SebTb-Hdr">
        <%While ( (NOT FieldNames.EOF))%><td bgcolor="#021870"><div align="center"><%=(FieldNames.Fields.Item("ColHdrText").Value)%></div></td>
<% FieldNames.MoveNext()
While ((Repeat1__numRows <> 0) AND (NOT History.EOF)) 
      <tr bordercolor="#666666" bgcolor="<%=bgcolourtbl%>">
dim myField
While ((NOT Recordset1.EOF)) 
<%myField = Recordset1.Fields.Item("FieldName")%>
<%=History.Fields.Item(myField) %>
%  Recordset1.MoveNext()

Is it possible for me to do something similar with the datagrid? - ideally I want to put a loop the recreates this bit below according to how many fields I have:

colModel: [{
        display: "<%FROM THE DB%>",
        name: "<%FROM THE DB%",
        width: 100,
        sortable: true,
        align: "left",
        hide: false

Is this possible?

Many thanks



Replied 25 Jan 2011 12:21:39
25 Jan 2011 12:21:39 Seb Adlington replied:
Ok So i am half way there, This first bit of code works and creates my column heading on the fly nicely...

%dim SebData
While ( (NOT FieldNames.EOF)) 

'SebData = SebData & "{display:""" & (FieldNames.Fields.Item("ColHdrText").Value) & """ ,name: """ &Recordset1.Fields.Item("FieldName")& """,width: 100,sortable: true,align: ""left"",hide: false},"

<div class="dmxgrid_pale_blue">
<script type="text/javascript" language="javascript">
    url: "ajax_servers/dmx_grid1.asp",
    showSearchOnLoad: true,
    sortname: "customercontact",
    sortorder: "DESC",
    dataType: "json",
    rpOptions: [5, 10, 15, 20, 25, 30, 35, 40, 45, 50],
    colModel: [<%=SebData%>],
    searchitems: [{
        display: "Order Number",
        name: "orderno"
        display: "Chassis Number",
        name: "chassisno"
<table id="dmx_grid2"><tr><td></td></tr></table>


Then for the Data I tried a similar thing in the dmx_grid page but at this point it falls over

%Dim table, columns, default_filter, default_sort, recs_per_page, debugmode, formats, SebData
connection = MM_Voyager_STRING
table          = "GetVehicleHistory"
    While (NOT FieldNames.EOF)
    SebData = SebData & "array(""" & FieldNames.Fields.Item("ColHdrText").Value & """, "", false),_"

columns        = array(_


default_filter = ""
default_sort   = "ORDER BY contactname DESC"
recs_per_page  = 15
debugmode      = false
formats        = array( _
	array("lcase",             "AlphaCase - Lower"), _
	array("ucase",             "AlphaCase - Upper"), _
	array("ucfirst",           "AlphaCase - First Letter Upper"), _
	array("ltrim",             "Trim - Left"), _
	array("rtrim",             "Trim - Right"), _
	array("trim",              "Trim - Both"), _
	array("nl2br",             "Convert - New Lines to BRs"), _
	array("Server.HTMLEncode", "Encode - HTML Encode"), _
	array("Server.UrlEncode",  "Encode - URL Encode") _
'array("ucwords",           "AlphaCase - Capitalize"), _
'array("rawurlencode",      "Encode - Raw URL Encode"), _
'array("urldecode",         "Decode - URL Decode"), _
'array("rawurldecode",      "Decode - Raw URL Decode"), _
'array("strip_tags",        "HTML - Strip HTML") _


I'm getting an error initialising the variable SebData - Just get an Error saying Missing )

Any ideas...anyone? []

Replied 27 Jan 2011 12:05:54
27 Jan 2011 12:05:54 Miroslav Zografski replied:
Hello Seb,

The Response.Write would be used to display something in a browser. You need to pass the data in the columns array. So maybe following will be better approach:
  While (NOT FieldNames.EOF)  
     columns = push a filed name in the array here. 

Replied 27 Jan 2011 20:22:58
27 Jan 2011 20:22:58 Seb Adlington replied:
Hi Miro,

Thanks for the reply
I didnt think Response.write was appropriate there.
So can I use the pus command with a variable?
In these lines I step through and build up the column syntax like you have in the file

While (NOT FieldNames.EOF)
SebData = SebData & "array(""" & FieldNames.Fields.Item("ColHdrText".Value & """, "", false),_"

Then I wanted to dump the contents of the SebData variable into the columns section - is this not the way to go about it?

I've not used the Push command before but will give it a try as you suggest


Replied 03 Feb 2011 10:52:24
03 Feb 2011 10:52:24 Miroslav Zografski replied:
Hello Seb,

Basically the columns is an array. So it is much more appropriate to use array commands in order to generate that array dynamically.
And Response.Write is not appropriate command.


Reply to this topic