Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Vertical Reordering of HTML Table Rows Using Shield UI

HTML table elements are used to represent tabular data in an optimized way. Different data elements are organized in rows and columns, presented on the page. In this article, we will demonstrate how to use the Shield UI Draggable component for changing the order of table rows by dragging them with the mouse.

The default behavior of HTML tables can be enhanced by adding various customizations like styling, buttons and handling specific events. Examples of that are the Bootstrap table CSS and the Shield UI Grid, which can be used to develop complex user interface scenarios.

The example below shows how you can add row reordering to the HTML table element styled with Bootstrap, which is general and can be applied to all table widgets like Shield UI's and others.

Requirements and Dependencies

Our example will use the jQuery, Shield UI and Bootstrap libraries. Hence you need to include the following resources in your HTML head section:

    
    

The HTML Markup

To represent our data, we add a standard HTML table element and an "Add Row" button to the page body. The button will be used to add new rows to the table:

    Title  
1. Cormen, Leiserson, Rivest, Introduction to Algorithms
2. Weiss, Data Structures and Problem Solving Using C++
3. Friedman, Koffman, Problem Solving, Abstraction and Design Using C++

HTML Styles

To make the table more visually attractive and improve the drag/drop experience, we add the following styles section to the head:

The JavaScript

The final piece is the JavaScript code that connects the HTML table with the Shield UI Draggable component. All the functions below are added to the same jQuery closure having the form:

First we will add the function that will be called to handle changes in the row order - i.e. on events like dragging, inserting new rows and deletion of rows:

    var onAfterReordering = function() {
        // here we just fix the numbers shown for each row

        // in reality, one can store the order information as an attribute of the row, 
        // or using jQuery.data on the row object, etc
        // this can be used to determine how the order has changed after dragging finishes

        $("#myTable tbody tr").each(function(index, row) {
            $($(row).find("td").get(1)).html((index + 1) + ".");
        });
    };

The most important part is the code that initializes dragging for each row from our table. Below is the definition of the function that does this for a single row:

    var initRowReordering = function(row) {
        $(row).css("cursor", "move");

        $(row).shieldDraggable({
            helper: function (params) {
                // the draggable helper is the element used as a preview of the element being dragged
                // it can be a copy, or the actual element

                // here we create a copy of the dragged row and add it in a table, 
                // so that the styles can be applied
                var helper = $('
'); var tbody = $('').appendTo(helper); tbody.append(row.clone()); // fix the style of the TDs in the helper row - widths are copied from the original row // this will make the drag helper look like the original helper.find('td').each(function (index) { $(this).width($(row.find('td')[index]).width()); }); helper.width(row.width()); return helper; }, events: { start: function (e) { // add a custom class to the dragged element // this will "hide" the row being dragged $(row).addClass("dragged"); }, drag: function (e) { // as the element is dragged, determine where to move the dragged row var element = $(e.element), elTopOffset = element.offset().top; var rows = $(row).siblings('tr').not('.dragged').get(); for (var i = 0; i elTopOffset) { $(row).insertBefore($(rows[i])); break; } // if last and still not moved, check if we need to move after if (i >= rows.length - 1) { // move element to the last - after the current $(row).insertAfter($(rows[i])); } } }, stop: function (e) { // dragging has stopped - remove the added classes $(row).removeClass("dragged"); // cancel the event, so the original element is NOT moved // to the position of the handle being dragged e.cancelled = true; e.skipAnimation = true; // call the on-after-reorder handler function right after this one finishes setTimeout(onAfterReordering, 50); } } }); };

Last, we will add the code that will initialize the row reordering for each row, add the event handlers for the Delete buttons on each row, and the code that gets executed when the "Add Row" button is clicked:

    // initializes the row reordering for each row
    $("#myTable tbody tr").each(function () {
        initRowReordering($(this));
    });

    // clicking the Delete button should delete the row and resync ordering information
    $("#myTable tbody tr button.btn-danger").click(function() {
        $(this).closest('tr').remove();
        onAfterReordering();
    });

    // adding a new row initializes reordering for it, as well as refreshes the ordering information
    $("#myAddButton").click(function() {
        var title = prompt("Enter new title: ");
        if (title) {
            var row = $('' + 
                '' + 
                '' + 
                '' + title + '' + 
                '' + 
                '').appendTo($("#myTable tbody"));
            initRowReordering(row);
            onAfterReordering();
        }
    });

The complete code can be seen on this page.

Tags: 
JavaScript, Grid, Data Visualization

Blog Categories

  • General
  • Showcase
  • Technical
  • White Papers
Sub Title Background: 


This post first appeared on Shield UI Blogs | Shield UI, please read the originial post: here

Share the post

Vertical Reordering of HTML Table Rows Using Shield UI

×

Subscribe to Shield Ui Blogs | Shield Ui

Get updates delivered right to your inbox!

Thank you for your subscription

×