Field->options(), primary key and alias

Field->options(), primary key and alias

milan.kratochvilmilan.kratochvil Posts: 5Questions: 3Answers: 0

Hello,
Using Field with a column, which is part of a compound primary key, I need to use an alias, e.g.:
Field::inst( 'a', 'b.c' )
Then, on client side I have to use

fields: [
  {
    data: 'b.c',
    name: 'a'
  }
]

( Using just name: 'b.c' I get an error: "Primary key element is not available in data set." submitting the form)

Adding the Field->options(), the options in the server response JSON are included as
options: {b.c: [{label: ..., value: ...}, {label: ..., value: ...},…]} (no dotted object notation, 'b.c' is the attribute name...)

Unfortunately, the client side is awaiting the options according to name, i.e. as
options: {a: ...}

So, opening the editor form, the select is empty...

Any idea?

Answers

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin

    The second argument to Field::inst() is both the HTTP parameter name (read on form submission) and the JSON parameter name. The client-side shouldn't see the a at all in the above case.

    Can you show me your full Editor initialisation please?

    Allan

  • milan.kratochvilmilan.kratochvil Posts: 5Questions: 3Answers: 0

    server side:

    $table = "s.p";
    $primary = array("system", ..., "pre_id");
    $editor = Editor::inst( $db, $table, $primary )
      ->fields(
          ...
          Field::inst( $table . ".pre_id", "pre.id" )
            ->options( Options::inst()
              ->table( "c.pre" )
              ->value( "pre_id" )
              ->label( "pre_text" )
            ),
          ...
    

    client side:

    var pEditor = new $.fn.dataTable.Editor( {
      ajax: "/api/s/p",
      fields: [
        ...
        {
          data: "pre.id",
          label: "pre",
          name: "s.p.pre_id",
          type: "select",
        },
        ...
      ], 
      table: "#pTable",
    } );
    
This discussion has been closed.