Default Value in Editor

Default Value in Editor

davidjmorindavidjmorin Posts: 101Questions: 31Answers: 0

I need to have the ID from table 1 as a default value for the editor field that will save into table 2. This is what I have but it is not working.

  $(document).ready(function() {
        var editor = new $.fn.dataTable.Editor({
            ajax: 'php/table.query.php',
            table: '#sku_request',
            fields: [{
                    label: "Note:",
                    name: "dpe_notes.note"
                },

                {
                    label: "Your Name:",
                    name: "dpe_notes.user"
                },
                {
                label: "Note ID:",
                name: "dpe_notes.note_id",
                def: function (data, type, full, meta, row) { return data.ID; }
            },




            ]
        });

The Note ID is what I need as the default from the table. The note ID is the ID from table 1.

My full script

(function($) {

    $(document).ready(function() {
        var editor = new $.fn.dataTable.Editor({
            ajax: 'php/table.sku_request.php',
            table: '#sku_request',
            fields: [{
                    label: "Note:",
                    name: "dpe_notes.note"
                },

                {
                    label: "Your Name:",
                    name: "dpe_notes.user"
                },
                {
                label: "Note ID:",
                name: "dpe_notes.note_id",
                def: function (data, type, full, meta, row) { return full.dp_expected.note_id; }
            },




            ]
        });

    $(document).ready( function () {
  var table = $('#sku_request').DataTable({
        ajax: 'php/table.sku_request.php',
        dom: 'Bfrtip',
        select: true,
        pageLength: 10,
        lengthChange: true,
        scrollX: true,

        language: {
                searchPanes: {
                        clearMessage: 'Clear All',
                        collapse: 'Filter',
                        cascadePanes: true,
                        viewTotal: true,
                        layout: 'columns-2',
                        dataLength: true

                }
        },
        buttons: [
                {
                        extend: "remove",
                        text: "Delete",
                        editor: editor
                },

                {
                        extend: "excelHtml5",
                        text: "Excel"
                },
        {
            extend: "edit",
            text: "Update",
            editor: editor
        },


        ],


        columns: [

      {
          data: "dp_expected.ID"
      },
      {
          data: "dp_expected.Invoice_"
      },
      {
          data: "dp_expected.District"
      },
      {
          data: "dp_expected.Sold_By"
      },
      {
        data: null,
        render: function ( data, type, full, meta, row ) {
                // Combine the first and last names into a single table field
              if(full.dpe_notes.note >= 0){
                return 'No Notes';
              }else {

                  return full.dpe_notes.note+' - '+full.dpe_notes.user;
              }

            } },
        ],
  });
} );


    });

}(jQuery));

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 63,523Questions: 1Answers: 10,473 Site admin

    The def function doesn’t have any arguments, so I would expect the above to throw an error about an undefined variable.

    Have a look at how I used a default value in this blog post which it sounds like you are doing something similar to?

    Allan

  • davidjmorindavidjmorin Posts: 101Questions: 31Answers: 0

    @allan thank you for that link. I believe this will help me. So I could just use the following in the def: ?

    function ( d ) {
                var selected = siteTable.row( { selected: true } );
     
                if ( selected.any() ) {
                    d.site = selected.data().id;
                }
            }
    
  • davidjmorindavidjmorin Posts: 101Questions: 31Answers: 0
    edited January 2021

    Yeah so tested and confirm this does not work.

    {
                        label: "Note ID:",
                        name: "dpe_notes.note_id",
                        def: function ( d ) {
                                    var selected = table.row( { selected: true } );
    
                                    if ( selected.any() ) {
                                        d.site = selected.data().id;
                                        return d.site;
                                    }
                                }
                    },
    
  • davidjmorindavidjmorin Posts: 101Questions: 31Answers: 0

    Im lost here. Any guidance is appreciated.

  • allanallan Posts: 63,523Questions: 1Answers: 10,473 Site admin
    Answer ✓

    Can you give me a link to your page so I can help to debug it?

    The first question is going to be - does it go into the if condition you have above? i.e. does selected contain any rows or not? If not, then is table pointing at the parent table? If it does, then is id a valid property in your data structure? Assigning id to d.site I would say is unusual - you could just use return selected.data().id;.

    Allan

This discussion has been closed.