Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • ThomasBrunbjerg 90 posts 182 karma points
    Sep 14, 2017 @ 08:22
    ThomasBrunbjerg
    0

    Allow for pagination, sorting and searching for custom repository

    So I am using UIomatic so have a custom section in my backoffice for manipulating SQL data. It's been working great so far and I managed to have all my data be displayed and to allow for standard CRUD operations, except Create, which I don't need.

    My client wants to have a search functionality, as well as sorting and pagination when more than say 20 items are displayed on a page. All these features are included in the standard UIomatic repository, but since I am creating a custom one, I will need to type these functions more or less from scratch. I have the source code for the default function that allows for this, but I am not sure how to rewrite this to work with mine.

    This is the signature of the function:

    public override UIOMaticPagedResult<Booking2> GetPaged(int pageNumber, int itemsPerPage, string searchTerm = "", IDictionary<string, string> filters = null,
                string sortColumn = "", string sortOrder = "")
    

    To fetch all my data from the SQL DB I am using a standard SQL query and returning this as an IEnumerable of my model type.

    public override IEnumerable<Booking2> GetAll(string sortColumn = "", string sortOrder = "")
            {
                var bookinger = new List<Booking2>();
                var conn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["umbracoDbDSN"].ToString());
                conn.Open();
                SqlCommand cmd;
                SqlDataReader rs;
                cmd = new SqlCommand("SELECT * FROM Booking WHERE Godkendt = 1 ORDER BY IndsendelseTidspunkt DESC", conn); //vælg kun godkendte bookinger
                rs = cmd.ExecuteReader();
                while (rs.Read())
                {
                    bookinger.Add(new Booking2
                    {
                        (...fancy assignment stuff...)
                    });
                }
                rs.Close();
                conn.Close();
                return bookinger;
            }
    

    The package uses PetaPoco for its default functionality, but I couldn't figure out how to make that work for me, so I created my own repository. My question is how I can go about adding a search option to start with. If I figure that out, hopefully I solve how to make sorting and pagination work as well. Let me know if you need to see more code samples.

Please Sign in or register to post replies

Write your reply to:

Draft