0

I have been struggling with this for the last two hours and I can't get it to work. Take a look to the following piece of code:

$js = '$.extend($.fn.fmatter , {
            userActions : function(cellvalue, options, rowData, addOrEdit) {
            var data = cellvalue.split("|");
            var id = options.rowId;
            var actions = "";';

foreach ($editorActions as $linkType => $value) {
    switch ($linkType) {
        case 'view':
            $js .= "if(data[1] == 1) {
                        actions += \"<a class='actionimage' href='" . $value . " + options.rowId' title='" . $this->translate->_e('View') . "' onClick='load_start();'><img src='/images/icons/16x16/document_view.png' width='16' height='16' alt='' /></a>\";
                    }";
    }

    break;
}

As you can see id is a value coming from Javascript and $value is coming from PHP. The idea is to get an hyperlink as for example:

$value = "/route/to/function/";
id = 19009; // I've omitted the $ sign since this var is coming from JS

var href = "' . $value . '" + id;'

Then I need to use the href var as part of the <a> element shown right after the definition.

With my code above I am getting this error:

Uncaught SyntaxError: Invalid or unexpected token

Can I get some help to get this right?

UPDATE:

This is how the code looks like after I render the page:

$(function () {
    $.extend($.fn.fmatter, {
        userActions: function (cellvalue, options, rowdata) {
            var data = cellvalue.split('|');
            var id = options.rowId;
            var actions = '';

            console.log(id);
            if (data[1] == 1) {
                actions += "<a class='actionimage' href='/sf/distributor/show/ + options.rowId' title='View' onClick='load_start();'><img src='/images/icons/16x16/document_view.png' width='16' height='16' alt='' /></a>";
            }

            return actions;
        }
    });
});

Notice how the function $.extend close properly. console.log(id) did print the value of options.rowId however this value doesn't have any effect on the hyperlink as you may notice this is the value /sf/distributor/show/ + options.rowId.

What is coming in $value is a plain string in the case above /sf/distributor/show/.

15
  • @Carcigenicate this will be rendered as a Javascript code on the view aftewards so there id will have sense Commented May 31, 2017 at 14:33
  • 5
    never mix php with js - it's bad practice and should be avoided at all costs. E.g. you can add data tags to dom elements to get your php data and assign it to a js var to use.. Commented May 31, 2017 at 14:34
  • 1
    On which line you are getting Uncaught SyntaxError Commented May 31, 2017 at 14:34
  • @ThisGuyHasTwoThumbs I wish not need to do that but I come latest to this company and them already had this develop now is giving a lot of issues and there is not time to refactoring this in the proper way as you said :-( Commented May 31, 2017 at 14:35
  • @ReynierPM aww I hear you, similar situation xD good luck :'( Commented May 31, 2017 at 14:36

2 Answers 2

2

You're missing double quotes and to end and reopen the string around options.rowId and a + in:

actions += \"<a class='actionimage' href='" . $value . " + options.rowId'

It should be:

actions += \"<a class='actionimage' href='" . $value . "\" + options.rowId + \"'
Sign up to request clarification or add additional context in comments.

Comments

1

Here you are missing "" and that way you are adding options.rowId as a string text.

href='/sf/distributor/show/" + options.rowId + "' 

Comments

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.