javascriptnode.jsdatatablesjquery-datatables-editor

Node.js datatables editor - Filter select option depending on a certain value


I'm using the node.js datatable editor library. When trying to filter the select options depending on a specific field (user_id) in table 'portfolios_isin' like this:

node.js

let editor = new Editor( db, 'portfolios_isin_mm' )
    .fields(
        new Field( 'portfolios_isin_mm.account_id' ),
        new Field( 'portfolios_isin_mm.user_id' ),
        new Field( 'portfolios_isin_mm.uid_foreign' )
            .options(new Options()
                .table('portfolios_isin')
                .value('id')
                .label('portfolio_name')
                .where(() => {  
                    //this.where('user_id', '=', '1')
                    this.where('portfolios_isin.user_id', '=', '1')
                })
            ),
        new Field( 'securities.issuer_name' )
    )

    .leftJoin( 'portfolios_isin', 'portfolios_isin.id', '=', 'portfolios_isin_mm.uid_foreign' );

I'm getting this Error:

TypeError: this.where is not a function
    at Builder.Editor.fields.Field.options.Options.table.value.label.where (/home/project/controllers/myproject.js:359:30)
    at Formatter.compileCallback (/home/project/node_modules/knex/lib/formatter.js:161:14)
    at Formatter.rawOrFn (/home/project/node_modules/knex/lib/formatter.js:104:36)
    at QueryCompiler_MySQL.whereWrapped (/home/project/node_modules/knex/lib/query/compiler.js:531:30)
    at QueryCompiler_MySQL.where (/home/project/node_modules/knex/lib/query/compiler.js:314:32)
    at /home/project/node_modules/knex/lib/query/compiler.js:147:30
    at Array.map (<anonymous>)
    at QueryCompiler_MySQL.select (/home/project/node_modules/knex/lib/query/compiler.js:146:33)
    at QueryCompiler_MySQL.toSQL (/home/project/node_modules/knex/lib/query/compiler.js:108:27)
    at Builder.toSQL (/home/project/node_modules/knex/lib/query/builder.js:115:44)
    at /home/project/node_modules/knex/lib/runner.js:56:32
    at tryCatcher (/home/project/node_modules/bluebird/js/release/util.js:16:23)
    at /home/project/node_modules/bluebird/js/release/using.js:185:26
    at tryCatcher (/home/project/node_modules/bluebird/js/release/util.js:16:23)
    at Promise._settlePromiseFromHandler (/home/project/node_modules/bluebird/js/release/promise.js:512:31)
    at Promise._settlePromise (/home/project/node_modules/bluebird/js/release/promise.js:569:18)

According to the docs https://editor.datatables.net/manual/nodejs/joins this is the way to do it. I have a similar project where I use the php datatable editor libraries (and all is working just fine) ... there I can filter the options like this:

php:

Editor::inst( $db, 'portfolios_isin_mm' )
    ->fields(
        Field::inst( 'portfolios_isin_mm.account_id' ),
        Field::inst( 'portfolios_isin_mm.user_id' ),
        Field::inst( 'portfolios_isin_mm.uid_foreign' )
            ->options( 'portfolios_isin', 'id', 'portfolio_name', function ($q) {
                $q->where( 'portfolios_isin.user_id', '1', '=' );
            }),
        Field::inst( 'securities.issuer_name' )         
    )

    ->leftJoin( 'portfolios_isin', 'portfolios_isin.id', '=', 'portfolios_isin_mm.uid_foreign' )    

How do I get to filter the option values in editor for node.js?


Solution

  • this.where is out of the scope where the function where exist, because you are using an arrow function which binds this to the outer/parent scope.

    Try this instead:

    .options(new Options()
                    .table('portfolios_isin')
                    .value('id')
                    .label('portfolio_name')
                    .where(function(){  
                        this.where('portfolios_isin.user_id', '=', '1')
                    })
                ),