Row ID after Editor Update

Row ID after Editor Update

psenechalpsenechal Posts: 32Questions: 2Answers: 0
edited February 2013 in Editor
Not sure if this question should go in the General Forum, or here in the Editor Forum.

I have a dataTable configured using Editor and everything works PERFECTLY. Love this solution. Here's my problem I'm trying to figure out.

When I Create or Update a record using Editor, everything works except for the following scenario:

On certain pages, I have created an addition external link besides Update and Delete. This link takes you to another page that updates a list of values for the record you were just on. To explain it better, I have a table that is a list of defined Metrics. You can update or delete each Metric using links I've placed in a column of the table:

[code] {
'mData': null,
'bSortable': false,
'sClass': 'center nowrap',
'sDefaultContent': '  '
}
[/code]

Notice the last link named "Values". This calls a function I've created:

[code]
// Values
$('.datatable').on('click', 'a.editor_values', function (e) {
e.preventDefault();

var row = $(this).closest('tr'),
data = oTable._(row),
id = data[0].id;

window.location = '/Metrics/Metric-Values/' + id;
});
[/code]

This links to another page, passing the id of the row as a parameter to make sure we load values for that particular record. Each Metric record can have unlimited Values associated with it.


Now here's the problem. When I load the Metrics page, clicking on that Value link works absolutely fine...it passes the proper row id. However, if I create a New record or Update an existing record, somehow that row id is no longer available and I end up with "undefined" as the parameter when I click on Values and redirect.

All of my JSON results contain an id value and a row[DT_RowId] value when doing creates and updates, and if I inspect the DOM AFTER doing an update, I can still see the correct id attached to the row. It's just somehow not accessible in my a.editor_values function anymore.

I know that was pretty complicated to explain, but if you have any possible suggestions on a way to remedy this situation, I'd appreciate them. It's not a huge deal to ask people to refresh the page, but I'd rather not have to.

Thanks so much!

Replies

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    I'd add a `console.log( data )` in there (just after your `_` call) and see what the dump is there. In theory it looks like it should work to me! Any chance you can give me a link to a test case?

    Good to hear that your Editor testing is going well.

    Regards,
    Allan
  • psenechalpsenechal Posts: 32Questions: 2Answers: 0
    edited February 2013
    I placed that just before the redirect and got the following:

    [Object]
    0: Object
    length: 1
    __proto__: Array[0]

    if I do the same thing WITHOUT editing the row beforehand, I also get:

    [Object]
    0: Object
    length: 1
    __proto__: Array[0]


    I honestly have absolutely no idea what that means, but I'm not seeing any difference in what it's outputting. Any ideas?
  • psenechalpsenechal Posts: 32Questions: 2Answers: 0
    I changed the line that sets the id to the following:

    id = $(this).parents('tr').attr('id');

    This seems to work okay. Do you see any concerns with doing it this way? Thanks!
  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    No - that looks like a valid way of getting the ID!

    Having said that, it should be in the data source object you are working with - what is in the `object` that your console debug dumps? `console.log( data[0] )` .

    Allan
  • psenechalpsenechal Posts: 32Questions: 2Answers: 0
    edited February 2013
    Before I update a record using Editor, I seem to have a full object
    [quote]
    Active
    true

    DT_RowId
    3

    Description
    ""

    Enterprise
    true

    Faq
    false

    Frequency
    ""

    Legend
    true

    Name
    "Call Response"

    New
    true

    ReportId
    "SCFR0006"

    Sample
    true

    Schedulable
    true

    SortOrder
    3

    Static
    false

    StaticUrl
    ""

    Updated
    true

    id
    3
    [/quote]

    after I update, the object looks like this

    [quote]
    Active
    true

    DT_RowId
    3

    Description
    ""

    Engine
    1

    Enterprise
    true

    Faq
    false

    Frequency
    ""

    Legend
    true

    Name
    "Call Response"

    New
    true

    ReportId
    "SCFR0006"

    Sample
    true

    Schedulable
    true

    SortOrder
    3

    Static
    false

    StaticUrl
    ""

    Updated
    true
    [/quote]

    You can see that it's missing the id value. I wonder if this is because of the way I'm returning the updated and new objects...

    [code]
    var response = new {
    id = report.Id,
    row = new {
    DT_RowId = report.Id,
    ReportId = report.ReportId,
    Engine = report.Engine,
    Name = report.Name,
    Description = report.Description,
    Frequency = report.Frequency,
    SortOrder = report.SortOrder,
    Active = report.Active,
    Schedulable = report.Schedulable,
    Sample = report.Sample,
    Legend = report.Legend,
    Faq = report.Faq,
    Enterprise = report.Enterprise,
    New = report.New,
    Updated = report.Updated,
    Static = report.Static,
    StaticUrl = report.StaticUrl
    }
    };

    return Json(response, JsonRequestBehavior.AllowGet);
    [/code]

    I'm using .NET. I've been setting the id value outside of the row values. Is that incorrect?
  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    Setting the id as you are is correct because Editor requires that ( http://editor.datatables.net/server/ ), however, your id isn't int he data source object that is given to DataTables, so you'd need to also add it in there.

    Editor 1.2.3 has a new `idSrc` option so you could use your `id` property rather than `DT_RowId` btw. Just if you want to reduce redundant information.

    Allan
This discussion has been closed.