Search code examples
javascriptangulardrag-and-dropsortablejs

Drag and drop column to another row in table using ngx-sortablejs


I am trying to drag column of a row to another row but it can't drop to another row's column array. I have following type of array ( sample data array ).

[
 { // row
    key: value,
      cols: [{ key: value }] // cols array
   },
  //n numers of row
]

My code.

<table id="id">
    <tbody sortablejs ngDefaultControl [(ngModel)]="Array">
        <tr *ngFor="let item of Array; let i = index">
            <td class="myRowClass sort-disabled">
                <!-- index logic -->
            </td>
            <td class="myRowClass sort-disabled">
                <!--logic -->
            </td>
            <td sortablejs>
                <div *ngFor="let cell of item.cols" class="divSize">
                    <!-- column logic -->
                </div>
            </td>
        </tr>
    </tbody>
</table>

Solution

  • By using [sortablejsOptions]="options" i achieved my functionality.

    <table id="id">
     <tbody [sortablejs]="Array" [sortablejsOptions]="Array">
      <tr *ngFor="let item of Array">
       <td class="myRowClass sort-disabled">
        <!-- index logic -->
       </td>
       <td>
        <!--row logic -->
       </td>
       <td [sortablejs]="item.cols" [sortablejsOptions]="options">
        <div *ngFor="let cell of item.cols; let j = index">
         <!--column logic -->
        </div>
       </td>
      </tr>
     </tbody>
    </table>
    

    options in .ts file group name must be same.

    options: Options = {
        group: { name: "test" }
    }