Select - display name, and store value:

Select - display name, and store value:

panzrampanzram Posts: 29Questions: 11Answers: 0
edited August 2015 in Editor

Hi,

I have the following editor array:

[["NameA",1],["NameB", 2]]

Created by a loader function that I use in the editor like:

  var editor;
  $(document).ready(function() {
    editor = new $.fn.dataTable.Editor({
      ajax: "server_data.php",
      table: "#example1",
      fields: [{
        label: "ObjectName",
        name: "ObjectName",
        type: "select",
        ipOpts: loader()
        }
      }]
    });

The server data file is:

Editor::inst( $db, 'mytable', 'id' )
    ->fields(
        Field::inst( 'ObjectName' )
    )
    ->process( $_POST )
    ->json();

I think it ouputs the label and not the value somehow. I must store the id (ie ObjectId), instead of the Name.

Can I use setValue and somehow get the post array and reference the 2nd item? ...ex:

Editor::inst( $db, 'mytable', 'id' )
    ->fields(
        Field::inst( 'ObjectName' )
          ->setValue( $_POST['ObjectName'][2] )
    )
    ->process( $_POST )
    ->json();

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 61,840Questions: 1Answers: 10,134 Site admin

    Hi,

    Are you able to modify loader() to return an object for each item in the list (rather than array) with label and value properties? If you can't another option is to put loader() into a closure function that would do that formatting for you. That then will ensure that Editor uses the correct elements for the aspects you expect.

    Regards,
    Allan

  • panzrampanzram Posts: 29Questions: 11Answers: 0

    Hi,

    First off, thanks for an outstanding product.

    My loader function does already output label and value (I think):

    var test= new Array({"label" : "a", "value" : "a"});
      function loader() {
        test.splice(0,1);
        $.ajax({
            url: 'get_object.php',
            async: false,
            dataType: 'json',
            success: function (json) {
                for(var a=0;a<json.length;a++) {
                    obj= { "label" : json[a][0], "value" : json[a][1] };
                    test.push(obj);
                }
            }
        });
        return test;
    }
    

    My get_object.php outputs the arrays:

    [["NameA",1],["NameB",2]]

    I think I've followed the cookbook, but I'm not sure...

  • allanallan Posts: 61,840Questions: 1Answers: 10,134 Site admin
    Answer ✓

    Thanks for the extra info. So if you inspect the select element when it is visible, does it have the options and tables that you would expect? The value attribute from each <option> is what will be submitted to the server and what Editor will attempt to write to the database.

    Allan

This discussion has been closed.