0

I have a data table with link on some of the rows to allow users to view details of selected record. I have the following on "View" cell and associated js function:

columns: [
    {
        ....
    },{
        "render": function (data, type, row) {
            var active = row.Active;
            var today = new Date();
            var startDate = new Date(row.StartDate)
            var cellContent = "";

            if (startDate > today && active == true) {
                cellContent = "<span class=\"PageLink\"><a href='javascript:' onclick=\"setDetailFields(" + row + ")\">View</a></span>";
            }
            return cellContent;
        }
    },{
        ....
    }
],

function setDetailFields(record) {  
    clear();
    var reqID = record.RequestID;
    var reqName = record.RequesterName;
    ....
}

When I click the "View" link I get the following JS error:

// JavaScript critical error at line 19, column 1 in (unknown source location)\n\nSCRIPT1007: Expected ']'

debugger shows the following code block when error occurs:

function onclick(event)
{
setDetailFields([object Object])
}
4
  • [object Object] is not valid JS syntax; what were you intending to do? Commented Aug 20, 2019 at 18:11
  • This is what VS debugger shows when it throws the js error. I am trying to pass the entire row of the data table as an argument to a js function. Commented Aug 20, 2019 at 18:20
  • The error message itself says that there is a missing closing brace somewhere on the 19th line on your JS code. Commented Aug 20, 2019 at 18:53
  • I don't think I am missing a bracket; data table is displayed correctly (it would barf if I was missing a bracket in its definition) and I have no opening bracket in js function to miss a closing one. I thought maybe the data structure, or type, of "row" that I am trying to pass to js function is the problem. It seems to be an "object" and not sure if it needs to be handled differently passed as an argument. Commented Aug 20, 2019 at 19:12

3 Answers 3

3

The problem is that when you cook up <span> HTML within render function you attempt to convert object into string, so your HTML looks like

"<span class="PageLink"><a href="javascript:" onclick="setDetailFields([object Object])">View</a></span>"

In your page source.

Instead, I may suggest to put click handler into your code:

$('table').on('click', '.PageLink', function(){
   let rowData = $('table').DataTable().row($(this).closest('tr')).data();
   let reqID = rowData.RequestID;
   let reqName = rowData.RequesterName;
   ...
})

(you should change 'table' for some proper selector in your actual code)

And change cellContent inside your render function to simply:

cellContent = '<span class="PageLink">View</span>';

You may decorate that <span> to appear as link in your CSS, if you wish:

.PageLink:hover {cursor: pointer}
Sign up to request clarification or add additional context in comments.

4 Comments

Thank you. I made the changes as suggested but the click function never gets hit. I changed 'table' in your example to actual id of the data table: var tblReq = $("#ReqTable").DataTable({... and then $('#tblReq').on('click', '.PageLink', function(){debugger let rowData = $('#tblReq').DataTable().row($(this).closest('tr')).data(); setDetailFields(rowData); })'
You specified different id's in DataTables constructor ('#ReqTable') and click handler ('#tblReq') isn't that a mistake?
I can help you to resolve your issue much faster if you would share live snippet that reproduces your problem, so I can see wider code context. You can use codepen or jsfiddle for that purpose.
Thanks; I will now proceed with waking up! You are right, I used var name of the table (tblReq) instead of its ID (ReqTable). Once I corrected the error, it worked just fine. Thanks a bunch.
0

Ran into same issue. Reason for the issue: Object is converted to string when html is rendered.

The problem can be sorted out in two ways:

Approach 1: add a click handler to the js code(as suggested in the answer above)

Approach 2: Say I want to pass a JSON object named 'row' to the onclick function. As I need a very few attributes of the 'row' object, instead of adding a new handler as in approach 1, I would rather just pass those specific parameters as below:

'<a href="javascript:" onclick="setDetailFields(\'' + row['attribute1'] + '\'\, \'' +row['attribute2'] + '\'\)">View</a>'

Comments

0

Please use JSON.stringify(row) while passing the row object to function, like this:

cellContent = "<span class=\"PageLink\">
<a href='javascript:' onclick=\"setDetailFields(" + 
JSON.stringify(row) + ")\">View</a></span>"; 

Instead of:

cellContent = "<span class=\"PageLink\">
<a href='javascript:' onclick=\"setDetailFields(" + row + 
")\">View</a></span>";

1 Comment

Great answer, but it would be nice if you could format the code snippets so that the changed part is visible without horizontal scrolling.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.