Radio Buttons Sort Control

Radio buttons sort controls are used to sorts content located at the specified CSS selector paths.

See the Pen jPList Radio Buttons Sort Control by 1rosehip (@1rosehip) on CodePen.

HTML Structure

Radio buttons sort control is defined by required data-jplist-control="radio-buttons-sort" data attribute. The data-group attribute defines group of elements that should be sorted. All buttons of the same group should have the same name or data-name attribute. The full list of data attributes can be found here.

<!-- jplist styles - should not be added if bootstrap or other CSS framework is used -->
<link href="jplist.styles.css" rel="stylesheet" type="text/css" />

<!-- sort by title ASC -->
<label>
    <input
        type="radio"
        data-jplist-control="radio-buttons-sort"
        data-path=".title"
        data-group="group1"
        data-order="asc"
        data-type="text"
        data-name="sort1"
        name="sort1"
        checked />
    Sort by title asc
</label>

<!-- sort by title DESC -->
<label>
    <input
        type="radio"
        data-jplist-control="radio-buttons-sort"
        data-path=".title"
        data-group="group1"
        data-order="desc"
        data-type="text"
        data-name="sort1"
        name="sort1" />
    Sort by title desc
</label>

<!-- content to sort -->
<div data-jplist-group="group1">

    <!-- item -->
    <div data-jplist-item>
        <div class="title">Title value 1</div>
        ....
    </div>

    <!-- item -->
    <div data-jplist-item>
        <div class="title">Title value 1</div>
        ....
    </div>

    <!-- item -->
    <div data-jplist-item>
        <div class="title">Title value 1</div>
        ....
    </div>

</div>

<!-- jplist library -->
<script src="jplist.min.js"></script>
<script>
    jplist.init();
</script>
            

Top and bottom panels

Radio button sort controls can be placed in both top and bottom panels for the better user experience. In this case data-name attribute should be the same within all radio buttons of the same group, but the name attribute should be different in top and bottom panels:

<!-- TOP PANEL CONTROLS -->
<!-- sort by title ASC -->
<label>
    <input
        type="radio"
        data-jplist-control="radio-buttons-sort"
        data-path=".title"
        data-group="group1"
        data-order="asc"
        data-type="text"
        data-name="sort1"
        name="top-panel-sort"
        checked />
    Sort by title asc
</label>

<!-- sort by title DESC -->
<label>
    <input
        type="radio"
        data-jplist-control="radio-buttons-sort"
        data-path=".title"
        data-group="group1"
        data-order="desc"
        data-type="text"
        data-name="sort1"
        name="top-panel-sort" />
    Sort by title desc
</label>

<!-- content to sort -->
<div data-jplist-group="group1">

    <!-- item -->
    <div data-jplist-item>
        <div class="title">Title value 1</div>
        ....
    </div>

    <!-- item -->
    <div data-jplist-item>
        <div class="title">Title value 1</div>
        ....
    </div>

    <!-- item -->
    <div data-jplist-item>
        <div class="title">Title value 1</div>
        ....
    </div>

</div>

<!-- BOTTOM PANEL CONTROLS -->
<!-- sort by title ASC -->
<label>
    <input
        type="radio"
        data-jplist-control="radio-buttons-sort"
        data-path=".title"
        data-group="group1"
        data-order="asc"
        data-type="text"
        data-name="sort1"
        name="bottom-panel-sort"
        checked />
    Sort by title asc
</label>

<!-- sort by title DESC -->
<label>
    <input
        type="radio"
        data-jplist-control="radio-buttons-sort"
        data-path=".title"
        data-group="group1"
        data-order="desc"
        data-type="text"
        data-name="sort1"
        name="bottom-panel-sort" />
    Sort by title desc
</label>
            

Jump to top

data-jump data attribute can be used to scroll page to the specified location when user selects a radio button. data-jump="top" scrolls page to the top. Any CSS selector can be used instead of top keyword.

<!-- sort by title ASC -->
<label>
    <input
        type="radio"
        data-jplist-control="radio-buttons-sort"
        data-path=".title"
        data-group="group1"
        data-order="asc"
        data-type="text"
        data-name="sort1"
        name="sort1"
        data-jump="top" />
    Sort by title asc
</label>
            

Double / Multiple Sorting

Double sorting can be achieved with additional set of data-path-1, data-order-1 and data-type-1 attributes. There can be any number of such sets, for example (data-path-2, data-order-2, data-type-2), (data-path-3, data-order-3, data-type-3), ... etc.

<label>
    <input
        type="radio"
        data-jplist-control="radio-buttons-sort"
        data-group="group1"
        data-name="sort1"
        name="sort1"
        checked

        data-path=".title"
        data-order="asc"
        data-type="text"

        data-path-1=".price"
        data-order-1="desc"
        data-type-1="number" />
    Sort by title ASC and by price DESC
</label>

<!-- content to sort -->
<div data-jplist-group="group1">

    <!-- item -->
    <div data-jplist-item>
        <div class="title">Title value 1</div>
        <div class="price">12</div>
        ....
    </div>

    <!-- item -->
    <div data-jplist-item>
        <div class="title">Title value 1</div>
        <div class="price">45</div>
        ....
    </div>

    <!-- item -->
    <div data-jplist-item>
        <div class="title">Title value 1</div>
        <div class="price">10</div>
        ....
    </div>

</div>

<!-- jplist library -->
<script src="jplist.min.js"></script>
<script>
    jplist.init();
</script>
            

Deep Linking

Deep links can be used to keep user selections and control states after the page refresh. To implement them data-id attribute should be added to HTML structure and deepLinking setting should be true in jPList parameters.

<!-- jplist styles - should not be added if bootstrap or other CSS framework is used -->
<link href="jplist.styles.css" rel="stylesheet" type="text/css" />

<!-- sort by title ASC -->
<label>
    <input
        type="radio"
        data-jplist-control="radio-buttons-sort"
        data-path=".title"
        data-group="group1"
        data-order="asc"
        data-type="text"
        data-name="sort1"
        name="sort1"
        data-id="titleasc"
        checked />
    Sort by title asc
</label>

<!-- sort by title DESC -->
<label>
    <input
        type="radio"
        data-jplist-control="radio-buttons-sort"
        data-path=".title"
        data-group="group1"
        data-order="desc"
        data-type="text"
        data-name="sort1"
        data-id="titledesc"
        name="sort1" />
    Sort by title desc
</label>

<!-- content to sort -->
<div data-jplist-group="group1">

    <!-- item -->
    <div data-jplist-item>
        <div class="title">Title value 1</div>
        ....
    </div>

    <!-- item -->
    <div data-jplist-item>
        <div class="title">Title value 1</div>
        ....
    </div>

    <!-- item -->
    <div data-jplist-item>
        <div class="title">Title value 1</div>
        ....
    </div>

</div>

<!-- jplist library -->
<script src="jplist.min.js"></script>
<script>
    jplist.init({
        deepLinking: true
    });
</script>
            

Storage

Local storage, session storage or cookies can be used to keep user selections and control states after the page refresh. To implement them data-id attribute should be added to HTML structure and storage setting should be passed to jPList constructor.

<!-- jplist styles - should not be added if bootstrap or other CSS framework is used -->
<link href="jplist.styles.css" rel="stylesheet" type="text/css" />

<!-- sort by title ASC -->
<label>
    <input
        type="radio"
        data-jplist-control="radio-buttons-sort"
        data-path=".title"
        data-group="group1"
        data-order="asc"
        data-type="text"
        data-name="sort1"
        name="sort1"
        data-id="titleasc"
        checked />
        Sort by title asc
</label>

<!-- sort by title DESC -->
<label>
    <input
        type="radio"
        data-jplist-control="radio-buttons-sort"
        data-path=".title"
        data-group="group1"
        data-order="desc"
        data-type="text"
        data-name="sort1"
        data-id="titledesc"
        name="sort1" />
        Sort by title desc
</label>

<!-- content to sort -->
<div data-jplist-group="group1">

    <!-- item -->
    <div data-jplist-item>
        <div class="title">Title value 1</div>
        ....
    </div>

    <!-- item -->
    <div data-jplist-item>
        <div class="title">Title value 1</div>
        ....
    </div>

    <!-- item -->
    <div data-jplist-item>
        <div class="title">Title value 1</div>
        ....
    </div>

</div>

<!-- jplist library -->
<script src="jplist.min.js"></script>
<script>
    jplist.init({
        storage: 'localStorage', //'localStorage', 'sessionStorage' or 'cookies'
        storageName: 'jplist'
    });
</script>
            

Data Attributes

The full list of control data attributes.

Name Description Values
data-jplist-control Defines radio buttons sort control. "radio-buttons-sort"
data-group Defines group of items that should be sorted. For example, if a control has data-group="group1" then items group should have data-jplist-group="group1" data attribute. any text value
data-name The data-name attribute is used to identify the group of sort buttons in radio mode or the same controls in different panels. Different controls should have different data-name attributes. By default, data-name attribute has default value. any text value
data-mode Defines sort buttons behaviour. "radio" (the default) or "checkbox"
data-path CSS selector that defines the HTML element that should be sorted "default" keyword for the initial value or any CSS selector
data-order Specifies the sort order. "asc" or "desc"
data-type Specifies the type of content that should be sorted. "text", "number" or "datetime"
data-datetime-format This attribute is used when data-type="datetime". It defines the structure of the date using following wildcards: {year}, {month}, {day}, {hour}, {min}, {sec}. Date structure, for example: data-datetime-format="{month} {day}, {year}"
data-selected Specifies if the button should be selected on page load. "true" or "false"
data-jump This data attribute can be used to scroll page to the specified location when user clicks a button. data-jump="top" scrolls page to the top. Any CSS selector can be used instead of top keyword. top keyword or any CSS selector
data-id This attribute is used for deep linking. any text value that may contain letters, digits, underscores or dashes