vendredi 10 février 2017

Error to delete object through an open dialog

I'm working with laravel 5.2 and angular material in a basic crud, I have a problem removing a selected item through an open dialog. Being more explicit, I have a view calling "read_item_form.component.html" in which I show the objects list and each one has the delete button, when clicking on this it shows an open dialog and in the actions of the dialog when pressing delete you must delete the registry, I tried my Code by calling the delete function directly and it works correctly. In the developer tools console I do not have any errors, however when I press delete I notice that you are not getting the id of the object that you want to destroy.

This is my code hope you can help me.

read_item_form.component.html

<md-list flex>
<md-toolbar md-scroll-shrink>
    <div class="md-toolbar-tools" flex="120">
    <p>
        <span>Item List</span>
     </p>

     <md-button class="md-raised md-large" ng-click="vm.add()" >Create</md-button>

    </div>
<md-list-item style="padding:0px 0px;">
    <div class="head-div" flex="15" layout="row" layout-align="center center">
        <p> ID </p>
    </div>
    <div class="head-div" flex="30" layout="row" layout-align="center center">
        <p> Title</p>
    </div>
    <div class="head-div" flex="40" layout="row" layout-align="center center">
        <p> Description </p>
    </div>

    <div class="head-div" flex="15" layout="row" layout-align="center center">
        <p> Options </p>
    </div>        
    <md-divider ng-if="!$last"></md-divider>
</md-list-item>
<md-list-item ng-repeat="item in vm.items" ng-if="$first" style="padding:0px 0px;">
    <div class="first-div-l" flex="15" layout="row" layout-align="center center">
        <p> </p>
    </div>
    <div class="first-div-r" flex="30" layout="row" layout-align="center center">
        <p> </p>
    </div>
    <div class="first-div-r" flex="40" layout="row" layout-align="center center">
        <p> </p>
    </div>
    <div class="first-div-r" flex="15" layout="row" layout-align="center center" layout-fill>
        <md-button  ng-click="vm.readItemID_update(item.id)">Edit</md-button>

        <md-button class="md-warn" ng-click="vm.deleteOne(item.id)" aria-label="update">Delete
        </md-button> 
    </div>
    <md-divider ng-if="!$last"></md-divider>        
</md-list-item>
<md-list-item ng-repeat="item in vm.items" ng-if="!$first" style="padding:0px 0px;">
    <div class="styled-div-l" flex="15" layout="row" layout-align="center center">
        <p> </p>
    </div>
    <div class="styled-div-r" flex="30" layout="row" layout-align="center center">
        <p> </p>
    </div>
    <div class="styled-div-r" flex="40" layout="row" layout-align="center center">
        <p> </p>
    </div>        
    <div class="styled-div-r" flex="15" layout="row" layout-align="center center" layout-fill>

        <md-button  ng-click="vm.readItemID_update(item.id)">Edit</md-button>

        <md-button class="md-warn" ng-click="vm.deleteOne(item.id)" aria-label="update">Delete
        </md-button> 
    </div> 
    <md-divider ng-if="!$last"></md-divider>        
</md-list-item>

read_item_form.component.js

import {CreateItemController} from '../../../dialogs/create-item/create-item.dialog.js';
import {DeleteItemController} from '../../../dialogs/delete-item/delete-item.dialog.js';
class ReadItemFormController{
constructor(API,ToastService,$state, DialogService){
    'ngInject';
    this.API=API;
    this.ToastService=ToastService;
    this.$state=$state;
    this.DialogService=DialogService;

    this.items=[];
    this.id='';

}

$onInit(){
    this.API.all('read-items').get('').then((response)=>{
        this.items = response.data.items;
    });

}
readItemID_update(id){
    this.id = id;
    this.$state.go('app.update-item',{ id: this.id });
}

/*readItemID_delete(id){
    this.id = id;
    this.API.all('delete-items').remove({id: this.id}).then(() => {
        this.ToastService.show('Item eliminado');
        this.$state.reload();
    });
}*/

 add() {
    let options = {
        controller: CreateItemController,
        controllerAs: 'vm'
    }
    this.DialogService.fromTemplate('create-item', options);
}

deleteOne(){
       let options = {
        controller: DeleteItemController,
        controllerAs: 'vm'
    }
    this.DialogService.fromTemplate('delete-item', options);          

}


}

export const ReadItemFormComponent = {
templateUrl: './views/app/components/read_item_form/read_item_form.component.html',
controller: ReadItemFormController,
controllerAs: 'vm',
bindings: {}
}

delete-item.dialog.js

export class DeleteItemController{
constructor(API, ToastService, $state, DialogService){
    'ngInject';
    this.API=API;
    this.ToastService=ToastService;
    this.$state=$state;
    this.DialogService=DialogService;

    this.items=[];
    this.id='';

}


delete(id){

    this.id = id;
        this.API.all('delete-items').remove({id: this.id}).then(() => {
        this.ToastService.show('Item eliminado');
        this.$state.reload();
});
        this.DialogService.hide();
}  

cancel(){
    this.DialogService.cancel();
    }  

}

delete-item.dialog.html

<md-dialog>
<form ng-submit="vm.delete()">

    <md-toolbar>
      <div class="md-toolbar-tools">
        <h2>Delete-item</h2>
      </div>
    </md-toolbar>

    <md-dialog-content>
        <div class="md-dialog-content">
            <p>
               Item will be deleted permanently!

            </p>
         </div>
    </md-dialog-content>

    <md-dialog-actions>
        <md-button type="button" ng-click="vm.cancel()">Cancel</md-button>
        <!--<md-button class="md-primary md-raised" type="submit">Delete</md-button>-->
        <md-button class="md-primary md-raised" ng-click="vm.delete(item.id)" >Delete</md-button>
    </md-dialog-actions>
</form>

ItemController.php

<?php

namespace App\Http\Controllers;

use App\Item;

use Illuminate\Http\Request;

use App\Http\Requests;

class ItemController extends Controller
{
public function create(Request $request)
{
    $this->validate($request,[
        'title' => 'required|string',
        'description' => 'required|string',
    ]);

    $item = new Item;
    $item->title       = $request->input('title');
    $item->description = $request->input('description');
    $item->save();

    return response()->success(compact('item'));
}

public function read(){
    $items=Item::get();
    return response()->success(['items'=>$items]);
}

public function readOne($id){
    $item=Item::find($id);
    return response()->success(['item'=>$item]);
}

public function update(Request $request){
    $this->validate($request,[
        'title' => 'required|string',
        'description' => 'required|string',
    ]);        
    $id = $request->input('id');
    $item=Item::find($id);
    $item->title       = $request->input('title');
    $item->description = $request->input('description');
    $item->save();        
    return response()->success(compact('item'));
}  


    public function delete(Request $request)
{
    $id = $request->input('id');
    $item=Item::find($id);
    if($item!=''){
        $item->delete();
        return response()->success(['var'=> true]);
    }
    else{
        return response()->success(['var'=> false]);
    }
}

public function deleteOne($id)
{
    $item=Item::find($id);
    $item->delete($id);
    //return response()->success(['item'=>$item]);
    return response()->success(compact('item'));

    }


}



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/2kYHMBw
via IFTTT

Aucun commentaire:

Enregistrer un commentaire