Search code examples
ruby-on-railsdhtmlx

onFullSync action, show updated info - DHTMLX Grid + RoR


I have a Ruby on Rails project where I use a DHTMLX Grid.

Is there a way of showing, using the event handler "onFullSync" provided by the grid API, to show updated data?

Let me explain a little better... I know I can do something like:

dp.attachEvent("onFullSync", function(){
       alert("update complete");
  })

But what I want is something more complex. I want to, after each completed update, alter a div adding the information like this:

  • Field 2 was updated to XYZ and field 3 was updated to XER on line X
  • Field 1 was updated to 123 and field 3 was updated to XSD on line Y

Is this possible?

Thanks


Solution

  • So, what I end up doing was:

    After creating the grid I created an array:

    var samples = [];       
    

    Then, as per @Aquatic suggestion, I added to "onEditCell" event the following line:

    samples[samples.length] = grid.cells(rId, 5).getValue();
    

    This allowed me to add to the array the value present on column 5 of the row changed. Then, on "onFullSync" event I hide or show the div created on the view with the messages (I distinguish if it's on row or more changed on the grid).

    //Deals with messages after update
    dp.attachEvent("onFullSync", function(){
      unique_samples = uniq_fast(samples.sort());
        if (unique_samples.length == 1){
          $('#updated-samples').text("");
          $(".messages").show();
          $('#updated-samples').text("A seguinte amostra foi actualizada: " + unique_samples[0]);
          //to clear the array
          samples = [];
        } else if (unique_samples.length > 1){
            $('#updated-samples').text("");
            $(".messages").show();
            $('#updated-samples').text("As seguintes amostras foram actualizadas: " + unique_samples.sort().join(", "));
            //to clear the array
          samples = [];
           } else {
              $('#updated-samples').text("");
              $(".messages").hide();   
             //to clear the array
          samples = [];          
              } 
      })
    

    The problem with using "onEditCell" is that everytime a field is changed on that row I get a repeated value on my "samples" array, I I had to remove duplicate from that array. For that I used one of the suggestions at this answer

      // remove duplicates in array
      function uniq_fast(a) {
          var seen = {};
          var out = [];
          var len = a.length;
          var j = 0;
          for(var i = 0; i < len; i++) {
               var item = a[i];
               if(seen[item] !== 1) {
                     seen[item] = 1;
                     out[j++] = item;
               }
          }
          return out;
      }
    

    Then I have on the beginning of the view, to show the messages:

    <div class="alert alert-success messages" id="updated-samples">
    

    And that's it. I could be not the most efficient way but it works for what I wanted. I will leave the question open a few more days to see if any other option appears.