Node.js数据表编辑器-根据特定值过滤选择选项

时间:2019-01-02 20:02:12

标签: javascript node.js datatables jquery-datatables-editor

我正在使用node.js数据表编辑器库。尝试根据表'portfolios_isin'中的特定字段(user_id)过滤选择选项时,如下所示:

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' );

我遇到此错误:

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)

根据文档https://editor.datatables.net/manual/nodejs/joins,这是这样做的方法。我有一个类似的项目,在这里我使用php datatable编辑器库(而且一切都很好用)...在那里我可以过滤这样的选项:

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' )    

如何在编辑器中为node.js过滤选项值?

1 个答案:

答案 0 :(得分:1)

this.where超出了函数where所在的范围,因为您正在使用将this绑定到外部/父范围的箭头函数。

尝试以下方法:

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