Count non empty cells in a row

Count non empty cells in a row

wijwijwijwij Posts: 54Questions: 12Answers: 0

Hi,

I wonder if someone can help me:

When I select a row to edit it and hit "Update", I want to count **the **none empty cells in this row and update another cell value in the DT accordingly.
Also, I want this cell value to get updated automatically when the DT initialized

Any help is appreciated

Answers

  • kthorngrenkthorngren Posts: 21,443Questions: 26Answers: 4,974
    Answer ✓

    One option might be to use the postEdit for this. You can iterate the data parameter to count the none empty cells. You can use the id parameter as the row-selector for row().data() to update the cell with row().data().

    Also, I want this cell value to get updated automatically when the DT initialized

    Use rows().every() to loop all the rows in initComplete. You can count the none empty values and update the proper cell.

    Kevin

  • wijwijwijwij Posts: 54Questions: 12Answers: 0

    Thanks Kevin.
    Any available example?

  • colincolin Posts: 15,240Questions: 1Answers: 2,599
    Answer ✓

    You probably wouldn't want to use postEdit as the data would've been sent to the server by then. preSubmit would be better - as you can count the empty cells (as Kevin described above) and put the value in the data being sent to the server. This example here shows how the case is changed on a field prior to submission - so you would do something like that.

    Also, I want this cell value to get updated automatically when the DT initialized

    This sounds like it has the potential to get messy if you have many rows. It might be worth ensuring that's correct already in the DB. But if not, yep, do as Kevin described, calling edit() to update the rows where the count is incorrect.

    Colin

  • kthorngrenkthorngren Posts: 21,443Questions: 26Answers: 4,974

    I agree with Colin. Storing that count in a field in the datatabse is a much better option.

    Kevin

  • wijwijwijwij Posts: 54Questions: 12Answers: 0

    Thank you Colin & Kevin.
    I will try your suggested ideas and I will update this page accordingly

    Thanks

  • wijwijwijwij Posts: 54Questions: 12Answers: 0

    Hi,

    Thanks to Kevin and Colin's answers, here is the script I used and worked for me, just in case someone may need it.
    The problem I have now is how to communicate the "coun" variable value and send it back to the database? so the datatable got updated immediately.

    I spent really long time trying to find an answer without success

                       var openVals;
                        prodEditor.on('preSubmit', function (e, o, action) { 
                            // Store the values of the fields on open
                            openVals = JSON.stringify(prodEditor.get());  
                            //alert(openVals.is_processed);
                            var subStr = openVals.match("{(.*)}");              
                            globArr = [];               
                            var answ = subStr[1].split(','); 
                            $.each(answ, function () {                 
                                newthis = this.split(':');
                                globArr.push(newthis);
                                if (newthis[1] !== '""' && newthis[1] !== '"0"' && newthis[0] !== '"ProductID"' && newthis[0] !== '"proce"') {                     
                                    coun++;
                                };
                            });
                            alert(coun);
    
  • colincolin Posts: 15,240Questions: 1Answers: 2,599

    You could add that variable to the data being sent - something like this where your alert() is :

        data.coun = coun;
    

    Then if you look at the message on the browser's network tab, you should see it being sent to the server,

    Colin

  • wijwijwijwij Posts: 54Questions: 12Answers: 0

    I added this line right after the alert(coun), and got this error

    Uncaught ReferenceError: data is not defined

  • colincolin Posts: 15,240Questions: 1Answers: 2,599
    Answer ✓

    Sorry, that would be o.coun = coun - I was taking the example from preSubmit,

    Colin

  • wijwijwijwij Posts: 54Questions: 12Answers: 0

    Excellent, I am getting the value of (o.coun) in the developer pane

    Any idea how to replace the current value of "is_processed" with "o.coun"?
    And if successful, I want the datatable to update its content automatically exactly how it did when we edit any of the fields inside the editor

    Many many thanks Colin :)

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

    I'm not clear but I think you would just do the same, i.e.

    o.coun = openVals.is_processed
    

    Colin

  • wijwijwijwij Posts: 54Questions: 12Answers: 0
    edited April 2021

    Thanks Colin.
    What I meant to say is that I want the "is_processed" value in the database to get updated with the value from the "coun" variable as soon as I closed the editor.

    Could it be done using ajax **and **POST?
    or
    just something like the following may suffice?

      if (action === 'edit')
                                {
                                    prodEditor
                                        .edit(prodTable.row({ selected: true }).index(), false)
                                        .set('is_processed', coun)
                                        .submit();  
                                }
    
  • allanallan Posts: 63,687Questions: 1Answers: 10,500 Site admin
    Answer ✓

    I'd suggest making your coun variable a regular Editor field - just of the hidden type. Then instead of using preSubmit move your code into initSubmit which will let you set the value of the hidden field.

    So you can do:

    editor.on('initSubmit', function (e, action) {
      // count how many empty fields there are from `field().val()` for your fields
      // let count = ...;
    
      // Then:
      editor.field('coun').val( count );
    } );
    

    That way coun is just a regular Editor parameter and can be treated as such on the server-side.

    Allan

  • wijwijwijwij Posts: 54Questions: 12Answers: 0

    YES!
    Allan, that was it!
    Oh my God, I spent really long time on this task.
    Thanks a lot, Kevin, Colin, and Allan...you are wonderful guys :smile:

This discussion has been closed.