Quantcast
Channel: Recent Discussions — DataTables forums
Viewing all articles
Browse latest Browse all 82357

Parent / child editing in child rows for NodeJS

$
0
0

Hi all,

Could someone please help me to convert PHP into JavaScript (NodeJS)?
It is the server side script from this blog:

Parent / child editing in child rows
https://datatables.net/blog/2019-01-11#Server-side-(PHP)

PHP:

//Parent table
Editor::inst( $db, 'sites' )
    ->fields(
        Field::inst( 'id' )->set( false ),
        Field::inst( 'name' )->validator( 'Validate::notEmpty' )
    )
    ->join(
        Mjoin::inst( 'users' )
            ->link( 'sites.id', 'users.site' )
            ->fields(
                Field::inst( 'id' )
            )
    )
    ->process( $_POST )
    ->json();


//Child table
if ( ! isset($_POST['site']) || ! is_numeric($_POST['site']) ) {
  echo json_encode( [ "data" => [] ] );
}
else {
  Editor::inst( $db, 'users' )
      ->field(
          Field::inst( 'users.first_name' ),
          Field::inst( 'users.last_name' ),
          Field::inst( 'users.phone' ),
          Field::inst( 'users.site' )
              ->options( 'sites', 'id', 'name' )
              ->validator( 'Validate::dbValues' ),
          Field::inst( 'sites.name' )
      )
      ->leftJoin( 'sites', 'sites.id', '=', 'users.site' )
      ->where( 'site', $_POST['site'] )
      ->process($_POST)
      ->json();
}

This is what I have done so far:

"use strict";

let db = require("./db");
let router = require("express").Router();

const {
  Editor,
  Field,
  Validate,
  Format,
  Options,
  Mjoin
} = require("datatables.net-editor-server");

router.all("/api/sites", async function(req, res) {
  // Parent table
  const editor = new Editor(db, "sites")
    .debug(true)
    .fields(
      new Field("id").set(false),
      new Field("name").validator(Validate.notEmpty())
    )
    .join(
      new Mjoin("users")
        .link('sites.id', 'users.site')
        .fields(
          new Field("id")
        )
    );

// Child table
if (condition) {

} else {

  const editor = new Editor(db, "users")
    .debug(true)
    .fields(
      new Field("users.first_name"),
      new Field("users.last_name"),
      new Field("users.phone"),
      new Field("users.site")
      .options('sites', 'id', 'name')
      .validator(Validate.dbValues()),
      new Field("sites.name")
    )
    .leftJoin( 'sites', 'sites.id', '=', 'users.site' )
    .where('site', $_POST['site']);
    }
    await editor.process(req.body);
    res.json(editor.data());
});

I have no idea how to convert the if condition...


Viewing all articles
Browse latest Browse all 82357

Trending Articles