1

I am trying to create a jqGrid using the result of a jQuery selection from an XML string.

I have a page that loads a large XML response (SOAP). I would like to save the initial response and dynamically use portions of the response in different jqGrid tables. When I search for the portion of the XML using jQuery, I get the correct portion of the response for the table, but I am stuck at the point of building a table from the jQuery result object. The loatTabTrip function below works fine with a raw string of XML given as the travelXml argument, but it does not work with the jQuery result. (So if I could get the jQuery object as a string of XML, that would work; or if jqGrid would accept the object.)

Any ideas?

<Travel>
    <TravelKey>2010020029A</TravelKey>
    <TravelCategoryCode>AIR</TravelCategoryCode>
    <Trips>
        <Trip>
            <AircraftTrip>
                <TripKey>
                    <TripId>50</TripId>
                    <TravelKey>2010020029A</TravelKey>
                </TripKey>
                <AssignedTravelTaskReferences>
                    <AssignedTravelTaskReference>
                        <TaskId>80203</TaskId>
                        <TravelKey>2010020029A</TravelKey>
                    </AssignedTravelTaskReference>
                </AssignedTravelTaskReferences>
                <TripTravelId>6JG79822S</TripTravelId>
                <Aircraft>
                    <AircraftModelCode>KC135R</AircraftModelCode>
                    <SerialNumber>83104038</SerialNumber>
                </Aircraft>
                <Route>...</Route>
            </AircraftTrip>
        </Trip>
        <Trip>...</Trip>
</Travel> 

function loadTabTrip(travelXml){
    jQuery("#travelTripsTable").jqGrid({
        datatype:'xmlstring',
        datastr: travelXml, 
        colNames:["ID","Aircraft"], 
        colModel:[
            {name:"id",index:"id", width:380, align:"right",xmlmap:">TripKey>TripId"},
            {name:"type",index:"type", width:80, xmlmap:">Aircraft>AircraftModelCode"}
        ],
        xmlReader: {
            root : "Travel>Trips",
            row: ">Trip>AircraftTrip",
            repeatitems: false,
            id: ">TripKey>TripId"
        }
    });
}
jcadcell
  • 791
  • 1
  • 8
  • 19

1 Answers1

1

You can use the jQuery .get method to retrieve the underlying DOM element - or in this case, the XML element.

From there, you can use the function from Convert xml to string with jQuery to convert that XML object into a string:

datastr: xmlToString( travelXml.get(0) )

Does that help?

Community
  • 1
  • 1
Justin Ethier
  • 131,333
  • 52
  • 229
  • 284
  • Thanks, Justin! I had tried .get() and xmlToString() but I did not understand how jQuery works enough to put those two together correctly. – jcadcell May 16 '12 at 13:20