Hey guys, there is a limitation in MYSQL, where you use group by and wanted max value, but when you try to fetch a row ID or any other column, it fails.
For example, there is one table on the left side called “potential_customers”, and on the right side, I wrote one query which returns maximum NEXT_CALL value for each customer_id.
So, in current example I wanted row ID 2 & 6.
But if I need ID or whole row, it fails even when you use order by column_name DESC.
Thankfully there is a workaround for this which is to use the combination of subquery and concat.
First, let’s alter the query, to concat customer_id and max of next call. Remove all order by if there is any.
Put that query is sub-query by adding rounded brackets around it.
Now, write a select query before sub query, & in where condition, use concat of required column. Don’t forget to use “IN” keyword.
And as you can see, it returns row ID that we need.
You can call whole column if you want.
So that’s it. Code link is given in the description.
I have made a code which will generate a scrollable pagination feature in Flutter App. Today I am going to be sharing it’s code with you guys. Let’s get started.
The tutorial is divided into 2 phases.
1st is displaying data in data table.
2nd is adding scrollable pagination at bottom.
To execute the phase 1, what we shall do is a copy-paste from my blog. The link is given in the description with a title as “Link 1 Code (Data table)”
Instructions are given, we just have to follow it.
In new project, we shall add a dependency.
Make sure that it is at the same level as a flutter text.
Then, you can download that dependency using the command.
Copy-paste the whole code and paste it in main.dart file.
Let’s run the project.
You will see the data in data-table which itself is a scrollable.
The data is coming from the Rest API.
Each row is sending the total number of rows in count. Every API may send total row count in different key and at difference places or won’t send it at all so take a note to make sure it sends a total number of rows.
Lets begin a phase 2.
We already created a code to get total pages which will be shown at the bottom of an APP page.
Here, we will add another singlechildscrollview.
We will allow it to scroll horizontally.
Add a row as a child, advantage is that we can add multiple children in it which will be our pagination buttons.
The sizedbox is required, let me tell you why in a minute.
Let’s add a width in it.
Here you can see that the text ABC is appearing the right side of a screen.
We will do some mathematics to make it at the level of a data table.
Then I will be using a for loop.
Inside that will be our page buttons.
We will be using container with some specific width. This width will be dynamic, depending upon the length of a page number. I shall explain it in a minute.
For a button, we will be using a RaisedButton as a child.
Inside RaisedButton, we will temporarily add a 1 text.
OnPressed function is necessary.
Button appears.
We shall temporarily change the page count as 10 in for loop.
We will create a function called numberFunction which will be deciding the width of every button.
Let’s create that function which will return a double value.
Whenever the length of a page number changes, so will the float amount of that button.
Now the width looks OK.
Add i+1 as a text which will accurately display page number.
On pressed, we will change the current page number to highlight it.
Let’s make number’s color as white.
Because there are only 15 records, there are only 2 pages.
So let’s make 2 records per page.
We will add sizedBox at the end of container too to make some spacing from the right side.
To make some gap between buttons, we shall add margin as 10 from right side.
Set height property as 50.
To change content when clicked on button, call the loadDAtaFromAPI method on button’s onPressed function.
In pagination, there is always a feature will tells users the current page by highlighting it.
So let’s change the color of current page.
Because it is dynamic, we shall create another function called getColorForThis.
It will return a color by checking if that page number matches with current one.
There is some error.
So the return type should be MaterialColor.
Let’s run the project again.
You will see the pagination as expected. It is scrollable and go up to infinite.
There is a link in description called “Link 2 Code (Final Pagination)”. You can refer that if your code is not executing as expected.
Enjoy the code, play with it to create your own version.
The biggest pros of using session in flutter or any other language is that you can pass values between 2 or multiple pages without form submission or setting it hidden field.
For example, there are 2 pages in flutter application, the PLUS button below increases the count on that page, I can set that value in session and pass it down in 2nd page and vice versa.
I shall show how to do the setup and how to implement session in flutter.
Let’s get started.
In basic project of flutter application, there is always a main.dart. file with a button which will increase the count. And I also added 2 raised-buttons manually in design to redirect to second page and to destroy session.
I also created another page called newPage.dart with some session code pre-applied, but it shows error because I haven’t added session library yet.
To add libraries or dependencies, pubspec.yaml file is used.
However, we are going add session library using commands.
So, let’s go to browser and search “flutter session”.
Click on installing tab.
There you will find command to install it in dependencies.
Run that command.
Reopen pubspec file, you will find flutter_session_manager line, which means the library is installed.
And there is no error in newPage.dart file.
Let’s go to main.dart file.
I shall enable code to redirect to second page.
To make session library actually work in dart file, we have to import it.
If we go to Readme tab, you will find session code, like how to create session variable.
I shall use 1st and 3rd line.
Actually I created some code. So just copy-paste it. Code link is given in the description.
From 1st point, copy the code and paste it below _counter variable.
This code is used for fetching session variable at the page-start.
SessionManager.get function is used for it.
To set the session variable “count”, sessionManager.set function is used.
Use it after the setState function.
Let’s run the project.
Let’s test the code.
From first page, I shall set counter value to 4.
And that value is successfully called in 2nd page.
From this page, if I change it, it will be reflected in 1st page.
We can also destroy session by using destroy function.
Or we can unset it.
So, in second raised-button, I shall call remove function.
var str_array = $("#userTable_info").html().split(" ");
for(var i=0;i<str_array.length;i++)
{
if(!isNaN(str_array[i]))
{
if(parseInt(str_array[i])>actual_cnt)
{
str_array[i] = actual_cnt;
}
}
}
$("#userTable_info").html(str_array.join(" "));
$("#userTable_info").show();
}
$("#userTable").on('page.dt',function(){
$("#userTable_info").hide();
setTimeout(function(){
pageCountDatatable();
},1000);
});
$("#userTable").on('length.dt',function(){
$("#userTable_info").hide();
setTimeout(function(){
pageCountDatatable();
},1000);
});
</script>
-------------------------------
In this tutorial, I shall show how we can overwrite the count of result in data table.
Here, we have one data table with 20 rows of actual data.
In this data table, I just wanted to add extra row which will represent additional column and will not be the part of the actual data.
So, actually I wanted to display 20 entries as a text instead of 21 entries.
So for that, I shall create a javascript function.
I am going to call it inside initComplete function.
For now, we add static value of actual row count as 20 rows.
Of course you can get that count dynamically.
Just inspect element, and find out the ID for that div.
Copy it.
We shall use it’s ID to get text inside it.
We shall split it by space. And convert it into an array.
In for loop, first we shall find out whether the specific array position has numeric value to it or not because we don’t want to replace texts. Then, we shall only replace numbers and those numbers which are greater than 20.
After the end of the loop, we shall overwrite the text in same ID.
So, if we refresh, we shall see that it is showing “20 entries” text.
However if we change the length per page, or change pages, it will again go back to “21 entries” text.
So for that, first copy the table ID, on its page change event, we shall write some jquery.
If we change page number, it shows an alert box, but it won’t change the text even if we call function.
So to fix that, we have to delay it’s execution by using the setTimeout function.
The time delay will be 1 second.
Call our function inside it.
However the output behaves weirdly so we shall hide it the page changes.
And we shall show it when the output becomes ready.
To display same output when page length is changed, copy-paste the whole code.
And replace “page” with “length”.
And now, your output will display no more than 20 entries text.
You can copy-paste the code from the link given in the description.