I'm struggling to paginate using mongoid. I'm able to call paginate on my collection object like
#results = #collections.paginate :page => 1, :per_page => 10
but how it needs to be referred in view?
In my view I have rendered the collection in partial as
<%= render :collection => #collections, :partial => collect.xml %>
<%= will_paginate #results %>
In the above line I get the error undefined method total_pages for array.
But there is no error if I remove the will_paginate call in view and all the collections are shown in the view without pagination. Pls help.
I think I'm going wrong in calling the pagination helper in view. I have searched for quite a long time and did not find an example that included the pagination call in view for mongoid.
Forgive me if it is a dumb question. I'm new to mongoid.
This method call
#results = #collections.paginate :page => 1, :per_page => 10
will populate #results with an array of 10 items from #collections. You don't need to do use will_paginate at all, just use the #results object as is.
If you get problems with the view giving more than 10 results, debug the controller and the view to ensure there isn't something in between messing with your #results array.
EDIT:
Aha, so the problem is in displaying pagination links on the view. Yes, for that you would need the view to know things like the current page and the total number of pages available, so that you can compute how many links are necessary.
In return, your links would need to let the controller know what page is being requested, which you can achieve with GET request parameters. Helpers are probably the best way to keep the view clean.
Related
I have a database with user data, If I wanted to render a partial for each document in my MongoDB collection how would I do that?
I can render one partial by just adding it to my view but how do I make it render multiple ones automatically without me retyping this {{>somePartial}} for each new document added?
I looked around and found something suggesting AJAX they were not completely similar to my problem. If this is the solution is there another way other then AJAX?
every time a new document is added to MongoDB I'd like a new partial added to my view (I don't mind if it only shows when the page is refreshed)
{{>somePartial}} // Different data
{{>somePartial}} // Different data
{{>somePartial}} // Different data
You can make an array of all the data from your monogdb then you can render your page and pass your data to the ejs template and add dynamic includes and pass different data e.g.
<% for (let i = 1; i <= <length>; i++) { %>
<%- include("<partial>",{data:"your_data_for_this_index"}) %>
<% } %>
I very much new to Node JS and Express Framework & mongodb. I am working on a website which has multiple categories and when we click on category it goes to a view, where based on the category clicked, data gets loaded in the view.
please check the link for website here.
I am looking help for navbar links eg. Montessori Premium->Toddler Material
As these are static links I can not figure how to pass any parameter or url format for this.
I have done the routing for individual products as they have ids coming from database so I am using that id in url.
Thanks in advance.
It seems to me like you're just missing a bit of glue in between the front-end and the back-end database calls. Here's a rough idea of the sequence you might be after and an explanation of what I think you're missing.
If I'm wrong, please add a comment and update your question to include the new details and I'll see about editing my answer.
In this example I'm creating a website which has a list of "items" in a shop and we're looking to display them all on a single page. Each item should then have an a href clicking through to that specific item where there might be more detail such as a "full description" of the item and maybe some images or something. We're going to look at the flow to build that first page with the list of items.
The user makes a HTTP GET call to the root of your website such as "http://www.mystore.com/".
This request comes through in Express to your controller which takes the req, res, next parameters.
Your controller makes a call to the database to get a list of all items including their names and ID's.
You then use something called templating to inject in the list of items.
In this template you iterate through the list doing something like this.
.
<ul>
{{#each items}}
<li><a href="/items/{{id}}>{{name}}</a></li>
{{/each}}
</ul>
In this template (which happens to be Handlebars), we're going through the list of items and using the id and name of each to inject them into the HTML to get something a bit like this back.
<ul>
<li><a href="/items/1>Shoes</a></li>
<li><a href="/items/2>Red spotted dress</a></li>
<li><a href="/items/3>Jeans</a></li>
</ul>
This is then served up to the user once the template has been processed.
In essence I think you're after some sort of templating engine, I'd highly recommend having a look at Handlebars with something like the express-hbs.
Thank you #Elliot for your time to answer this. Yes I am using handlebars for templating. The solution I found is, because I am using static links, I am passing query string in anchor tag link.
eg. http://localhost:8000/product?category=Toddler_Material
and then using req.query for capturing the values (Toddler_Material) in router
var category = req.query.category;
I have a product package with several options in my Orchard site and I need users to be able to send an enquire based on a single option:
I've seen out there many articles about Tokens, Dynamic Forms (the examples using Dynamic Forms seem to be just for a single form type), I can think of many ways to achieve this but I don't know what is really the cleanest and fastest way to implement this functionality.
I would need a hint on what to keep researching to speed this up.
Thank you in advance
In your view, I'd wrap the button around an <a> tag something like this:
<a href="#(String.Format("/{0}/{1}", "x", y))" target="_blank">
//put button code or whatever here
</a>
Where 'x' is the extended url and y could be the query string such as: www.google.com/game/q=2
When you do this and get the redirect correct, in your driver for the page, you can get the page number from the url using something like this:
private readonly IHttpContextAccessor _httpContextAccessor;
var httpContext = _httpContextAccessor.Current();
var query = httpContext.Request.QueryString["q"];
This what i have in my controller
$projects=Project::where_sup_id($user_id)->get();
$total=count($projects);
$per_page=3;
$projects = Paginator::make($projects, $total, $per_page);
return View::make('project.index')->with('projects',$projects);
And this in my View
#foreach ($projects->results as $project)
{{$project->title}}
#endforeach
{{$projects->links();}}
but when i view it in the browser it is displaying all rows in all pages...the links are displaying perfectly! what do u think is the problem? please help! thank u in advance!
You are counting all rows, but not using limit at all, Laravel fluent query builder has skip() and take() method for that.
Btw, you don't need to manually paginate it. Laravel does pagination automatically with paginate() method.
Do it that way:
$projects = Project::where_sup_id($user_id)->paginate(3); // 3 means records per page
return View::make('project.index')->with('projects', $projects);
You are doing view work correctly.
I have a really strange problem trying to paginate custom posts types in their archives page, I created a custom post type called property and set an archive page called properties to show them all.
So inside my archive-property.php file added a form (GET method) with a dropdown box so users are able to set how many posts they want to display per page, it looks like this:
<select name="prop_number" id="prop_number">
<option value="12">12</option>
<option value="25">25</option>
<option value="50">50</option>
</select>
So before everything I do:
if (isset($_GET)) {
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$query_args = array(
'post_type' => 'property',
'paged' => $paged,
'posts_per_page' => 12 // Default number of properties per page
);
// Custom number properties per page
if (isset($_GET['prop_number']) && !empty($_GET['prop_number'])) {
$query_args['posts_per_page'] = $_GET['prop_number'];
}
}
query_posts($query_args);
// Do loop and other stuff
It works fine but when I use next_posts_link() to get to the second page I get a page not found 404 error, however if I go to my WP admin area and set the "Blog pages show at most" option to the same number I want per page in my select box (for example I manually set 25 on "Blog pages show at most" and select 25 in my prop_number select box) it does work fine.
I even tried using the WP-PageNavi plugin and it does render the correct amount of numbers depending on how many properties I want to show but I have the same problem 404 pages on any page I try to go unless I manually set the page in the backend.
If I didn't need the option to choose how many properties I want to show I'd just set the number manually but since users can choose between 3 options I can't set this to a fixed number in the backend.
Could anyone tell me what's wrong? Thanks in advance!
I'm not sure if you ever resolved your issue, but for the sake of anyone finding this question you may want to try flushing your permalink structure.
From http://codex.wordpress.org/Rewrite_API/flush_rules:
//Ensure the $wp_rewrite global is loaded
global $wp_rewrite;
//Call flush_rules() as a method of the $wp_rewrite object
$wp_rewrite->flush_rules();
Put that in your functions.php file, refresh your page, and see if that fixes your problem. It has for me in the past when I've had pagination 404 issues.
NOTE: once you have flushed your permalinks, remember to erase those two lines (see above) as you do not need or want that running every time a page is requested - it only needs to be done once. (This entire action may or may not be the same as going to the Permalinks page in your admin console.)