referencing the data array using sName instead of position integer

referencing the data array using sName instead of position integer

kjmkjm Posts: 19Questions: 0Answers: 0
edited November 2009 in General
Hi,

I'd like to be able to reference the columns somehow using the 'sName' property instead of the numberical position

for example like [code] aData["MobileNumber"] [/code] instead of [code] aData[12][/code].

Is this possible?

thanks

I have my code below

[code]
oTable = $('#referralTable').dataTable(
{
"bFilter": true,
"bJQueryUI": true,
"sPaginationType": "full_numbers",
"bProcessing": false,
"bAutoWidth": false,
"sAjaxSource": "../Referral/Search",
"aoColumns": [
{ "bVisible": false, "sName": "ReferralId" },
{ "sTitle": "Referral Date", "sName": "ReferralDate" },
{ "sTitle": "Assessment Date", "sName": "AssessmentDate", "bVisible": false },
{ "sTitle": "CRN", "sName": "CRN" },
{ "sTitle": "JSID", "sName": "JSID" },
{ "sTitle": "Name", "sWidth": "200px", "sName": "Name" },
{ "sTitle": "Site", "sWidth": "300px", "sName": "ProviderSiteName", "bVisible": false },
{ "sName": "AssessmentDeliveryMode", "bVisible": false },
{ "sTitle": "Agency", "sName": "ReferralAgencySource" },
{ "sTitle": "Agency Source", "sName": "ReferralAgencySourceSite" },
{ "bVisible": false, "sName": "CSC" },
{ "bVisible": false, "sName": "DateOfBirth" },
{ "bVisible": false, "sName": "Gender" },
{ "bVisible": false, "sName": "Mobile" },
{ "bVisible": false, "sName": "Phone" },
{ "bVisible": false, "sName": "ResidentialAddressLine1" },
{ "bVisible": false, "sName": "ResidentialAddressLine2" },
{ "bVisible": false, "sName": "ResidentialAddressPostCode" },
{ "bVisible": false, "sName": "ResidentialAddressState" },
{ "bVisible": false, "sName": "PostalAddressLine1" },
{ "bVisible": false, "sName": "PostalAddressLine2" },
{ "bVisible": false, "sName": "PostalAddressPostCode" },
{ "bVisible": false, "sName": "PostalAddressState" },
{ "bVisible": false, "sName": "PnterpreterRequired" },
{ "bVisible": false, "sName": "Interpreterlangauge" },
{ "bVisible": true, "sTitle": "Provider Site", "sName": "ProviderSiteAddress" },
{ "bVisible": false, "sName": "ProviderSiteServiceCode" },
{ "sName": "ReferralAssessmentStatus", "bVisible": false }


],
[/code]

I then reference these columns using the position in the array like this

[code]
function OnGridRowClick() {

/* Get the position of the current data from the node */
var aPos = oTable.fnGetPosition(this);
/* Get the data array for this row */
var aData = oTable.fnGetData(aPos[0]);
/* Update the data array and return the value */

alert(aData["assessmentDate"]);



$("#uxReferralDate").html(aData[1]);
$("#uxPTAAppoinmentDate").html(aData[2]);
$("#uxCRN").html(aData[3]);
$("#uxJobseekerId").html(aData[4]);
$("#uxName").html(aData[5]);
//6=referral status
$("#uxProviderName").html(aData[7]);
//8=delivery mode
$("#uxReferralSource").html(aData[9]);
$("#uxReferralAgencySite").html(aData[10]);
$("#uxCSCCode").html(aData[11]);

$("#uxDateOfBirth").html(aData[12]);
$("#uxGender").html(aData[13]);
$("#uxMobile").html(aData[14]);
$("#uxPhone").html(aData[15]);

$("#uxResidentialAddress").html(aData[16] + ", " + aData[17] + ", " + aData[18] + " " + aData[19]);
$("#uxPostalAddress").html(aData[20] + ", " + aData[21] + ", " + aData[22] + " " + aData[23]);

$("#uxInterpreterRequired").html(aData[24]);
$("#uxLanguage").html(aData[25]);

$("#uxProviderSiteAddress").html(aData[26]);
$("#uxSiteServiceCode").html(aData[27]);

$("#uxAssessmentStatusValue").html(aData[28]);

$('#uxReferralDetailsDialog').dialog("open");

//$("Description").html("CRN example");
}

[/code]

Replies

  • kjmkjm Posts: 19Questions: 0Answers: 0
    I think I have found the solution in the fnGetColumnIndex() extension...haven't tried it yet but will keep posted if not working
  • allanallan Posts: 63,523Questions: 1Answers: 10,473 Site admin
    Hi kjm,

    This isn't possible in DataTables core itself since it doesn't work like this internally, however, what you might be able to do is construct a array which you can use for yourself which will give pointers to the columns in DataTables. ie.

    [code]
    var aNames = [];
    aNames["first"] = 0;
    aNames["second"] = 1;

    var oFirstColumn = oTable.fnSettings().aoColumns[ aNames["first"] ];
    [/code]
    Regards,
    Allan
  • kjmkjm Posts: 19Questions: 0Answers: 0
    Hi Allan,

    thanks for your response.

    there is an extension written by Michael Ross called fnGetColumnIndex which seems to do the job

    [code]alert(aData[oTable.fnGetColumnIndex("CRN")]);[/code]

    [code]
    /*
    * Function: fnGetColumnIndex
    * Purpose: Return an integer matching the column index of passed in string representing sTitle
    * Returns: int:x - column index, or -1 if not found
    * Inputs: object:oSettings - automatically added by DataTables
    * string:sCol - required - string matching the sTitle value of a table column
    */
    $.fn.dataTableExt.oApi.fnGetColumnIndex = function(oSettings, sCol) {
    var cols = oSettings.aoColumns;
    for (var x = 0, xLen = cols.length; x < xLen; x++) {
    if (cols[x].sTitle.toLowerCase() == sCol.toLowerCase()) {
    return x;
    };
    }
    return -1;
    }

    [/code]

    this could probably be changed to work with the sName property as well

    Kurt
  • allanallan Posts: 63,523Questions: 1Answers: 10,473 Site admin
    Hi Kurt,

    Yes indeed, I think the only change needed would be to change "cols[x].sTitle" to "cols[x].sName".

    Regards,
    Allan
This discussion has been closed.