CakePHP, jQuery - add data attribute for binding

I am looking for a way to insert a data attribute into my table bindings in CakePHP. It automatically generates the following code, but I don’t know how to change it so that I become a data attribute.

The reason I want to achieve this is because I am binding to another div on the same page and want to prevent the page from reloading . Should I embed preventDefault()from jQuery? And if so, how to do it?

This is my current code:

<?php foreach ($servers as $server): ?>
  <tr>
     <td><?= $this->Number->format($server->id) ?></td>
     <td><?= h($server->url) ?></td>
     <td><?= h($server->description) ?></td>
     <td><?= h($server->Timestamp) ?></td>
     <td class="actions">
         <?= $this->Html->link('View', array('#' => 'admin-view-' . $server->id)) ?>
         <?= $this->Html->link('Edit', array('#' => 'admin-edit-' . $server->id)) ?>
     </td>
   </tr>
<?php endforeach; ?>

Here is the result I'm looking for; this needs to be done using CakePHP conventions (ignore the list item):

<a data-target="admin-edit">Edit</a>
<a data-target="admin-view">View</a>
+4
source share
2

: -

<?= $this->Html->link('View',array('#' => 'admin-view-' . $server->id,'data-target'=>"admin-view");?>
<?= $this->Html->link('Edit',array('#' => 'admin-view-' . $server->id,'data-target'=>"admin-edit");?>

, href="javascript:void(0);", : -

<?= $this->Html->link('View','javascript:void(0);',array('#' => 'admin-view-' . $server->id,'data-target'=>"admin-view");?>
<?= $this->Html->link('Edit','javascript:void(0);',array('#' => 'admin-view-' . $server->id,'data-target'=>"admin-edit");?>

jQuery ( ), : -

$('link').click(function(e){
   e.preventDefault();
   //rest your code
});

. - , jQuery , .

+3

CakePHP HtmlHelper link

CakePHP link 3 .

  • [String]
  • URL [ ]
  • / []

:

 $this->Html->link($title, $url = null, array $options = []);

:

echo $this->Html->link(
   'Title',
   '/path/to/url', #OR ['controller'=>'','action'=>'','others']
   [
     'class' => 'button',
     'target' => '_blank',
     'data-url'=>'#',
     'data-path'=>'',
     /*Other attributes*/
   ]
);

CakePHP

, href='' <a>, button <a>.

+3

Source: https://habr.com/ru/post/1672440/


All Articles