Skip to content

Instantly share code, notes, and snippets.

@robertvrabel
Last active February 27, 2022 15:46
Show Gist options
  • Select an option

  • Save robertvrabel/7f878cebd0e62ec9ed0cb2779a3097a9 to your computer and use it in GitHub Desktop.

Select an option

Save robertvrabel/7f878cebd0e62ec9ed0cb2779a3097a9 to your computer and use it in GitHub Desktop.

Revisions

  1. robertvrabel revised this gist Mar 9, 2017. 4 changed files with 4 additions and 4 deletions.
    2 changes: 1 addition & 1 deletion move-down.php
    Original file line number Diff line number Diff line change
    @@ -4,5 +4,5 @@
    SET display_order = (display_order - 1)
    WHERE display_order > :current_position
    AND display_order <= :desired_position
    AND user_id = :user_id;
    AND user_id = :user_id";
    }
    2 changes: 1 addition & 1 deletion move-up.php
    Original file line number Diff line number Diff line change
    @@ -4,5 +4,5 @@
    SET display_order = (display_order + 1)
    WHERE display_order >= :desired_position
    AND display_order < :current_position
    AND user_id = :user_id
    AND user_id = :user_id";
    }
    2 changes: 1 addition & 1 deletion reset.php
    Original file line number Diff line number Diff line change
    @@ -2,4 +2,4 @@
    $query = "UPDATE todos
    SET display_order = :desired_position
    WHERE display_order = 0
    AND user_id = :user_id;
    AND user_id = :user_id";
    2 changes: 1 addition & 1 deletion set.php
    Original file line number Diff line number Diff line change
    @@ -2,4 +2,4 @@
    $query = "UPDATE todos
    SET display_order = 0
    WHERE display_order = :current_position
    AND user_id = :user_id;
    AND user_id = :user_id";
  2. robertvrabel revised this gist Mar 9, 2017. 1 changed file with 4 additions and 4 deletions.
    8 changes: 4 additions & 4 deletions move-up.php
    Original file line number Diff line number Diff line change
    @@ -1,8 +1,8 @@
    // Move up: Update the items between the desired position and the current position, increasing each item by 1 to make space for the new item
    if ($move == 'up') {
    $query = "UPDATE `form_field_rel`
    $query = "UPDATE todos
    SET display_order = (display_order + 1)
    WHERE display_order >= " . $desired_position . "
    AND display_order < " . $current_position . "
    AND form_id = " . $form_id;
    WHERE display_order >= :desired_position
    AND display_order < :current_position
    AND user_id = :user_id
    }
  3. robertvrabel revised this gist Mar 9, 2017. 1 changed file with 36 additions and 0 deletions.
    36 changes: 36 additions & 0 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,36 @@
    <table>
    <thead>
    <tr>
    <th scope="col">user_id</th>
    <th scope="col">display_order</th>
    <th scope="col">todo</th>
    </tr>
    </thead>
    <tbody>
    <tr>
    <td>2000</td>
    <td>1</td>
    <td>Take out garbage</td>
    </tr>
    <tr>
    <td>2000</td>
    <td>2</td>
    <td>Clean house</td>
    </tr>
    <tr>
    <td>2000</td>
    <td>3</td>
    <td>Do dishes</td>
    </tr>
    <tr>
    <td>2000</td>
    <td>4</td>
    <td>Cut grass</td>
    </tr>
    <tr>
    <td>2000</td>
    <td>5</td>
    <td>Change light bulb</td>
    </tr>
    </tbody>
    </table>
  4. robertvrabel revised this gist Mar 9, 2017. 1 changed file with 7 additions and 0 deletions.
    7 changes: 7 additions & 0 deletions list.html
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,7 @@
    <ul class="todos">
    <li class="todo">Take out garbage</li>
    <li class="todo">Clean house</li>
    <li class="todo">Do dishes</li>
    <li class="todo">Cut grass</li>
    <li class="todo">Change light bulb</li>
    </ul>
  5. robertvrabel revised this gist Mar 9, 2017. 4 changed files with 0 additions and 0 deletions.
    File renamed without changes.
    File renamed without changes.
    File renamed without changes.
    File renamed without changes.
  6. robertvrabel revised this gist Mar 9, 2017. 2 changed files with 0 additions and 0 deletions.
    File renamed without changes.
    File renamed without changes.
  7. robertvrabel created this gist Mar 9, 2017.
    35 changes: 35 additions & 0 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,35 @@
    // Make the form fields sortable/draggable
    $('.todos').sortable({
    items: '.todo',
    start: function(event, ui) {
    // Create a temporary attribute on the element with the old index
    $(this).attr('data-currentindex', ui.item.index());
    },
    update: function(event, ui) {
    let user_id = $('#user_id').val();
    let current_position = $(this).attr('data-currentindex');
    let desired_position = ui.item.index();

    // Reset the current index
    $(this).removeAttr('data-currentindex');

    // Post to the server to handle the changes
    $.ajax({
    type: "POST",
    url: "/url-to-handle-database-updates/",
    data: {
    desired_position: desired_position,
    current_position: current_position,
    user_id: user_id
    },
    beforeSend: function() {
    // Disable dragging
    $('.todos').sortable('disable');
    },
    success: function(html) {
    // Re-enable dragging
    $('.todos').sortable('enable');
    }
    });
    }
    });
    2 changes: 2 additions & 0 deletions gistfile2.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,2 @@
    // Determine if the user is moving the item up or down in the listing
    $move = $desired_position > $current_position ? 'down' : 'up';
    5 changes: 5 additions & 0 deletions gistfile3.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,5 @@
    // Set the display_order for the dragged item to be 0 so we can update this record later by display_order = 0
    $query = "UPDATE todos
    SET display_order = 0
    WHERE display_order = :current_position
    AND user_id = :user_id;
    8 changes: 8 additions & 0 deletions gistfile4.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,8 @@
    // Move down: Update the items between the current position and the desired position, decreasing each item by 1 to make space for the new item
    if ($move == 'down') {
    $query = "UPDATE todos
    SET display_order = (display_order - 1)
    WHERE display_order > :current_position
    AND display_order <= :desired_position
    AND user_id = :user_id;
    }
    8 changes: 8 additions & 0 deletions gistfile5.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,8 @@
    // Move up: Update the items between the desired position and the current position, increasing each item by 1 to make space for the new item
    if ($move == 'up') {
    $query = "UPDATE `form_field_rel`
    SET display_order = (display_order + 1)
    WHERE display_order >= " . $desired_position . "
    AND display_order < " . $current_position . "
    AND form_id = " . $form_id;
    }
    5 changes: 5 additions & 0 deletions gistfile6.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,5 @@
    // Update the item that was dragged and set it to be the desired position now that the slot is opend up
    $query = "UPDATE todos
    SET display_order = :desired_position
    WHERE display_order = 0
    AND user_id = :user_id;