Link to cakephp within jquery?

Asked

Viewed 87 times

1

How to create a link using Cakephp conventions within Jquery ?

I’m trying like this but I still can’t make it work.

$(document).ready(function() {
    $('#dataTables-example').DataTable({
        "processing": true,
        "serverSide": true,
        "ajax":{
            url: "<?php echo $this->Html->url("/Empresas/indexAjax.json");?>",
            dataSrc:""
        },
        "columns": [
            {"data": "Empresa.id"},
            {"data": "Empresa.nomeFantasia"},
            {"data": "Empresa.cnpj"},
            {"data": "Empresa.telefone1"},
            {"data": "Empresa.telefone2"},
            {"data": "Empresa.celular"},
            {"data": "Empresa.aberto"},
            {"data":null,
                "bSortable": false,
                "render": function(obj) {
                    return "<?php echo $this->Html->link('<i class="glyphicon glyphicon-eye-open"></i>',
                                                        array('action' => 'view', obj["Empresa"].id), 
                                                        array('title'=>'view', 'escape' => false)); ?>";

                    //return '<a href=/Produto/Detalhar/' + o["Empresa"].id + '>' + 'More' + '</a>';
                }
            }
        ]
    });   
});

1 answer

1


Good afternoon,

to use cakephp url paths in javascript is simple, use only path :-)

 url: "/Empresas/indexAjax.json"

because who will interpret the route to the javascript request is cakephp!

in a separate script from .js you can’t execute php code so it’s not working as desired, if you’re ever using php code inside javascript you need to have javascript in a document. html or . php EX:

<!DOCTYPE>
<html>
<head>
<script src="/js/jquery.js"></script>
<script>
   $(document).ready(function(){
      alert("o dobro de 5 é <?php echo '10'?>");
   }):
</script>

</head>
<!-- resto do html imaginário -->

I hope I helped. hug

Browser other questions tagged

You are not signed in. Login or sign up in order to post.