Esempio n. 1
0
 public dynamic Paging(DatatablesPaging datatablesPaging)
 {
     return(_serviceSysFuctionTest.PagingFilter(datatablesPaging));
 }
Esempio n. 2
0
        /// <summary>
        /// Phân trang - có tìm kiếm theo từng column và tìm kiếm chung cho tất cả columns( text search )
        /// </summary>
        /// <param name="datatablesPaging"></param>
        /// <param name="filter"></param>
        /// <returns></returns>
        public dynamic PagingFilter(DatatablesPaging datatablesPaging, FilterDefinition <T> filter = null)
        {
            // https://www.codewithmukesh.com/blog/jquery-datatable-in-aspnet-core/
            filterFinal = filterGlobal;

            if (filter != null)
            {
                filterFinal &= filter;
            }

            if (datatablesPaging == null)
            {
                return(null);
            }

            datatablesPaging.PageSize = Convert.ToInt32(datatablesPaging.Length);
            //default Ascending Sort by Code !

            datatablesPaging.SortColumn = char.ToUpper(datatablesPaging.SortColumn[0]) + datatablesPaging.SortColumn.Substring(1);
            SortDefinition <T> v_Sort = Builders <T> .Sort.Ascending(datatablesPaging.SortColumn ?? "Code");

            //nếu có chỉ định column cần sort và chỉ thị sort (asc, desc) thì làm theo
            if (!String.IsNullOrEmpty(datatablesPaging.SortColumnDirection) &&
                datatablesPaging.SortColumnDirection.Equals("desc", StringComparison.OrdinalIgnoreCase))
            {
                v_Sort = Builders <T> .Sort.Descending(datatablesPaging.SortColumn ?? "Code");
            }

            //lấy tổng số documents có trong collection
            //ước lượng dựa vào metadata nên nó sẽ chạy nhanh hơn (lượng data lớn), nhưng nếu có nhiều cluster thì có thể không chính xác
            long totalDocuments = _collection.EstimatedDocumentCount();

            List <T> result = new List <T>();

            //lấy tất cả những document đang active

            if (!string.IsNullOrEmpty(datatablesPaging.SearchValue))
            {
                datatablesPaging.SearchValue = "/.*" + datatablesPaging.SearchValue + ".*/";
                try
                {
                    //trùng tên sẽ tự động không tạo nữa, nhưng khác tên mà trùng key ($**) thì báo lỗi exception
                    IndexKeysDefinition <T> keys = Builders <T> .IndexKeys.Text("$**");

                    var options = new CreateIndexOptions()
                    {
                        DefaultLanguage = "english",
                        Name            = "TextIndex"
                    };
                    var indexModel = new CreateIndexModel <T>(keys, options);
                    _collection.Indexes.CreateOne(indexModel);
                    // https://www.codeproject.com/Articles/524602/Beginners-guide-to-using-MongoDB-4-0-2-and-the-off
                    // https://mongodb.github.io/mongo-csharp-driver/2.11/getting_started/admin_quick_tour/#list-the-databases
                    // https://viblo.asia/p/tim-hieu-ve-index-trong-mongodb-924lJL4WKPM
                    // use CreateIndexModel
                }
                catch (Exception) { }

                //nếu có điều kiện tìm kiếm (searchValue có giá trị) thì lọc theo điều kiện trước khi lấy
                var v_search = Builders <T> .Filter.Text(datatablesPaging.SearchValue, new TextSearchOptions { CaseSensitive = false });

                filterFinal &= v_search;
            }

            //tìm kiếm xem có filter theo column hay không, nếu có thì add thêm filter vào
            if (datatablesPaging.SearchArray != null && datatablesPaging.SearchArray.Count > 0)
            {
                foreach (KeyValuePair <string, string> pair in datatablesPaging.SearchArray)
                {
                    filterFinal &= new BsonDocument {
                        { pair.Key, new BsonDocument {
                              { "$regex", pair.Value }, { "$options", "i" }
                          } }
                    };
                }
            }

            //lấy tổng số documents sau khi đã lọc theo điều kiện tìm kiếm
            long recordsFiltered = _collection.CountDocuments(filterFinal);

            if (datatablesPaging.PageSize == -1)
            {
                result = _collection.Find(filterFinal)
                         .Sort(v_Sort)
                         .ToList();
            }
            else
            {
                result = _collection.Find(filterFinal)
                         .Skip(Convert.ToInt32(datatablesPaging.Start))
                         .Limit(datatablesPaging.PageSize)
                         .Sort(v_Sort)
                         .ToList();
            }

            if (recordsFiltered == 0)
            {
                recordsFiltered = totalDocuments;
            }

            //trả về dữ liệu theo chuẩn paging của datatable.net
            dynamic resjs = new
            {
                draw         = datatablesPaging.Draw,
                recordsTotal = recordsFiltered, // totalDocuments
                recordsFiltered,
                data = result
            };

            return(resjs);
            // https://stackoverflow.com/questions/58072703/jsonresultobject-causes-the-collection-type-newtonsoft-json-linq-jtoken-is
            // use return Json(dynamic type)
        }
 public dynamic Paging(DatatablesPaging datatablesPaging)
 {
     return(_serviceCustomerTest.PagingFilter(datatablesPaging));
 }