0

I'm working on a little test code using angular-dragula and I have managed to get the drag-and-drop thing working, but it's not updating the source data array.

I declare some variables in the angular controller:

$scope.todos=['Stuff','Thangs'];
$scope.plan=new Array(($scope.endTime - $scope.startTime)*(60/$scope.timeBlock));

for(var i=0;i<$scope.plan.length;i++){
  $scope.plan[i]=["-"];
}

Which is perhaps not hte best way to initialise, but I get an array of arrays, each containing an "placeholder" character.

The HTML then draws this out:

<div ng-app='dayplannerApp' ng-controller="MainCtrl">
  <div class='wrapper'>
    <button ng-click='dump()'>Dump</button>
  <div class='container' id='source-box' dragula='"first-bag"' dragula-model='todos'>
    <div id="source" ng-repeat='text in todos' ng-bind='text'></div>
  </div>
  <hr>
    <div class='container'><pre>{{plan[0] | json}}</pre></div>
    <div class='container'><pre>{{plan[1] | json}}</pre></div>
  <hr>
  <table ng-repeat="n in  Range()" border='1'>
    <tr ng-class-odd="'odd'" ng-class-even="'even'" ><td width='20%'>{{n}}</td><td>
      <div class='container' dragula='"first-bag"'  dragula-model='plan[$index]'>
      <div ng-attr-id="{{ 'block-' + n}}"  ng-repeat='text in plan[$index]'  ng-bind='text' ng-click='setId($event)'></div>
      </div></td></tr>
  </table>
</div>
  </div>

This seems to work, I can drag 'stuff' or 'thangs' over to the divs that contains the ng-repeat and can drop them there, the two "pre" containers are updated if I drop things in the first two rows.

But when I try to dump the underlying "plan" array with:

console.log($scope.plan);

I just get the original array of array with only the placeholders. Am i doing something wrong with the model assignment? or the binding? Why does plan[1] get updated in the div, but not in the javascript?

johnnyRose
  • 7,310
  • 17
  • 40
  • 61
Dave Alger
  • 339
  • 7
  • 23

1 Answers1

0

What I managed to do was re-write with:

for (var hour = $scope.startTime; hour < $scope.endTime; hour++) {
       for (var mins = 0; mins < 60; mins+=$scope.timeBlock){
        var minVal = mins;
        if (mins===0){
          minVal='00';
        }
        result.push(
          {
            'Time': hour + ':' + minVal,
            'ToDo': []
          });
      }
    }
    return result;

Then I used that as the source:

<table  border=1 style="table-layout:fixed;">
        <tr><th width='10%'>Time</th><th width='80%'>Activity</th><th width='5%'>Drop here</th></tr>
        <span>
        <tr ng-repeat="n in Day" ng-class-odd="'odd'" ng-class-even="'even'" >
            <td width='20%'>{{n.Time}}</td>
            <td width='80%'>{{n.ToDo[0]}}</td>
            <td width='10%' style='background-color:black;overflow:hidden' class='container' dragula='"first-bag"' dragula-model='n.ToDo'>
              {{n.ToDo}}
            </td>
        </tr>
        </span>
      </table>

Not really sure what the problem was. Is it that plan[$index] as the model won't update?

Dave Alger
  • 339
  • 7
  • 23