Exemplo n.º 1
0
        public APIResult <Libs.DataTableResponse <sec_roles> > All(Requests.DataTableRequest request)
        {
            Libs.DataTableResponse <sec_roles> response = Libs.DataTableResponse <sec_roles> .getDataTable(Libs.General.getDataTabe(request, "sec_roles"));

            if (response.data == null)
            {
                return(APIResult <Libs.DataTableResponse <sec_roles> > .Error(ResponseCode.BackendInternalServer, "API_ERROR_BAD"));
            }

            return(APIResult <Libs.DataTableResponse <sec_roles> > .Success(response, "API_SUCCESS"));
        }
Exemplo n.º 2
0
        public static DataTableResponse <T> getDataTable(IEnumerable <T> list, IEnumerable <KeyValuePair <string, string> > querystring, Requests.DataTableRequest request)
        {
            DataTableResponse <Dictionary <string, object> > dataTableResponse = new DataTableResponse <Dictionary <string, object> >();
            DataTable dt = new DataTable();

            dt = list.ToDataTable();

            dataTableResponse.recordsTotal = list.Count();

            string whr = "";

            //foreach (var i in querystring)
            //{
            //    whr += (whr == "" ? "" : " and ") + i.Key + " = '" + i.Value + "'";
            //}

            if (request != null && request.search != null)
            {
                string subWhr = "";
                foreach (var item in request.columns.Where(a => a.searchable).ToList())
                {
                    subWhr += (subWhr == "" ? "" : " or ") + item.data + " like '%" + request.search.value + "%' ";
                }
                //subWhr += ")";
                whr += (whr == "" ? (subWhr == "" ? "" : subWhr) : (subWhr == "" ? "" : " and " + subWhr));
            }

            string orders = "";

            if (request != null && request.order != null && request.order.Count > 0)
            {
                foreach (var ord in request.order)
                {
                    if (request.columns[ord.column].orderable)
                    {
                        orders += (orders == "" ? "" : " , ") + request.columns[ord.column].data + " " + ord.dir;
                    }
                }
                //orders = (orders == "" ? "" : " order by ") + orders;
            }
            int length = 1000;
            int start  = 0;

            if (request != null)
            {
                length = (request.length == -1? length: request.length);
                start  = request.start;
            }

            List <DataRow> rows = dt.Select(whr, orders).ToList();

            dataTableResponse.recordsFiltered = rows.Count();

            rows = rows.Skip(start).Take(length).ToList();
            DataTable newdt = new DataTable();

            foreach (DataColumn c in dt.Columns)
            {
                newdt.Columns.Add(c.ColumnName, c.DataType);
            }
            foreach (var row in rows)
            {
                newdt.LoadDataRow(row.ItemArray, true);
            }
            dataTableResponse.data = General.DataTableToDictionary(newdt);


            Libs.DataTableResponse <T> d = getDataTable(dataTableResponse);


            return(d);
        }