fnRender without mDataProp problem

fnRender without mDataProp problem

grahampcharlesgrahampcharles Posts: 20Questions: 1Answers: 0
edited February 2012 in DataTables 1.9
I'm using fnRender to make "edit" button columns; the data source is an Json object returned from the server, but not filtered there. I let DataTables do all the column defintion:

[code]
oTable = $("#mytable").dataTable({
"bServerSide": false,
"sAjaxSource": "myUrl",
"sServerMethod": "POST",
"aoColumns": [
{ "fnRender": function (o, val) {
return "edit".format(
o.aData['BidderNumber']);
}
},
... ]
});


[/code]

I get the "Requested unknown parameter for column 0" error, but then the table renders absolutely correctly.

Troubleshooting in various ways -- defining the columns explictly, setting "sName" or "sTitle" on the columns -- those didn't help. But I found that the error goes away if I define mDataProp on the column with the fnRender (even though I don't use the value):

[code]
"aoColumns": [
{ "mDataProp": "ColumnIDontUse", "fnRender": function (o, val) {
return "edit".format(
o.aData['BidderNumber']);
}
},
[/code]

I've just started using 1.9 -- I can't say for sure, but I'd say this problem didn't occur in 1.8.x -- I used roughly the same pattern a lot and never found it to be a problem.

I'm not especially worried about this, since it's easy enough to define an mDataProp for every column -- just thought I'd report it.

g.

Replies

  • allanallan Posts: 63,498Questions: 1Answers: 10,470 Site admin
    Thanks for the detailed explanation - I think that DataTables is correct in giving an error here since the mDataProp value is used for storing the information as well as getting it - so when you set the information with fnRender that value needs to be stored somewhere. By default mDataProp is the column index integer, so it should go there and can then be read back from that same location when needed in future.

    It should be that mDataProp is defined automatically to use the column index for the column that it is operating on, so I don't know where the 'undefined' is coming from in the error message - I would expect an integer to be given - for example: http://live.datatables.net/avujac/edit .

    Thinking about the changes since 1.8 - fnRender will now give you the value of the cell as the second property to try and make life easier, so it is trying to read the current cell value, hence the error. So yes, I think you probably could have got away with this in 1.8 - but not in 1.9. I'll think on this a bit to see how it might be improved!

    Allan
  • grahampcharlesgrahampcharles Posts: 20Questions: 1Answers: 0
    Sounds good! I've also noted source code around here with "mDataProp": null -- but I haven't tried that yet. I'm happy to test some alternatives if that would help!

    It's probably relatively common to define a column with "delete, edit" buttons -- what's the preferred method?

    In any event, the workaround works fine... so no worries if this is a fringe case.
  • metacormetacor Posts: 1Questions: 0Answers: 0
    Hi!

    Same here.
    With 1.8.x this worked:

    [code]
    "aoColumns": [
    { "mDataProp": "alias" },
    { "mDataProp": "user_name" },
    { "mDataProp": "email" },
    { "mDataProp": "groups" },
    { "fnRender": function(o) { return 'fixed value"}}
    [/code]

    And mDataProp would save it as a integer [4] silently.
    In 1.9.1 i get the warning. So i have to write:

    [code]
    "aoColumns": [
    { "mDataProp": "alias" },
    { "mDataProp": "user_name" },
    { "mDataProp": "email" },
    { "mDataProp": "groups" },
    { "mDataProp": null, "fnRender": function(o) { return 'fixed value"}}
    [/code]

    No big deal but i think it breaks backwards compatibility a bit. It can be very difficult to guess!!
  • allanallan Posts: 63,498Questions: 1Answers: 10,470 Site admin
    In retrospect yes, this is an annoying backwards compatibility issue :-(. However is is a useful feature in 1.9 and removing it now would again break backwards compatibility! So I think leaving it as it stands is the way to go, and I'll need to ask you to accept my apologies.

    Allan
This discussion has been closed.