volosoft / jtable

A JQuery plugin to create AJAX based CRUD tables.
http://www.jtable.org
1.1k stars 505 forks source link

Unable to Sort using jTsorting #1216

Open pv619 opened 10 years ago

pv619 commented 10 years ago

Hi.. I am trying to sort my columns but it ain't doing it for some reason. Am I missing something here?

My Controller code:

 public JsonResult BilingReportList(int jtStartIndex = 0, int jtPageSize = 0, string jtSorting = null)
{
    try
    {
        if (Request.IsAuthenticated == true)
        {
            string Path = @"C:\\5Newwithdate.xls";
            OleDbConnection con = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source= '" + Path + "';Extended Properties=" + (char)34 + "Excel 8.0;IMEX=1;" + (char)34 + "");
            OleDbDataAdapter da = new OleDbDataAdapter("select * from [Sheet1$]", con);
            System.Data.DataTable data = new System.Data.DataTable();
            da.Fill(data);
            con.Close();

            List<TopPlayed> daa = new List<TopPlayed>();
            foreach (DataRow p in data.Rows)
            {
                TopPlayed top = new TopPlayed()

                {
                    TrackID = Convert.ToInt32(p.Field<double>("ID")),
                    TrackName = p.Field<string>("Track Name"),
                    ArtistName = p.Field<string>("Artist Name"),
                    Times = Convert.ToInt32(p.Field<double>("NoOfPlays"))
                };

                daa.Add(top);
            }

            var newlist = daa.OrderByDescending(i => i.Times);       

            return Json(new { Result = "OK", Records = newlist, TotalRecordCount = daa.Count });

        }
        return Json(new { Result = "ERROR" });
    }
    catch (Exception ex)
    {
        return Json(new { Result = "ERROR", Message = ex.Message });
    }
}

jtable View Code:

$(document).ready(function () {

$('#StudentTableContainer').jtable({

    title: 'Top Played Tracks',
    paging: false, 
    sorting: true,
    defaultSorting: 'Track Name ASC', //Set default sorting
    actions: {
        listAction: '@Url.Action("BilingReportList")'
    },
    fields: {
        TrackID: {
            title: 'Track ID',
            list: true,
            sorting: true
        },
        TrackName: {
            title: 'Track Name',
            list: true
        },
        ArtistName: {
            title: 'Artist Name',
            list: true
        },
        Times: {
            title: 'Times',
            key: true,
            create: false,
            edit: false,
            resize: false,
            list: true
        },
    }
});

$('#StudentTableContainer').jtable('load');
});

On the client side I have enabled sorting but still it ain't doing it.

Please help me out, I am new to jTable implementing with C# ASP.NET MVC 4.. Any help is highly appreciated :)

pv619 commented 10 years ago

anyone please????

misterparsons commented 10 years ago

Hi,

The sorting is performed on the server not in the browser. So your server code needs to respond to the jtSorting parameter.

Good Luck

pv619 commented 10 years ago

Hi Misterparsons,

Thanks for your suggestion.. I have tried passing the jtSorting parameter but it keeps on failing. Is it possible for you to show how to pass the jtSorting parameter on this line of the code?

var newlist = daa.OrderByDescending(i => i.Times).ToList().GetRange(jtStartIndex, jtPageSize).Sort(jtSorting);

I have been stuck with it from a week now, have tried mostly everything.

Hope that you help out and thanks in advance :)

misterparsons commented 10 years ago

Hi,

If sorting is turned on, jtable posts it to the server with each listAction request. You do not need to do anything in the browser, same is true for jtStartIndex and jtPageSize. What browser and server are you using? Your server code needs to respond to the parameter and do the necessary sorting.

Also looking at your code the default sorting should match the field name not the title of the field. i.e TrackName.

georgetrad commented 10 years ago

@pv619 I will help you achieving this with PHP:

  1. You need to get what type of sorting the user has selected by doing this : $sorting = $_GET['jtSorting'];
  2. Then you will have to use $sorting variable in the SQL query like this: SELECT * FROM $tableName ORDER BY $sorting
mitchimangubat commented 9 years ago

@georgetrad Thank you for the solution. This has totally helped me on fixing the sorting issue.