Editing in Editor where one pulldown depends on another

Editing in Editor where one pulldown depends on another

ChrisLChrisL Posts: 13Questions: 4Answers: 0

Hi,

Here is the controller in question:

let db = require('../db');
let router = require('express').Router();
let {
    Editor,
    Field,
    Validate,
    Format,
    Options
} = require("datatables.net-editor-server");

router.all('/api/service_provided', async function(req, res) {
    if (! req.body.business_id) {
        res.json({data:[], err:"No business_id selected"});
        console.log('service_provided.js -> No business_id selected.');
        return;
    }
    let editor = new Editor(db, 'service_provided')
        .fields(
            new Field("service_provided.id"),
            new Field("service_provided.client_business_id"),
            new Field("service_provided.client_contact_id")
                .options(
                    new Options()
                        .table('contact')
                        .value('id')
                        .label('full_name')
                        .where(function() {
                            this.where("business_id",req.body.business_id)
                        })
                )
                .setFormatter(Format.ifEmpty(null)),
            new Field("service_provided.quote_request_date"),
            new Field("service_provided.quote_sent_to_client_date"),
            new Field("service_provided.supplier_business_id")
                .options(
                    new Options()
                        .table('business')
                        .value('id')
                        .label('business_name')
                        .where(function() {
                            this.where( 'business_relationship_type_id', '1' )
                                .orWhere( 'business_relationship_type_id', '3' );
                        })
                )
                .setFormatter(Format.ifEmpty(null)),
            new Field("service_provided.supplier_contact_id")
                .options(
                    new Options()
                        .table('contact')
                        .value('id')
                        .label('full_name')
                        .where(function() {
                            this.where('contact.business_id', 'service_provided.supplier_business_id')
                        })
                )
                .setFormatter(Format.ifEmpty(null)),
            new Field("service_provided.product_id"),
            new Field("service_provided.service_type_id"),
            new Field("service_provided.service_description"),
            new Field("service_provided.purchase_date"),
            new Field("service_provided.start_date"),
            new Field("service_provided.end_date"),
            new Field("service_provided.vendor_contract_number"),
            new Field("service_provided.vendor_instance_number"),
            new Field("service_provided.job_number"),
            new Field("service_provided.last_price"),
            new Field("service_provided.weeks_prior_notification"),
            new Field("service_provided.note_id"),
            new Field("client_contact.full_name"),
            new Field("supplier_business.business_name"),
            new Field("supplier_contact.full_name"),
            new Field("product.product_description"),
            new Field("service_type.service_description")
        )
        .leftJoin("contact as client_contact","client_contact.id","=","service_provided.client_contact_id")
        .leftJoin("business as supplier_business","supplier_business.id","=","service_provided.supplier_business_id")
        .leftJoin("contact as supplier_contact","supplier_contact.id","=","service_provided.supplier_contact_id")
        .leftJoin("product","product.id","=","service_provided.product_id")
        .leftJoin("service_type","service_type.id","=","service_provided.service_type_id");

    console.log("service_provided.js -> processing request for business_id: "+req.body.business_id);
    editor.where( function () {
        this.where( 'client_business_id', req.body.business_id );
    } );

    await editor.process(req.body);
    res.json(editor.data());
});

module.exports = router;

As you will see at line 53 I'm attempting to limit the values used to populate a "pulldown" according to the value selected in another pulldown on the edit form - with no luck.

Can anyone tell me how to do this correctly please?

Thanks in anticipation.

Chris.

This question has an accepted answers - jump to answer

Answers

  • ChrisLChrisL Posts: 13Questions: 4Answers: 0

    I forgot to say this is the licenced version of Editor being used, but not Editor 2.0 yet.

  • allanallan Posts: 63,230Questions: 1Answers: 10,416 Site admin
    Answer ✓

    Hi Chris,

    The issue here is that the options which can be loaded using the code above are the same for every row. If you want to change the options available based on the row contents, you need to need to use dependent() to get the new options for each row being edited. This post has details on how that can be done.

    Allan

  • ChrisLChrisL Posts: 13Questions: 4Answers: 0

    Thank you Allan!

Sign In or Register to comment.