This forum has moved to a new location and is in read-only mode. Please visit talk.octobercms.com to access the new location.

MDSAI.COM
MDSAI.COM

I need insert a relation record with Ajax into the current model and refressing the partial, it is possible??

mjauvin
mjauvin

It's possible. Can you give more details on what you want to achieve?

Last updated

MDSAI.COM
MDSAI.COM

marc.jauvin24240 said:

It's possible. Can you give more details on what you want to achieve?

Yes, I'm sorry but my English is very bad. I have an order model, which has a relationship of lines, and on the other hand I have a product model, within the order form I load a popup where I select a product and at that time I want to create a new line in the order and refresh the lines. Can you do something like that?

mjauvin
MDSAI.COM
MDSAI.COM
MDSAI.COM

mjauvin said:

Did you check this tutorial?

https://octobercms.com/support/article/ob-21

I have followed the tutorial, but now I have a field with dependson to the lines field and it does not work. How can I update the requested model field when the lines model is updated?

mjauvin
mjauvin

Please show some code so we can help

MDSAI.COM
MDSAI.COM

Controller pedidos

<?php namespace mdsai\Mdshop\Controllers;

use Backend\Classes\Controller;
use BackendMenu;
use Flash;
use Session;

class Pedidos extends Controller
{
    public $implement = [        
        'Backend\Behaviors\ListController',
        'Backend\Behaviors\FormController',        
        'Backend\Behaviors\RelationController'
    ];

    public $listConfig = 'config_list.yaml';
    public $formConfig = 'config_form.yaml';
    public $relationConfig = 'config_relation.yaml';

    protected $lineaFormWidget;

    public function __construct()
    {
        parent::__construct();
        BackendMenu::setContext('mdsai.Mdshop', 'main-menu-mdshop', 'side-menu-mdshop-pedidos');
        $this->addCss("/plugins/mdsai/mdshop/controllers/assets/css/personalizacion.css", "1.0.0");
        $this->addJs("/plugins/mdsai/mdshop/controllers/assets/js/adminpedidos.js", "1.0.0");        

        $this->lineaFormWidget = $this->createLineaFormWidget();
    }    

    protected function createLineaFormWidget($id = null) {
        $config = $this->makeConfig('$/mdsai/mdshop/models/mdlineaspedidos/fields.yaml');
        $config->alias = 'lineasForm';
        $config->arrayName = 'Linea';
        if ($id === null){
            $config->model = new \mdsai\Mdshop\Models\MDLineaspedidos;
        }else{
            $config->model = \Mdsai\Mdshop\Models\MDLineaspedidos::find($id);
        }
        $widget = $this->makeWidget('Backend\Widgets\Form', $config);
        $widget->bindToController();
        return $widget;
    }    

    public function onLoadCreateLineaForm() {
        $this->vars['lineaFormWidget'] = $this->lineaFormWidget;
        $this->vars['idpedido'] = post('idpedido');
        return $this->makePartial('linea_create_form');
    }

    public function onLoadEditLineaForm() {
        $this->vars['lineaFormWidget'] = $this->createLineaFormWidget(post('idlinea'));
        $this->vars['idpedido'] = post('idpedido');
        $this->vars['idlinea'] = post('idlinea');
        return $this->makePartial('linea_create_form');
    }

    public function onEditLinea(){
        $data = $this->lineaFormWidget->getSaveData();
        $recordID = post('idlinea');
        $model = \mdsai\Mdshop\Models\MDLineaspedidos::find($recordID);
        $model->fill($data);
        $model->save();
        return $this->refreshLineasList();
    }   

    public function onDeleteItem(){
        $recordID = post('idlinea');
        $model = \mdsai\Mdshop\Models\MDLineaspedidos::find($recordID);
        $pedido = $this->getPedidosModel();
        $pedido->lineas()->remove($model, post("sessionKey"));
        return $this->refreshLineasList();
    }

    public function onTarifaChange(){
        Session::put('tarifaLineapedido', post("MDPedidos[tarifaclientepedido]"));
    } 

    protected function getPedidosModel(){
        $pedidoID = post('idpedido');
        //$pedido = \mdsai\Mdshop\Models\MDPedidos::find($pedidoID);
        if (null !== $pedidoID){
            $pedido = \mdsai\Mdshop\Models\MDPedidos::find($pedidoID);
        }else{
            $pedido = new \mdsai\Mdshop\Models\MDPedidos();
        }
        return $pedido;
    }  

    protected function refreshLineasList($sessionKey = null){
        if (!$sessionKey){ $sessionKey = $this->lineaFormWidget->getSessionKey(); }
         $lineas = $this->getPedidosModel()->lineas()->withDeferred($sessionKey)->get();
        $this->vars["sessionKey"] = $sessionKey;
        $this->vars['lineas'] = $lineas;
        $this->vars["idpedido"] = post('idpedido');
        return ['#lineaspedido' => $this->makePartial('lineaspedido')];       
    }
}

Last updated

mjauvin
mjauvin

Ok, can you explain a bit more what you're doing and what's not working as expected?

MDSAI.COM
MDSAI.COM

I really appreciate your interest

I have a Pedidos Model and this model has many lineas models

 public $hasMany = [
     'lineas' => [
         '\Mdsai\Mdshop\Models\MDLineaspedidos', 'key' => 'pedido_id', 'otherKey' => 'id'
     ]
 ];

The totals fields of de Pedidos model depends on partial lineas

tabs:
    fields:
        lineas:
            span: full
            cssClass: ''
            path: lineas
            type: partial
            tab: 'Lineas del Pedido'
    totales:
        span: full
        cssClass: ''
        path: totales
        type: partial
        dependsOn:
            - codtransportepedido
            - codfpagopedido
            - porcfinanciacionpedido
            - porcprontopagopedido
            - porcirpfpedido
            - lineas
        tab: Totales

when i insert or update lineas models following the tutorial you say, dependson not work and not refresh de totals fields

Last updated

mjauvin
mjauvin

What does your totales partial look like?

mjauvin
mjauvin

Read this:

https://octobercms.com/docs/backend/forms#field-dependencies

You need to create a filterFields() method that will calculate the totales and return a partial push response

MDSAI.COM
MDSAI.COM

when I change or insert a new line, dependson does not run and therefore the filterfields function does not either

MDSAI.COM
MDSAI.COM

I'm going crazy

mjauvin
mjauvin

Did you add the filterFields() method in your Model? Can you show the code?

mjauvin
mjauvin

I tested this further and you don't need the filterFields() method here... you need the "totales" field being of type partial, and the partial will get called whenever the lineas field changes.

So your partial for totales needs to go through the current lineas and calculate the new total value.

1-16 of 16

You cannot edit posts or make replies: the forum has moved to talk.octobercms.com.