use data from one column as default for another

use data from one column as default for another

MadMax76MadMax76 Posts: 149Questions: 33Answers: 1
edited March 2021 in Free community support

Hi,

in
http://freigabe.kontura.at/test/posit.html
I am showing this data:

{ data: "pos_netto1" , editField: "Nettoposition", render: $.fn.dataTable.render.number( '.', ',', 2, '' ), className: 'dt-body-right'},    

and in the editor I define this:

{ label: "Netto", name: "Nettoposition", default: "pos_netto1", render: $.fn.dataTable.render.number( '.', ',', 2, '' )},

but the "default" does not work, instead of the value of "pos_netto1" the word itself is copied as default.

How can I change this?
Thanks
Max

Edited by Allan - Syntax highlighting. Details on how to highlight code using markdown can be found in this guide.

This question has an accepted answers - jump to answer

Answers

  • kthorngrenkthorngren Posts: 21,180Questions: 26Answers: 4,923

    You may want to use dependent() for this. See Colin's example in this thread.

    Kevin

  • MadMax76MadMax76 Posts: 149Questions: 33Answers: 1

    HI Kevin,

    I studied those examples and will use "dependend" in future...

    But i think for this case this is not what I need, my problem-descirption was misleading - sorry!

    Trying to make this clear:
    ALready on the sql-server I have a "real" value and a "proposed" value. If there is a real value, those will be the same. If the real value is NULL, then the proposed value is something different. If this is not edited, in future the proposed value will be used.

    To make it more complicated, the real value comes from the table directly, the proposed value from a view with a lot of calculations (which I use as readTable).

    The datatable hat as field definition:

    data: "proposed" , editField: "real", 
    

    For the end-user it will be strange to see a value in the table and when opening the editor, not seeing a value in the same field. Therefore I tried this in the editor-definition:

    { label: "Netto", name: "real", default: editor_pos.field('proposed').value }
    

    but this kills the whole datatable as 'proposed' is not a editor-field...
    So whenever real is null, I want the proposed as default.

    I hope I have been able to make this understandable,
    thanks
    Max

  • allanallan Posts: 63,226Questions: 1Answers: 10,416 Site admin

    Hi Max,

    Yes, default: editor_pos.field('proposed').value wouldn't work, because the value (editor_pos....) is evaluated when that code runs. Whereas you actually want the value to be evaluated when you trigger the create action - that can be done with a function:

    def: function () {
      return editor_pos.field('proposed').val();
    }
    

    However, that isn't actually a good idea to use that approach since it is order dependent. It needs the proposed value to be set first. Instead, I would suggest you use the initCreate event:

    editor.on('initCreate', function () {
      var val = editor_pos.field('proposed').val();
      editor_pos.field('pos_netto1').val(val);
    });
    

    Allan

  • MadMax76MadMax76 Posts: 149Questions: 33Answers: 1

    Hi Allan,

    I changed this slightly to

            editor_pos
                .on('initEdit', function () {
                    var val = editor.field('pos_netto1').val();
                    editor_pos.field('Nettoposition').val('val');
                });
    
    

    however this

    var val = editor.field('INP_ID').val();
    

    does cause an error not permitting any changes; I also tried with a different field but pos_netto1 (as this is not part of the editor), but still does not work. If I leave thsi line away and put any number instead of 'val' in works.

    Thanks!
    Max

  • allanallan Posts: 63,226Questions: 1Answers: 10,416 Site admin

    Ah - looking at the page I see the issue. You don't actually have a pos_netto1 field - so we need to get that value from the selected row in the DataTable:

    editor_pos
        .on('initEdit', function () {
            var val = table.row({selected: true}).data().pos_netto1;
            editor_pos.field('Nettoposition').val(val);
        });
    

    Allan

  • MadMax76MadMax76 Posts: 149Questions: 33Answers: 1
    edited March 2021

    almost there!! Catching the value now works, but: Only when i select the row. Inline editing is "blocked" until I select one row, then I can do an inline-edit in any row - but the values propsed always come from the selected row, which ist an interesting feature but not what it should do...
    I tried table.row('.active')., but that does not work at all.

    Thanks
    Max

  • allanallan Posts: 63,226Questions: 1Answers: 10,416 Site admin
    Answer ✓

    Ah yes, I hadn't considered inline editing. Try this - it's a little simpler anyway:

    editor_pos
        .on('initEdit', function (e, node, data, items, type) {
            editor_pos.field('Nettoposition').val(data.pos_netto1);
        });
    

    Allan

  • MadMax76MadMax76 Posts: 149Questions: 33Answers: 1

    THANKS!!!!!!

This discussion has been closed.