Page 1 of 2 12 LastLast
Results 1 to 10 of 13

Thread: How to send records of grid (in XML format) back to server?

  1. #1
    Ext JS Premium Member
    Join Date
    Mar 2007
    Location
    IL, USA
    Posts
    141

    Default How to send records of grid (in XML format) back to server?

    I am trying to send records inside a editable grid back to the server. I know I can use Ext.util.JSON.encode() to convert them into JSON format, but how can I send them back in XML format? (If possible, using the same format with the XML loaded by Ext.data.XMLReader). Does Ext provide some functions like that? Thanks.

  2. #2
    Sencha User Animal's Avatar
    Join Date
    Mar 2007
    Location
    Bédoin/Nottingham
    Posts
    30,892

    Default

    Code:
    Ext.override(Ext.data.Record, {
        asXml : function(rowIndex) {
        	var r = this.store.reader.meta;
        	var elName = r.record;
        	var result = "<" + elName + " id=\"" + this.id + "\">";
       		this.fields.each(function(f) {
       			result += "<" + f.name;
       			if (f.type && f.type !== "auto") {
       				result += " type=\"" + f.type + "\"";
       			}
       			result += ">" + this.get(f.name) + "</" + f.name + ">";
       		}, this);
         	return result + "</" + elName + ">";
        }
    });
    That adds an asXml method to the Record class so that you can serialize the modified Records from your Store, and post them back to your server.

  3. #3

    Default Usage

    Yo Animal,

    What is the usage for your code?

    I keep getting 'asXML is not a function' error:

    Thanks!

    Some of the stuff I've tried:
    grid.store.getAt(0).asXml;
    grid.store.getAt(0).asXml();
    grid.store.getAt(0).asXml(0);
    grid.store.asXML(0);

    //SingleSlectionModel
    gAttrs.getSelectionModel().getSelected().asXml;
    gAttrs.getSelectionModel().getSelected().asXml();
    gAttrs.getSelectionModel().getSelected().asXml(0);

    rec = gAttrs.getSelectionModel().getSelected();
    rec.asXml;
    rec.asXml();
    rec.asXml(0);

  4. #4
    Ext User
    Join Date
    Mar 2007
    Posts
    34

    Default

    how to read entire gridl ?

  5. #5
    Sencha User Animal's Avatar
    Join Date
    Mar 2007
    Location
    Bédoin/Nottingham
    Posts
    30,892

  6. #6

    Lightbulb Where was this when I needed it?

    Quote Originally Posted by Animal View Post
    Code:
    Ext.override(Ext.data.Record, {
        asXml : function(rowIndex) {
        	var r = this.store.reader.meta;
        	var elName = r.record;
        	var result = "<" + elName + " id=\"" + this.id + "\">";
       		this.fields.each(function(f) {
       			result += "<" + f.name;
       			if (f.type && f.type !== "auto") {
       				result += " type=\"" + f.type + "\"";
       			}
       			result += ">" + this.get(f.name) + "</" + f.name + ">";
       		}, this);
         	return result + "</" + elName + ">";
        }
    });
    That adds an asXml method to the Record class so that you can serialize the modified Records from your Store, and post them back to your server.
    I went through hell last night trying to do this and why this thread never came up in my search? I don't know. But I got it done the hard way.

    Animal - Possible you could influence the developers to include this as part of Ext.util.JSON?

  7. #7
    Ext User
    Join Date
    Mar 2007
    Location
    Boston
    Posts
    349

    Default

    You should probably escape special characters when constructing xml what way.

    Sanjiv

  8. #8

    Default It's very useful

    Thank you in advance ,Animal .
    but i find a little thing :
    in xmlReader:
    PHP Code:
    var myReader = new Ext.data.XmlReader({
       
    totalRecords"results"// The element which contains the total dataset size (optional)
       
    record:"row",        // The repeated element which contains row information
       
    id"id"                 // The element within the row that provides an ID for the record (optional)
    }, Employee); 
    but in jsonreader , there is no "rs.recore " but only "rs.root" :
    PHP Code:
    var myReader = new Ext.data.JsonReader({
        
    totalProperty"results",    // The property which contains the total dataset size (optional)
        
    root"rows",                // The property which contains an Array of row objects
        
    id"id"                     // The property within each row object that provides an ID for the record (optional)
    }, Employee); 

    so, i change your code "rs.record" to "rs.root" with jsonReader .
    BTW,Can you tell more about "store.reader.meta" ,Thanks a lot.
    Last edited by Jim Chen; 19 Nov 2007 at 11:49 PM. Reason: modify the code

  9. #9

    Default form toXML() method

    Hi guys, found this post while looking for ways to post my form as XML back to the server. Here is something Im using to serialise the whole form, its blunt but does the job:

    // Override Ext.form.BasicForm to add Serialization functionality
    Ext.override(Ext.form.BasicForm, {
    toXml : function() {
    var _xml = '<ResultSet>\n<Result>\n';
    var _values = this.getValues();
    for (var prop in _values) {
    _xml+= String.format('<{0}>{1}</{0}>\n',prop, _values[prop]);
    };
    _xml += '</Result>\n</ResultSet>';
    return _xml;
    }
    });

  10. #10

    Default to use it

    In order to use this, when you have instantiated a form panel try this:

    alert(myFormPanel.getForm().toXML());

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •