The value of the select is edit and the datatable take the first value of the select

The value of the select is edit and the datatable take the first value of the select

rshunrshun Posts: 44Questions: 9Answers: 0

Hi,

MV is a checkbox field, Unit is select dropdown field. When MV change, it sends

{"Unit":"ABC","MV":1} to server.

It seems editor post first value of select even if id does not change.

https://datatables.net/forums/discussion/comment/132492/#Comment_132492

I try .edit($(this).closest("td"), false, { submit: 'changed' } ) to try to stop sending Unit. It still does not working.

Please help me.

Rick

Replies

  • rshunrshun Posts: 44Questions: 9Answers: 0

    Is it possible to remove "Unit"
    in
    editor
    .edit($(this).closest('tr'), false, { submit: 'changed' })
    .set('MV', $(this).prop('checked') ? 1 : 0)
    .submit();
    ?

  • rshunrshun Posts: 44Questions: 9Answers: 0
    edited March 2021

    I try to remove Unit before submit it back server

                var changes = editor.edit($(this).closest('tr'), false, { submit: 'changed' });
    
                // if Unit in changes, then clear it
                if (changes.val('Unit').length != 0)
                {
                    changes.clear('Unit');
                }
    
                changes.set('MV', $(this).prop('checked') ? 1 : 0);
    
                changes.submit();
    

    I got javascript error: Uncaught Unknown field name - Unit

    I need a way to check Unit in changes or not.

    Please help me.

  • allanallan Posts: 63,516Questions: 1Answers: 10,472 Site admin

    Hi rshun,

    Can you give me a link to your page showing the issue so I can take a look and help to debug this please?

    Thanks,
    Allan

  • rshunrshun Posts: 44Questions: 9Answers: 0

    That is internal website.

    I try

    var changes = editor.edit($(this).closest('tr'), false, { submit: 'changed' });
    
    if (changes.includes('Unit'))
    {
        changes.clear('Unit');
    }
    
    changes.set('MV', $(this).prop('checked') ? 1 : 0);
    changes.submit();
    

    I got javascript error: Uncaught Unknown field name - Unit

    Anyway, I need a way to remove Unit from changes.

  • rshunrshun Posts: 44Questions: 9Answers: 0

    I also try

    changes.field('Unit').multiRestore();

    Is it possible to do single field restore?

  • allanallan Posts: 63,516Questions: 1Answers: 10,472 Site admin

    changes.clear('Unit');

    Removes the Unit field! From the clear() docs:

    Remove one or more fields from the form.

    You perhaps want to use changes.field('Unit').val(''); here?

    Allan

  • rshunrshun Posts: 44Questions: 9Answers: 0
    edited March 2021
    var changes = editor.edit($(this).closest('tr'), false, { submit: 'changed' });
                changes.clear();
                changes.set('MV', $(this).prop('checked') ? 1 : 0);
                changes.submit();
    

    Now I got javascript error: Uncaught Unknown field name - MV

    I think I need a way to add back MV field.

    Rick

  • rshunrshun Posts: 44Questions: 9Answers: 0
    edited March 2021

    I try

    var changes = editor.edit($(this).closest('tr'), false, { submit: 'changed' });
                var mvField = changes.field('MV');
                changes.clear();
                changes.add(mvField);
                changes.set('MV', $(this).prop('checked') ? 1 : 0);
                changes.submit();
    

    I got javascript error: Uncaught TypeError: Cannot read property 'opts' of undefined
    at name

    How can I put MV field back to changes?

    Thanks.

  • allanallan Posts: 63,516Questions: 1Answers: 10,472 Site admin

    changes.clear() will remove all fields. I'm not sure why you would want to remove any fields?

    If you aren't able to give me a link to your page, could you modify this example to demonstrate the issue you are originally seeing, so I can debug it please?

    Allan

  • rshunrshun Posts: 44Questions: 9Answers: 0
    edited March 2021

    Unit is a dropdown field. Unit depend on Site. Because editor post first value of select. I don't want to have 'Unit' in my changes.

    I figure out that in my MV checkbox change, I can remove Unit before submit. After submit, I add it back. It works.

    $('#table').on('change', 'input.editor-mv', function () {
    
                var changes = editor.edit($(this).closest('tr'), false, { submit: 'changed' });
    
                changes.clear('Unit');
                changes.set('MV', $(this).prop('checked') ? 1 : 0);
                changes.submit();
                
                changes.add({
                    label: "Unit:",
                    name: "Unit",
                    type: "select"
                }, 'Site');
            });
    

    Before:
    {"Unit":"ABC","MV":1}

    After:
    {"MV":1}

  • colincolin Posts: 15,240Questions: 1Answers: 2,599
    edited March 2021

    I appreciate you've got a working solution, but it sounds like you're using it as a placeholder value - there is a placeholder attribute you can use instead (see select).

    Colin

  • rshunrshun Posts: 44Questions: 9Answers: 0

    Thanks, Colin. I will try placeholder later.

This discussion has been closed.