0

I am trying to fetch data from gridmvc and show graphs using chart.js its working fine but issue is that its showing just with pages. Because i have enabled paging in grid and when i click on next page then next grid data page graphs show, but i want to show graph of complete grid data includes all pages.

     <div class="panel-body">

            @await Html.Grid(Model).Columns(columns =>

       {

           columns.Add(c => c.ID).Titled("StudentID").Filterable(true);

           columns.Add(c => c.Name).Titled("Name").Filterable(true);

           columns.Add(c => c.Major).Titled("Major").Filterable(true);

           columns.Add(c => c.Minor).Titled("Minor").Filterable(true);
           columns.Add(c => c.Email).Titled("Email").Filterable(true);

           columns.Add(c => c.Address).Titled("Address").Filterable(true);
           columns.Add(c => c.GPA).Titled("GPA").Filterable(true);


       }).Searchable(true, false, true).WithPaging(10).ChangePageSize(true).Sortable(true).EmptyText("No data found").Named("GridSearch").RenderAsync()

        </div>

Javascript

               function LoadChart() {
              debugger;
              var chartType = parseInt($("#rblChartType input:checked").val());
              var items = $(".grid-mvc").find(".grid-table > tbody").children();
              var json = [];
              $.each(items, function (i, row) {

                  $col1=$(row).children()[0].innerText;
                  $col2 = $(row).children()[1].innerText;

                  $col3 =$(row).children()[2].innerText;

                  $col4 =$(row).children()[3].innerText;
                  $col5 =$(row).children()[4].innerText;

                  $col6 =$(row).children()[5].innerText;
                  $col7 =$(row).children()[6].innerText;

                  json.push({ 'StudentID': $col1, 'Name': $col2, 'Major': $col3, 'Minor': $col4, 'Email': $col5, 'Address': $col6, 'GPA': $col7      
              })     
     // Map JSON values back to label array
              var labels = json.map(function (e) {
                  return e.Name;
              });
              console.log(labels); // ["2016", "2017", "2018", "2019"]

              // Map JSON values back to values array
              var values = json.map(function (e) {
                  return e.GPA;
              });
              var chart=BuildChart(labels, values, "Students Name by GPA");

I want to show graphs which include complete data in gridmvc not just on current page.

1 Answers1

0

but issue is that its showing just with pages. Because i have enabled paging in grid and when i click on next page then next grid data page graphs show, but i want to show graph of complete grid data includes all pages.

   var items = $(".grid-mvc").find(".grid-table > tbody").children();
              var json = [];
              $.each(items, function (i, row) {

                  $col1=$(row).children()[0].innerText;
                  $col2 = $(row).children()[1].innerText;

                  $col3 =$(row).children()[2].innerText;

                  $col4 =$(row).children()[3].innerText;
                  $col5 =$(row).children()[4].innerText;

                  $col6 =$(row).children()[5].innerText;
                  $col7 =$(row).children()[6].innerText;

                  json.push({ 'StudentID': $col1, 'Name': $col2, 'Major': $col3, 'Minor': $col4, 'Email': $col5, 'Address': $col6, 'GPA': $col7      
              })     

The issue relates the above scripts, since you implement paging, when using the above code to get the table resource, it only gets the current page records, then display the page grahps.

To solve this issue, you could get the records from the page model (Model) or create an action method to get all records, then, use JQuery Ajax method to call this method and get the grid data.

To get records from the page model, in your Asp.net Core MVC application, you could use the Json.Serialize() method to convent the Model to JSON string first, then use JSON.parse() method convent the JSON string to JavaScript Object, then loop through the Object and get all data.

Code like this (Index.cshtml)

@model List<StudentViewModel>
@section Scripts{
    <script>
        $(function () {
            LoadChart();
        });

        function LoadChart() {
            debugger;
            //var chartType = parseInt($("#rblChartType input:checked").val());
            //var items = $(".grid-mvc").find(".grid-table > tbody").children();
            var json = [];
            var items = JSON.parse('@Json.Serialize(Model)');
            $.each(items, function (index, item) {
                json.push({ 'StudentID': item.id, 'Name': item.name, 'Major': item.major, 'Major': item.major, 'Email': item.email, 'Address': item.address, 'GPA': item.gpa });
            });

            //show graphs based on the json array.

The screenshot like this:

enter image description here

Zhi Lv
  • 18,845
  • 1
  • 19
  • 30
  • Thanks for response and provided the solution,its working fine and loading all, but issue is when i apply filter on `gridmvc` its showing few record according to filter but graphs showing all, i want to show graphs also show according to apply filter as well – Abdul Hameed Apr 05 '21 at 09:07
  • Well i think we need to get complete data from table because table data changing according to search and filter, if we use `model` its showing all the records but not according to filter and search. Hope you got it – Abdul Hameed Apr 05 '21 at 09:10
  • OK, I understand that. To solve this issue, after getting all data from the Model, I suggest you could also use the [`Array.filter()` method](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter) to filter data according to the search and filter, you can refer this link: [How to filter JSON Data in JavaScript or jQuery?](https://stackoverflow.com/questions/23720988/how-to-filter-json-data-in-javascript-or-jquery). After that, display the graphs based on the filtered data. – Zhi Lv Apr 05 '21 at 09:22
  • But i am using `gridmvc` filter which show filter automatcially, how can i apply `gridmvc` filter to json data? – Abdul Hameed Apr 05 '21 at 10:03
  • Try to use check the gridmvc document, whether there has some method to get the filter parameters. Or you could try to use JQuery to get the filter value, then using it to filter the json data. – Zhi Lv Apr 06 '21 at 08:49
  • but how can i do this? In documentation its not contains javascript information, also regarding to filters, this is not just 1-2 filters, its so many filters in each column like contains, equal,greater then, less then ,also sorting, searching, there are 7 columns, how can i manage this type of filters? Better solution is get just complete data from table which show and apply graph – Abdul Hameed Apr 06 '21 at 09:48
  • Can you share the gridmvc document that you are referring? "Better solution is get just complete data from table which show and apply graph" Try to use F12 developer tools to check the html elements, since you are using paging, the html table only contains the current page and the filter data, so if we directly loop through the html table, you can only get part of the record. So, I think it might not a good solution, we should find a way to get the filtered data. – Zhi Lv Apr 07 '21 at 01:24
  • Thanks for response , yeah here is link for official documentation [Link](https://github.com/gustavnavar/Grid.Blazor/blob/master/docs/dotnetcore/Documentation.md) also you are right, please provide me solution for above this, – Abdul Hameed Apr 07 '21 at 04:18
  • Yes, you are right the official document doesn't explain about this function. Since this issue relates GridMVC, I suggest you could post this question on the [GridMVC forum](https://github.com/gustavnavar/Grid.Blazor/issues), then, the package developer might tell us whether there have any solution or workaround. – Zhi Lv Apr 08 '21 at 09:03