Number of Items
- Boris
-
Topic Author
- Offline
- New Member
-
Less
More
- Posts: 5
- Thank you received: 0
1 year 4 months ago #1
by Boris
Number of Items was created by Boris
Hi there!
Is there a chance to get the actuall number of items via JS within the index.php of the template I am using?
Reason why asking:
• I want to have a badge over a special icon I am using on MyShortlist
• the icon comes not from your module and it comes several times / depending on the viewport / from different other modules
• I just need to know, how the get the actual number via JS (not JQuery) / DOM and CSS shouldn't be the problem at my end
• In this certain project, we are using just one list and this only for not-logged-in users / so, only one certain cookie needs to be checked
•• if it is existing
•• and if so, how many numbers of items the user have in the list
But the value of the used cookie is not clear to me and if it contains the number of items ...
Sure you can help me out wir this one!
Best,
Boris
Is there a chance to get the actuall number of items via JS within the index.php of the template I am using?
Reason why asking:
• I want to have a badge over a special icon I am using on MyShortlist
• the icon comes not from your module and it comes several times / depending on the viewport / from different other modules
• I just need to know, how the get the actual number via JS (not JQuery) / DOM and CSS shouldn't be the problem at my end
• In this certain project, we are using just one list and this only for not-logged-in users / so, only one certain cookie needs to be checked
•• if it is existing
•• and if so, how many numbers of items the user have in the list
But the value of the used cookie is not clear to me and if it contains the number of items ...
Sure you can help me out wir this one!
Best,
Boris
Please Log in or Create an account to join the conversation.
- Christopher Mavros
-
- Offline
- Administrator
-
1 year 4 months ago - 1 year 4 months ago #2
by Christopher Mavros
Christopher Mavros
me@mavxr.com
If you like our extensions, please rate us on the JED!
Replied by Christopher Mavros on topic Number of Items
Hello Boris and thank you for posting.
Currently, there's no existing foundation to support that for every save method that MyShortlist allows.
Our modules do load everything around with JS/Ajax, but you can't really get the number of items alone.
I could theoretically add that, as long as you provide the cookie prefix with your Javascript request to the component's ajax controller.
Now, the quick and dirty answer. If you only use the non-logged in cookie save method, you can check the cookies with Javascript and get the number of items.
MyShortlist's cookie is simply called myshortlist, and anything you add as a list ID (cookie prefix) is prepended to that, so it becomes prefixheremyshortlist.
Here's some Javascript code that could probably do what you're asking for.
I have not tested the above code, so let me know if you face any difficulties with it.
Currently, there's no existing foundation to support that for every save method that MyShortlist allows.
Our modules do load everything around with JS/Ajax, but you can't really get the number of items alone.
I could theoretically add that, as long as you provide the cookie prefix with your Javascript request to the component's ajax controller.
Now, the quick and dirty answer. If you only use the non-logged in cookie save method, you can check the cookies with Javascript and get the number of items.
MyShortlist's cookie is simply called myshortlist, and anything you add as a list ID (cookie prefix) is prepended to that, so it becomes prefixheremyshortlist.
Here's some Javascript code that could probably do what you're asking for.
Code:
function countItems(cookiePrefix) {
const cookieName = cookiePrefix + 'myshortlist';
const cookieMatch = document.cookie.match(new RegExp('(^|;)\\s*' + cookieName + '\\s*=\\s*([^;]+)'));
if (cookieMatch) {
const cookieValue = decodeURIComponent(cookieMatch[2]);
if (cookieValue === '') { return 0; }
const semicolonCount = (cookieValue.match(/;/g) || []).length;
const itemTotal = semicolonCount + 1;
return itemTotal;
} else {
return 0;
}
}
// Example usage:
const totalItems = countItems("");
console.log('Total items of the default list:', totalItems);
const totalItemsPrefixList = countItems("prefixhere");
console.log('Total items of prefixhere list:', totalItemsPrefixList);
I have not tested the above code, so let me know if you face any difficulties with it.
Christopher Mavros
me@mavxr.com
If you like our extensions, please rate us on the JED!
Last edit: 1 year 4 months ago by Christopher Mavros.
Please Log in or Create an account to join the conversation.
- Boris
-
Topic Author
- Offline
- New Member
-
Less
More
- Posts: 5
- Thank you received: 0
1 year 4 months ago - 1 year 4 months ago #3
by Boris
Replied by Boris on topic Number of Items
Hey Christopher,
many thanks for this one!
It's a good one to start with …
… but the value of the cookie doesn't include semikolon
via 'CustomBlocks' it contains '%23mx%235%3B%23mx%234' by having two items in the list.
But I come back later to this again … first to solve a different issue … I will start anotehr thread for this on.
EDIT: '16-153-%3B15-153-' the value of the cookie with two items without CustomBlocks
many thanks for this one!
It's a good one to start with …
… but the value of the cookie doesn't include semikolon
via 'CustomBlocks' it contains '%23mx%235%3B%23mx%234' by having two items in the list.
But I come back later to this again … first to solve a different issue … I will start anotehr thread for this on.
EDIT: '16-153-%3B15-153-' the value of the cookie with two items without CustomBlocks
Last edit: 1 year 4 months ago by Boris. Reason: Other value of the cookie added
Please Log in or Create an account to join the conversation.
- Christopher Mavros
-
- Offline
- Administrator
-
1 year 4 months ago - 1 year 4 months ago #4
by Christopher Mavros
Christopher Mavros
me@mavxr.com
If you like our extensions, please rate us on the JED!
Replied by Christopher Mavros on topic Number of Items
You are correct.
%3B is the entity code for semi-colons.
I will update the earlier response to use decodeURIComponent.
I also fixed a problem at line const semicolonCount = ...
I just checked the code and it works for me.
%3B is the entity code for semi-colons.
I will update the earlier response to use decodeURIComponent.
I also fixed a problem at line const semicolonCount = ...
I just checked the code and it works for me.
Christopher Mavros
me@mavxr.com
If you like our extensions, please rate us on the JED!
Last edit: 1 year 4 months ago by Christopher Mavros.
Please Log in or Create an account to join the conversation.
Moderators: Christopher Mavros