How to add a value to post

How to add a value to post

pwc1011pwc1011 Posts: 62Questions: 0Answers: 0
edited January 2010 in General
In the example, it is possible to push a value onto the aoData stack before an ajax post. However, I can't figure out how to get a dynamic value from the table, since there is no handle to the table when in the fnserverData function.

[code]
fnServerData: function (sSource, aoData, fnCallback) {
aoData.push({"id": ??? });
$.ajax({
"dataType": 'json',
"type": "POST",
"url": sSource,
"data": aoData,
"success": fnCallback
});
}

[/code]

Thanks,
Patrick

Replies

  • allanallan Posts: 63,498Questions: 1Answers: 10,470 Site admin
    Hi Patrick,

    It's jQuery's wacky key/value pair set up that you need to use: http://datatables.net/examples/server_side/custom_vars.html

    Something like this:
    aoData.push( { "name": "more_data", "value": "my_value" } );

    Regards,
    Allan
  • pwc1011pwc1011 Posts: 62Questions: 0Answers: 0
    Allan,

    I can get the value pushed, what I can't get is the value.

    For example, from within the function fnServerData, I need to get a list of classes on the table. I've tried parent, parents("table"), etc... and I can't seem to get a handle within the function for the table for which the retrieve is being executed.

    Patrick
  • allanallan Posts: 63,498Questions: 1Answers: 10,470 Site admin
    Oh - you want a reference to oTable (or whatever the result of $().dataTable() is)? I think (and guessing a bit here) is that if you are getting oTable as undefined at this point, then it's because it hasn't actually finished being created at this point! Given that the table will be empty to start with, you could do a test for oTable being undefined? Assuming I'm right here of course...

    Allan
  • pwc1011pwc1011 Posts: 62Questions: 0Answers: 0
    I think we're on the same page, or close anyway. Even if oTable isn't defined, there is the table html. It seems like somehow I should be able to get a pointer to however much of oTable exists or the table html being used to create the table? Maybe?

    Patrick
  • allanallan Posts: 63,498Questions: 1Answers: 10,470 Site admin
    The thing is that none of oTable exists until the constructor function is finished - which is when it exits. However the Ajax call is made during that initialisation! Hence what I think you are seeing. What is it exactly that you are trying to do - there might be away around it using the settings object (which presents the internal functions) - although looking at the code, you can't actually access oSettings in fnServerData at the moment... Hmmm...

    Allan
  • pwc1011pwc1011 Posts: 62Questions: 0Answers: 0
    Actually, I resolved it by tweaking your code a bit... something I hate doing!

    I added an additional input when creating the table "yltID" and if it exists, I add it to the settings for the table and push it on the aoData stack before the ajax fetch.

    I could use the standard get ../../Blah/blah/1, but I'm trying to use posts as much as possible. In this case, the 1 is the unique id for the query. Example, a list of actions for project 1 - (pseudo sql statement) "select all actions where projectid = 1"

    Since all my tables will have at a minimum an id passed as part of the parameters, I had to figure something out and move along.

    Possibly you have a better option now that you know what I'm trying to accomplish?

    Patrick
  • allanallan Posts: 63,498Questions: 1Answers: 10,470 Site admin
    Not really at the moment. It suspect there would be a 'better' or easier way if the settings object was available in the server data function - but it's currently not. I'll think about how best to add than in future.

    Allan
This discussion has been closed.