Open In App

jQWidgets jqxDataTable sortBy() Method

jQWidgets is a JavaScript framework for making web-based applications for PC and mobile devices. It is a very powerful, optimized, platform-independent, and widely supported framework. The jqxDataTable is used for reading and displaying the data from the HTML Table. This is also used to display data from various data sources like XML, JSON, Array, CSV, or TSV.

The sortBy() method is used to sort a specified column if sortable is set to true.



Syntax:

$("#dataTable").jqxDataTable('sortBy', 'dataField', 'sortOrder');

Parameters: This method accepts two parameters which are illustrated below:



 

Linked Files: Download jQWidgets from the given link. In the HTML file, locate the script files in the downloaded folder.

<link rel=”stylesheet” href=”jqwidgets/styles/jqx.base.css” type=”text/css” />
<script type=”text/javascript” src=”scripts/jquery.js”></script>
<script type=”text/javascript” src=”jqwidgets/jqxcore.js”></script>
<script type=”text/javascript” src=”jqwidgets/jqxdata.js”></script>
<script type=”text/javascript” src=”jqwidgets/jqxbuttons.js”></script>
<script type=”text/javascript” src=”jqwidgets/jqxscrollbar.js”></script>
<script type=”text/javascript” src=”jqwidgets/jqxlistbox.js”></script>
<script type=”text/javascript” src=”jqwidgets/jqxdropdownlist.js”></script>
<script type=”text/javascript” src=”jqwidgets/jqxdatatable.js”></script>

Example: The below example illustrates the jQWidgets sortBy() method.




<!DOCTYPE html>
<html lang="en">
  
<head>
    <link rel="stylesheet" href=
        "jqwidgets/styles/jqx.base.css" type="text/css" />
    <script type="text/javascript" 
        src="scripts/jquery.js"></script>
    <script type="text/javascript" 
        src="jqwidgets/jqxcore.js"></script>
    <script type="text/javascript" 
        src="jqwidgets/jqxdata.js"></script>
    <script type="text/javascript" 
        src="jqwidgets/jqxbuttons.js"></script>
    <script type="text/javascript" 
        src="jqwidgets/jqxscrollbar.js"></script>
    <script type="text/javascript" 
        src="jqwidgets/jqxlistbox.js"></script>
    <script type="text/javascript" 
        src="jqwidgets/jqxdropdownlist.js"></script>
    <script type="text/javascript" 
        src="jqwidgets/jqxdatatable.js"></script>
  
    <script>
        $(document).ready(function () {
            var data = new Array();
            var Employee_Name = ["Ravi", 
                "Sumit", "Amit", "Aakash"];
                  
            var Company = ["GeeksforGeeks", 
                "Amazon", "Google", "GeeksforGeeks"];
  
            var Designation = ["Content Writer", 
                "Software Engineer", 
                "Data Scientist", "HR"];
  
            a = 0;
            while (a < 4) {
                var row = {};
                row["Employee_Name"] = Employee_Name[a];
                row["Company"] = Company[a];
                row["Designation"] = Designation[a];
                data[a] = row;
                a++;
            }
  
            var source = {
                localData: data,
                dataType: "array",
                dataFields: [
                    {
                        name: "Employee_Name",
                        type: "string",
                    },
                    {
                        name: "Company",
                        type: "string",
                    },
                    {
                        name: "Designation",
                        type: "string",
                    },
                ],
            };
            var dataAdapter = new $.jqx.dataAdapter(source);
            $("#table").jqxDataTable({
                width: 550,
                theme: "energyblue",
                editable: true,
                sortable: true,
                source: dataAdapter,
                columns: [
                    {
                        text: "Employee_Name",
                        dataField: "Employee_Name",
                        width: 200,
                    },
                    {
                        text: "Company",
                        dataField: "Company",
                        width: 160,
                    },
                    {
                        text: "Designation",
                        dataField: "Designation",
                        width: 190,
                    },
                ],
            });
            $("#jqxbutton").jqxButton({
                theme: "energyblue",
                height: 30,
            });
            $("#jqxbutton").click(function () {
                $("#table").jqxDataTable(
                    "sortBy", "Employee_Name", "asc");
            });
        });
    </script>
</head>
  
<body>
    <center>
        <h1 style="color: green;">
            GeeksforGeeks
        </h1>
        <h3>
            jQWidgets jqxDataTable sortBy() Method
        </h3>
        <div id="table"></div>
        <input type="button" style="margin: 20px;" 
            id="jqxbutton" value="Sort the first column" />
    </center>
</body>
  
</html>

Output:

Reference: https://www.jqwidgets.com/jquery-widgets-documentation/documentation/jqxdatatable/jquery-datatable-api.htm?search=


Article Tags :