Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Discuss: idiomatic way of displaying size of a filtered collection #239

Open
PK1A opened this issue Jul 10, 2014 · 6 comments
Open

Discuss: idiomatic way of displaying size of a filtered collection #239

PK1A opened this issue Jul 10, 2014 · 6 comments

Comments

@PK1A
Copy link
Contributor

PK1A commented Jul 10, 2014

Consider a typical example of an expression where we want to have a data table that is filtered, paginated and sorted:

{foreach itemIdx, item in ctrl.tableItems | filter:ctrl.search | sorter.sort | limit:limitBoundaries.from:limitBoundaries.count}
            <tr>
                <td>{item.CustomerName}</td>
               ...
            </tr>
{/foreach}

Now, we would like to display the number of filtered elements. In the TODO MVC example we can see one approach:

syncData : function () {
. While it works it is rather cumbersome to write. Ideally I would like to be able have this logic in the template only. 2 proposals:

  • {(ctrl.tableItems | filter:ctrl.search).length}
  • {foreach itemIdx, item in (filtered = ctrl.tableItems | filter:ctrl.search) | sorter.sort | limit:limitBoundaries.from:limitBoundaries.count} and then {filtered.length}
@PK1A
Copy link
Contributor Author

PK1A commented Jul 10, 2014

I've tried {let filteredCollection = (ctrl.tableItems | filter:ctrl.search)} but this doesn't seem to do the trick either.

@benouat
Copy link
Member

benouat commented Jul 10, 2014

I also encountered this issue, and I have to admit that even the work around are not that easy to implement...

We have to provide something.

@b-laporte
Copy link
Member

I am surprised the {let} option doesn't work - this is a bug

@b-laporte
Copy link
Member

I just tried the following in the "list ordering" sample of the playground and it works:

    <div class="msg">
            {let nameSorter=new Sorter({property:"name",states:"ADN"})}
            <span class="info">
                List sorted with a Sorter object (current state: {nameSorter.state})
            </span>
            <ol>
                {let list=persons|nameSorter}
                {foreach p in list}
                    <li>{p.name}</li>
                {/foreach}
                Number of items:{list.length}
            </ol>
            <a onclick="{nameSorter.nextState()}">Change sort order</a>
        </div>

So are you sure the {let} version didn't work?

@PK1A
Copy link
Contributor Author

PK1A commented Jul 10, 2014

Hmm, it might have to do with the place where I've put {let} - just before its usage and after {foreach}. I will re-test it as soon as I can get the application back to the usable state :-)

In any case what I love to be able to avoid the let statement - supporting {(ctrl.tableItems | filter:ctrl.search).length} would be ideal.

@PK1A
Copy link
Contributor Author

PK1A commented Jul 10, 2014

@b-laporte ok, here is what happens with {let}:

  • dropping () makes the expression compile: {let filteredCollection = ctrl.tableItems | filter:ctrl.search}. With parenthesis ({let filteredCollection = (ctrl.tableItems | filter:ctrl.search)}) I was getting the "Invalid expression" error.
  • After it compiles the display works OK the first time but the expression doesn't get evaluated when the filtering expression (ctrl.search) changes

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants