简体   繁体   English

Cakephp 2.X具有嵌入式按钮

[英]Cakephp 2.X Having Buttons Inline

I'm following the blog tutorial featured on cakephp website: https://book.cakephp.org/2.0/en/tutorials-and-examples/blog/part-two.html 我正在关注cakephp网站上的博客教程https : //book.cakephp.org/2.0/en/tutorials-and-examples/blog/part-two.html

I am trying to add a Cancel button from the edit page and want it inline to the save button of the form however when I add cancel inside the form the Form->end() function in cake submits my cancel as a save, so alternatively I have tried adding the Cancel button after the Form->end(), placing it just before the closing div however now the buttons are now stacked on top of one another. 我正在尝试从编辑页面添加一个“ 取消”按钮,并希望将其内联到表单的“保存”按钮,但是当我在表单内添加“取消”时,cake中的Form-> end()函数将我的取消提交为保存,因此我试过在Form-> end()之后添加Cancel按钮,将其放置在结束div之前,但是现在按钮现在彼此堆叠了。 Is there a way to have a redirecting cancel within the form without it submitting an edited change? 有没有一种方法可以在表单内取消重定向而无需提交已编辑的更改?

Code is as follows: 代码如下:

<div class="container">
    <h1>Edit Post</h1>
    <?php
        echo $this->Form->create('Post');
        echo $this->Form->input('article_title', array('type' => 'text','maxlength' =>'100', 'id'=>'ArticleHeader',"placeholder"=>"Article Header (100 char)", 'class'=>'centertext'));
        echo $this->Form->input('article_link', array( 'type' => 'url','maxlength' =>'200', 'id'=>'ArticleLink',"placeholder"=>"Article Link (200 char)", 'class'=>'centertext'));
        echo $this->Form->input('id', array('type' => 'hidden'));

        echo $this->Form->button('Save', array('type' => 'submit', 'class'=>'button disabled', 'id'=>'SaveEdit'), array('inline' => true));
        echo "\r\n";?>
        <button class='button' onclick="window.location.href='<?php echo Router::url(array('controller'=>'Posts',
                   'action'=>'index' ),array('inline' => true))?>'">Cancel</button>
    <?php
        echo $this->Form->end();
    ?>
</div>

The CakePHP FormHelper wraps inputs and the corresponding labels in divs. CakePHP FormHelper将输入和相应的标签包装在div中。 For this reason you'll not get inlined elements. 因此,您不会获得内联元素。 If you want to use the Formhelper to create your inputs you have to disable the div by 'div' => false as a input option and wrap the cancel/submit inputs by hand. 如果要使用Formhelper创建输入,则必须通过'div' => false禁用div作为输入选项,并手动包装取消/提交输入。

If you simply want to reset the form and stay on the page you can use 'type' => 'reset' for an input. 如果您只想重置表单并停留在页面上,则可以使用'type' => 'reset'作为输入。 If you want to return to the index page 如果要返回索引页面

<div class="submit">
    <?php
    // Reset
    echo $this->Form->input('Reset', array(
        'label' => false,
        'type' => 'reset',
        'div' => false
    ));

    // Cancel
    echo $this->Html->link(
        __('Cancel'),
        Router::url(array('controller' => 'Posts', 'action' => 'index')),
        array('class' => 'button')
    );

    // Submit and closing Form Tag
    echo $this->Form->end(array(
        'label' => __('Submit'),
        'div' => false
    ));
    ?>
</div>

Since the Button Tag itself has no href attribute , you have to use a link for this. 由于按钮标签本身没有href 属性 ,因此您必须使用链接。 Let this link point to your index action and assign 'class' => 'button' to it. 让此链接指向您的索引操作并为其分配'class' => 'button' In your CSS you can style it like the submit button like that (I took it from cake.generic.css): 在您的CSS中,您可以像这样的提交按钮设置样式(我从cake.generic.css中获取它):

input[type=submit], input[type=reset], .button {
    display: inline;
    font-size: 110%;
    width: auto;
}

form .submit input[type=reset], .button {
    background:#FFDACC;
    background-image: -webkit-gradient(linear, left top, left bottom, from(#FFDACC), to(#9E2424));
    background-image: -webkit-linear-gradient(top, #FFDACC, #9E2424);
    background-image: -moz-linear-gradient(top, #FFDACC, #9E2424);
    border-color: #2d6324;
    color: #fff;
    text-shadow: rgba(0, 0, 0, 0.5) 0 -1px 0;
    padding: 8px 10px;
}
form .submit input[type=reset]:hover, .button {
    background: #9E2424;
}

See the CakePHP HtmlHelper and FormHelper for more Options. 有关更多选项,请参见CakePHP HtmlHelperFormHelper

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM