【问题标题】:cakephp turn postLink into buttoncakephp 将 postLink 变成按钮
【发布时间】:2020-08-04 16:21:06
【问题描述】:

我想将删除 postLink 更改为按钮。我尝试了很多组合,但更改为按钮会使表单提交而不是重定向到删除方法。

不确定我还可以尝试什么。

<?= $this->Form->postButton(__('X', ['type' => 'button']), ['controller' => 'StrategiesConditions', 'action' => 'delete', $strategiesConditions['id']], ['confirm' => __('Are you sure you want to delete # {0}?', $strategiesConditions['id'])]) ?>

整体代码

    <div class="strategies form content">
        <?= $this->Form->create($strategy) ?>
        <fieldset>
            <legend><?= __('Edit Strategy') ?></legend>
            <?php
                echo $this->Form->control('user_id', ['options' => $users]);
                echo $this->Form->control('name');
                echo $this->Form->control('description');
                echo $this->Form->control('one_hundred_trades');
            ?>
            <table id="conditions-table">
                <thead>
                    <tr>
                        <th>Name</th>
                        <th>Level</th>
                        <th></th>
                    </tr>
                    <tr>
                        <td>
                            <input id="condtitions-input"></input>
                            <select id="condtitions-level">
                                <option value="Mandatory">Mandatory</option>
                                <option value="Important">Important</option>
                                <option value="Support">Support</option>
                            </select>
                        </td>
                        <td>
                            <button id="add-condtition" type='button' onclick="addCondition()">Add Conditions</button></td>
                        </td>  
                    </tr>
                    <?php foreach ($strategy->strategies_conditions as $key=>$strategiesConditions) : ?>
                    <tr>
                        <td>
                            <?php echo $this->Form->hidden('strategies_conditions.' . $key . '.id'); ?>
                            <?php echo $this->Form->control('strategies_conditions.'.$key.'.name', array( 'label' => false )); ?>
                            <select name="strategies_conditions[<?=$key?>][level]">
                                <option <?= ($strategiesConditions['level'] == "Mandatory") ? 'selected="selected"' : "" ?> value="Mandatory">Mandatory</option>
                                <option <?= ($strategiesConditions['level'] == "Important") ? 'selected="selected"' : ""  ?> value="Important">Important</option>
                                <option <?= ($strategiesConditions['level'] == "Support") ? 'selected="selected"' : ""  ?> value="Support">Support</option>
                            </select>
                        </td>
                        <td>
                            <?= $this->Form->postLink(__('X', ['type' => 'button']), ['controller' => 'StrategiesConditions', 'action' => 'delete', $strategiesConditions['id']], ['confirm' => __('Are you sure you want to delete # {0}?', $strategiesConditions['id'])]) ?>
                        </td>
                    </tr>
                    <?php endforeach; ?>                       
                </thead>
            </table>                
        </fieldset>
        <?= $this->Form->button(__('Submit')) ?>
        <?= $this->Form->end() ?>
    </div>

【问题讨论】:

  • 您想改变它的外观或行为方式?
  • 我想将外观从文本更改为按钮,但仍然具有删除关联记录的行为。就像在 view.php 中一样
  • 刚刚注意到这是您的编辑页面,所以整个内容都包含在一个表单中。虽然 HTML5 有很多方法可以做到这一点,但这不会生成那种代码。您将在这里得到的是无效的 HTML,因此结果是不可预测的;您可能会从不同的浏览器获得不同的行为。无论如何,从这里删除链接不会将您带到不同的页面,从而丢失您所做的任何编辑吗?您可能需要的是某种 Ajax 链接,或者您的控制器处理的“删除此”复选框。
  • 除了 Greg Schmidt 所说的:你不能把你的 postlink 放在$this-&gt;Form-&gt;create()$this-&gt;Form-&gt;end() 之间。
  • 有什么问题? This demo 展示了添加 CSS 类使其显示为按钮是多么容易。

标签: cakephp cakephp-4.x


【解决方案1】:

我使用 Cakephp block 解决了 formform 的问题,我添加了样式来制作按钮。

<?= $this->Form->postLink(__('X'), 
       [
          'controller' => 'StrategiesConditions', 
          'action' => 'delete', 
           $strategiesConditions->id
       ], 
       [
           'block' => true,
           'class' => 'btn btn-primary',
           'confirm' => __('Are you sure you want to delete # {0}?', $strategiesConditions['id'])
           ]) ?>
                        </td>
                    </tr>
                    <?php endforeach; ?>                       
                </thead>
            </table>                
        </fieldset>
<?= $this->Form->button(__('Submit')) ?>
<?= $this->Form->end() ?>
<?=  $this->fetch('postLink'); ?>

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多