css Audio - Active file-generic CSS - Active Generic - Active HTML - Active JS - Active SVG - Active Text - Active file-generic Video - Active header Love html icon-new-collection icon-person icon-team numbered-list123 pop-out spinner split-screen star tv

Pen Settings

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

Code Indentation

     

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

HTML Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

            
              <div id='myInstructions'>
    You can click on the table headers to sort.
</div>

<div id='mySearchBar'>
    <label for='myInput'>Search in Description: </label>
    <input id='myInput' type='text' onkeyup='searchDescription();' autofocus/>
</div>

<table id='myTable'>
    <thead>
        <tr>
            <th title='Click to sort by name' onclick='sortBy("name");'>Name</th>
            <th title='Click to sort by description' onclick='sortBy("description");'>Description</th>
            <th title='Click to sort by child ID' onclick='sortBy("child.id");'>Child ID</th>
        </tr>
    </thead>

    <tbody id='myTableBody'>
        <!-- Will be dynamically generated -->
    </tbody>
</table>
            
          
!
            
              #myInstructions {
    margin-bottom: 24px;

    color: steelblue;
    font-family: 'Arial';
    font-size: 24px;
    font-weight: bold;
}

#mySearchBar {
    margin-bottom: 24px;

    label {
        font-family: 'Arial';
        font-size: 16px;
        font-weight: bold;
    }

    input {
        width: 240px;
        font-size: 16px;
    }
}

#myTable {
    th, td {
        border-bottom: 1px solid #ddd;
        padding: 8px 12px;
        
        font-family: 'Arial';
    }

    th {
        background-color: #eee;

        &:hover {
            background-color: #ddd;
            cursor: pointer;
        }
    }
}
            
          
!
            
              /**************************************************

    Get mock data

**************************************************/
// `data` is what we get from the backend.
// `dataFiltered` is what we display on the table.
const data = [
    {
        id: 0,
        name: 'V',
        description: 'v is for victory',
        child: {
            id: 'A273',
            name: 'dog'
        }
    },
    {
        id: 1,
        name: 'e',
        description: undefined,
        child: {
            id: 'A48',
            name: 'cat'
        }
    },
    {
        id: 2,
        name: 'o',
        description: undefined,
        child: {
            id: 'B415',
            name: 'dodo'
        }
    },
    {
        id: 3,
        name: 'h',
        description: 'h is for hello world, this is my first program',
        child: {
            id: 'B30',
            name: 'bison'
        }
    },
    {
        id: 4,
        name: 'U',
        description: 'u is for user experience',
        child: {
            id: 'A706',
            name: 'bear'
        }
    },
    {
        id: 5,
        name: 'y',
        description: 'y is for yo-yo',
        child: {
            id: 'B261',
            name: 'alligator'
        }
    }
];

// Initially, display all data
let dataFiltered = data.slice();


/**************************************************

    Update the table

**************************************************/
let myTableBody = document.getElementById('myTableBody');

const displayTable = (data) => {
    myTableBody.innerHTML = '';

    data.forEach((datum, index) => {
        let row = myTableBody.insertRow();

        // Display name
        let cell_name = row.insertCell(0);
        let node_name = document.createTextNode(datum.name);

        cell_name.appendChild(node_name);


        // TODO #1:
        // 
        // Based on the code above, let's display the
        // `description` and `child's id`, too.
        // 
        // Note, because description is an optional field,
        // some data may have a value of `undefined`. For
        // these descriptions, show an empty string instead.
        //



        //
        // END TODO #1


    });
};

// Display the table for the first time
displayTable(dataFiltered);


/**************************************************

    Search data by description

**************************************************/
let myInput = document.getElementById('myInput');

const searchDescription = () => {
    // Get the query string
    const query = myInput.value.trim();


    // TODO #2:
    // 
    // Set `dataFiltered` to be a filtered array of `data`.
    // 
    // `dataFiltered` contains all data whose description
    // includes `query`.
    // 
    // If the query is empty, then display all data again.
    // 



    //
    // END TODO #2


    // Update the table
    displayTable(dataFiltered);
};


/**************************************************

    Allow sorting data

**************************************************/
const sortDataBy = (data, sortColumn) => {
    switch (sortColumn) {
        case 'name': {
            // Sort in ascending order
            data.sort((a, b) => {
                const value1 = a.name.toLowerCase();
                const value2 = b.name.toLowerCase();

                if (value1 > value2) {
                    return 1;
                }

                if (value2 > value1) {
                    return -1;
                }

                return 0;
            });

            break;
        }

        case 'description': {
            // TODO #3 (option 1):
            // 
            // Implement sorting by description (ascending
            // order only).
            // 
            // In ascending order, we want to show data
            // whose description is `undefined` or an
            // empty string **last**.
            //



            //
            // END TODO #3 (option 1)

            break;
        }

        case 'child.id': {
            // TODO #3 (option 2):
            // 
            // Implement sorting by child's id (ascending
            // order only).
            // 
            // The id will always consist of 1 letter,
            // followed by a series of numbers.
            // 
            // Here, ascending means, we sort by the letter
            // in ascending order first, then by the numbers
            // in ascending order.
            //



            //
            // END TODO #3 (option 2)

            break;
        }
    }

    // Update the table
    displayTable(data);
};

const sortBy = (sortColumn) => {
    // Sort the filtered list
    sortDataBy(dataFiltered, sortColumn);
};
            
          
!
999px
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.
Loading ..................

Console