How to check multi select value

How to check multi select value

MatzMatz Posts: 4Questions: 2Answers: 0

I wan't to prevent submit if all selected field is empty
My view like this

1.

2.

3.

I wan't to prevent from submit when this red square on picture is empty

Answers

  • rf1234rf1234 Posts: 2,944Questions: 87Answers: 416

    check the form "on preSubmit" and return false if all the selected rows have empty values:
    https://editor.datatables.net/reference/event/preSubmit

  • MatzMatz Posts: 4Questions: 2Answers: 0
    edited March 2020

    I already find that preSubmit, and here's my code

         editor.on( 'preSubmit', function ( e, o, action ) {
               $.each(o.data, function (key, value) {
                        if(haveError == false){
                            if(o.data[key]['date_from'] == "" || o.data[key]['date_to'] == "" || o.data[key]['reason'] == "" || o.data[key]['remarks'] == ""){
                                  return false;
                            }
                        }
                        filterValue['k'][ctr] = key;
                        ctr = ctr + 1;           
                    });  
        });
    

    I don't want to put all data into loop and check it one by one, I already see this Multi-row editing and I don't know the syntax, what I want is check with out checking it 1 by 1

  • colincolin Posts: 15,237Questions: 1Answers: 2,599

    If you want to check a condition on multiple fields, you're going to have to check them one by one. As rf1234 said, preSubmit will stop the submission if you return false.

    Colin

  • rf1234rf1234 Posts: 2,944Questions: 87Answers: 416

    Of course there is an alternative. I am not 100% sure what you really want to achieve here but if you want to avoid sending empty rows to the server then you could also prevent the selection of these rows. You would then only activate the button when a valid selection has been made. Then you don't use an Editor event but a Data Tables event. Please note that I can't provide you with examples in the outdated Data Tables syntax that you are using because I don't know it.

    table
        .on('select', function (e, dt, type, indexes) {
            if ( ! aCertainPage ) {
                var el = dt.row({selected: true}).data().fixed.element;
                if ( ( isA(el,'loan') || isA(el,'swapPayable') || isA(el,'deposit')) &&
                     ( isA(el,'realRepayment')                                     )    ) {
                    dt.buttons('.editorOnly').nodes().removeClass('hidden');       
                } else {
                    dt.buttons('.editorOnly').nodes().addClass('hidden');
                }
            } else {
                dt.row({selected: true}).deselect();
            }
        })
    

    This is for single select. You would need to do the checking in a loop over all rows selected. Like this for example using the api:

    table.rows().every( function ( rowIdx, tableLoop, rowLoop ) {
        var data = this.data();
        if ( data.yourField <= '' ) {
            table.row(this).deselect();
        }
    } );
    
This discussion has been closed.