更新类别

时间:2014-06-09 09:39:16

标签: forms laravel

我非常接近解决方案,但我被困住了。我有一个类别页面,在同一页面上创建和删除类别。我正在添加一个编辑功能,编辑链接,并将其添加到用户可以在同一页面上编辑类别的位置,而不是创建另一个静态页面并重复相同的代码。

这是我的更新控制器功能

public function postEdit() {
    $category = Category::find(Input::get('id'));

    if ($category) {
        $category->update();
        return Redirect::to('admin/categories/index')
        ->with('message', 'Category Updated');
    }

    return Redirect::to('admin/categories/index')
        ->with('message', 'Something went wrong, please try again');
}

以下是用户可以查看,创建,编辑和删除列出的类别的类别页面我尝试使用条件表示将创建表单显示为默认值,并且仅在单击编辑按钮时显示编辑表单。< / p>

@extends('layouts.main')

@section('content')

<div id="admin">

    <h1>Categories Admin Panel</h1><hr>

    <p>Here you can view, delete, create, and edit new categories.</p>

    <h2>Categories</h2><hr>

    <ul>
        @foreach($categories as $category)
            <li>
                {{ $category->name }} - 
                {{ Form::open(array('url'=>'admin/categories/destroy', 'class'=>'form-inline')) }}
                {{ Form::hidden('id', $category->id) }}
                {{ Form::submit('delete') }}
                {{ Form::close() }}

                {{ Form::open(array('url'=>'admin/categories/edit', 'class'=>'form-inline'))}}
                {{ Form::hidden('id', $category->id) }}
                {{ Form::submit('edit') }}
                {{ Form::close() }}
            </li>
        @endforeach
    </ul>

    <h2>Create New Category</h2><hr>

    @if($errors->has())
    <div id="form-errors">
        <p>The following errors have occurred:</p>

        <ul>
            @foreach($errors->all() as $error)
                <li>{{ $error }}</li>
            @endforeach
        </ul>
    </div><!-- end form-errors -->
    @endif
    <php
    if(isset(url=>'admin/categories/destroy')) {
        {{ Form::open(array('url'=>'admin/categories/create')) }}
        <p>
            {{ Form::label('name') }}
            {{ Form::text('name') }}
        </p>
        {{ Form::submit('Create Category', array('class'=>'secondary-cart-btn')) }}
        {{ Form::close() }}

    }else{

        {{ Form::open(array('url'=>'admin/categories/edit')) }}
        <p>
            {{ Form::label('name') }}
            {{ Form::text('name') }}
        </p>
        {{ Form::submit('Create Category', array('class'=>'secondary-cart-btn')) }}
        {{ Form::close() }}

    }
?>

</div><!-- end admin -->

@stop

请举例说明如何在同一个视图页面上完成此操作。我知道我不得不传递一个变量作为填写所选类别的表单中的值。 谢谢你的帮助。

顺便说一句,如果您是主持人并且关闭我的问题,我将阻止我的IP地址,在网络中丢弃一百万个IP地址,并且只是使用临时电子邮件一次又一次地注册。

我尝试了一个javascript onclick事件,这是我的结果。我无法让它发挥作用。

链接按钮

 {{ HTML::Link('#', 'edit', array('class'=>'form-inline', 'id'=>'button',      'onclick'=>'showEditForm(this)'))}}

的Javascript

<script>
function showEditForm(id) { 
id.innerHTML="


{{ Form::open(array('url'=>'admin/categories/edit')) }}
    <p>
        {{ Form::hidden('id', $category->id) }}
        {{ Form::label('name') }}
        {{ Form::text('name') }}
    </p>
    {{ Form::submit('Edit Category', array('class'=>'secondary-cart-btn')) }}
    {{ Form::close() }}

";

}
</script>

1 个答案:

答案 0 :(得分:0)

实际上你的问题非常好,因为咆哮我给了它一个-1。尽管如此,这个问题确实值得回答。

让我们总结一下你的问题。你有3个主要任务。

  • 创建
  • 修改
  • 删除

适用于创建

很容易。使用2列布局。在侧栏上,显示创建类别的表单。然后使用ajax提交。 e.g。

$(function(){
    $(document).on('submit','#writeCategory', function(e){

                var f = $(this);
                e.preventDefault();
                var d = f.serialize();

                $.ajax({
                    type        :       POST,
                    url         :       //your url,
                    data        :       d
                }).done(function(data){
                    alert('submitted');
                    //you can also prepend/append the submitted data after successful submission
                }).fail(function(x,h,r){
                    alert('error occured')
                });                
    });
});


编辑:

进行编辑,您可以使用Xeditable。该网页包含该网站中的所有演示文档。


删除:

我提到了一种非常简化的方法。使用data-属性。如果你想删除一个类别,可能你只需要id。

假设您正在循环以显示所有类别。这样做时,将id存储在say data-id属性中。并为每个类别写一个删除链接。

e.g。单个类别实体的删除链接可能如下所示:

<a href="#" class="delete" data-id={{$i->id}}>delete</a>

休息很容易。只需使用ajax并删除链接。 e.g。

$(function(){
        $('.delete').click(function(e){
            e.preventDefault();
            var a = $(this);            
            var answer = confirm('Are you sure you want to delete this?');
            if (answer)
            {
                
                    $.ajax({
                        type        :     'post',
                        url         :       //your url,
                        data        :       {id:a.attr('data-id')}  
                    }).done(function(data){
                        alert('deleted');
                    }).fail(function(x,h,r){
                        alert('error occured');
                    });
            }

        });
    });

你也可以修改它。例如删除后,如果以表格方式显示类别,则可以向上滑动该行(包含指定的catgory信息),暗示该类别已被删除。

加成

视图:

有大量的javascript模型窗口插件。只需选择一个并通过ajax,显示详细信息。

所有4项任务,在一个地方完成。

N.B。这里我使用 jQuery 只是为了方便。如果你想要普通的javascript,你也可以这样写....