In this plunk I have two tables, one regular ng-table and an ng-table-dynamic with columns. Both point to the same data.
The second table doesn't show column titles, how to fix that?
HTML
<br/> Table 1
<table ng-table="tableParams" class="table table-bordered table-hover">
<tbody>
<tr ng-repeat="u in data" ng-dblclick="alert('double click')">
<td title="'User ID'">{{ u.uid }}</td>
<td title="'Group'">{{ u.ugr }}</td>
</tr>
</tbody>
</table>
<br/> Table 2
<table ng-table-dynamic="tableParams with cols" class="table table-bordered table-hover">
<tr ng-repeat="row in data">
<td title="col.nm" ng-repeat="col in cols">{{row[col.nm]}}</td>
</tr>
</table>
Javascript:
var app = angular.module('app', ['ngTable']);
app.controller('myCtl', function($scope,NgTableParams) {
$scope.cols = [ {nm:'uid'}, {nm:'ugr'} ];
$scope.data = [
{ uid: 'User 1',ugr: 'Group 1'},
{ uid: 'User 2', ugr: 'Group 2'}
];
$scope.tableParams = new NgTableParams({dataset: $scope.data});
});
You're missing the title property within your cols:
$scope.cols = [ {nm:'uid', title: 'User ID'}, {nm:'ugr', title: 'Group ID'} ];
See this plunkr.
Related
I have an issue with this library. First of all, to test the library, I made a simple example work with ul and li tags. It was straightforward. Then, I needed to do a table, and when converting my example to a table it didn't work.
Table is showed but I cannot move any row.
I'm using the cdn way.
I think is something what I missing.
html
<div id='app-example-drag' >
<table class="table table-striped">
<thead class="thead-dark">
<tr>
<th scope="col">Id</th>
<th scope="col">Name</th>
<th scope="col">Sport</th>
</tr>
</thead>
<draggable v-model="list" tag="tbody">
<tr v-for="item in list" :key="item.name">
<td scope="row">{{ item.id }}</td>
<td>{{ item.name }}</td>
<td>{{ item.sport }}</td>
</tr>
</draggable>
</table>
</div>
js
Vue.component('draggable',vuedraggable);
applicationExample = new Vue({
el: '#app-example-drag',
display: "Table",
order: 8,
data() {
return {
list: [
{ id: 1, name: "Abby", sport: "basket" },
{ id: 2, name: "Brooke", sport: "foot" },
{ id: 3, name: "Courtenay", sport: "volley" },
{ id: 4, name: "David", sport: "rugby" }
]
};
}
});
https://jsfiddle.net/0Luhd694/3/
Thanks in advance
I just ran into this same problem. I think it has something to with recent version. Replace the draggable element with a tbody and make is='draggable'.
<div id='app-example-drag' >
<table class='table'>
<thead>
<tr><th scope='col'>description</th></tr>
</thead>
<tbody tag='tbody' v-model='lista1' is='draggable' group='items'>
<tr v-for='item in lista1' :key='item.id'>
<td scope='row'>{{item.description}}</td>
</tr>
</tbody>
</table>
</div>
https://jsfiddle.net/oqf64kdx/
Let us assume we have following data structure:
var data = [
{
name: "item name",
nestedData: [{
name: "nested name",
quantity: 1
},
{
name: "nested name 2",
quantity: 2
}
]
},
{
name: "item name 2",
nestedData: [{
name: "nested name 3",
quantity: 3
}
]
}
];
Standard behavior of ng-repeat directive will iterate over high level elements. If we run ng-repeat="item in data" it will produce two items.
Is it possible - without using custom directive - to iterate over first item ("item name") twice (multiply it by a length of nestedData array)?
The output I'd like to achieve is:
<table>
<thead>
<th>Name</th>
<th>Nested name</th>
<th>Nested quantity</th>
</thead>
<tbody>
<tr>
<td rowspan="2">item name</td>
<td>nested name</td>
<td>1</td>
</tr>
<tr>
<td ng-hide="true">item name</td>
<td>nested name 2</td>
<td>2</td>
</tr>
<tr>
<td>item name 2</td>
<td>nested name 3</td>
<td>3</td>
</tr>
</tbody>
</table>
Nested ng-repeat is not suitable in this situation because there's a need to iterate over <tr>'s.
You can use a nested ng-repeat to get your desired result as it's valid HTML to have multiple tbody elements.
Here is a JSFiddle for a working example
<table>
<thead>
<th>Name</th>
<th>Nested name</th>
<th>Nested quantity</th>
</thead>
<tbody ng-repeat="item in data">
<tr ng-repeat="nestedItem in item.nestedData">
<td rowspan="{{item.nestedData.length}}" ng-hide="$index == 1">{{item.name}}</td>
<td>{{nestedItem.name}}</td>
<td>{{nestedItem.quantity}}</td>
</tr>
</tbody>
</table>
It's a different approach to achieve expected output.
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
var data = [
{
name: "item name",
nestedData: [
{
name: "nested name",
quantity: 1
},
{
name: "nested name 2",
quantity: 2
},
{
name: "nested name 3",
quantity: 3
}
]
},
{
name: "item name 2",
nestedData: [{
name: "nested name 3",
quantity: 3
}
]
}
];
var nestedData = [];
angular.forEach(data, function(item){
if(item.nestedData.length > 1){
angular.forEach(item.nestedData, function(nestedItem){
nestedData.push({
name : item.name,
nestedName: nestedItem.name,
nestedQty: nestedItem.quantity,
colspan: item.nestedData.length
});
});
} else {
nestedData.push({
name : item.name,
nestedName: item.nestedData[0].name,
nestedQty: item.nestedData[0].quantity
});
}
});
$scope.data = nestedData;
});
tr.multiple > td:first-child {
display: none;
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.0.0-alpha.6/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.6.1/angular.min.js"></script>
<div class="container" ng-app="myApp" ng-controller="myCtrl">
<table class="table table-stripped">
<thead>
<th>Name</th>
<th>Nested name</th>
<th>Nested quantity</th>
</thead>
<tbody>
<tr ng-repeat="item in data" ng-class="{'multiple': item.colspan > '1' && !$first}">
<td rowspan="{{item.colspan ? item.colspan : '1'}}">{{item.name}}</td>
<td>{{item.nestedName}}</td>
<td>{{item.nestedQty}}</td>
</tr>
</tbody>
</table>
</div>
I"m just starting to learn Vue and I'm having a little issue printing out the correct number of rows I should be getting for a table.
My table has 2 columns and 4 expected rows. When I test my code I get a table with 2 columns and one row with 4 column values using v-repeat or I get a table with 2 columns and 4 rows with the same information and 4 column values over 4 rows.
Basically trying to make a table that looks like this
Col 1 | Col2
row1 rData | rData
row2 rData | rData
row3 rData | rData
row4 rData | rData
The html
<table>
<thead id="tblHead">
<th v-for="item in items">
{{ item.message }}
</th>
</tr>
</thead>
<tbody id="tblInside">
<!-- <tr v-for="stuff in stuffs">
{{ stuff.message }}-->
<tr v-repeat="stuffsTD">
<td v-for="tdStuff in stuffsTD">
{{ tdStuff.message }}
</td>
</tr>
</tbody>
</table>
The Vue.js
var tbl = new Vue({
el: '#tblHead',
data: {
items: [
{ message: 'One' },
{ message: 'Two'}
]
}
})
var inTbl = new Vue({
el: '#tblInside',
data: {
stuffsTD: [
{ message: 'Row1 Col1 Plz' },
{ message: 'Row1 Col2 Plz' },
{ message: 'Row2 Col1 Plz' },
{ message: 'Row2 Col2 Plz' }
]
}
})
try this html:
<table>
<thead id="tblHead">
<th v-for="item in items">
{{ item.message }}
</th>
</tr>
</thead>
<tbody id="tblInside">
<tr>
<td v-for="tdStuff in stuffsTD">
{{ tdStuff.message }}
</td>
</tr>
</tbody>
</table>
Figured it out on my own. Have to use v-for on the tr element
<table id="tblData">
<thead>
<tr>
<th v-for="column in columns">
{{ column | uppercase }}
</th>
</tr>
</thead>
<tbody>
<tr v-for="tableData in tableData">
<td>
{{ tableData.client }}
</td>
<td>
{{ tableData.ad }}
</td>
<td>
{{ tableData.rt }}
</td>
</tr>
</tbody>
</table>
and
var tbl = new Vue({
el: '#tblData',
data: {
columns: [ 'some', 'thing', 'here' ],
tableData: [
{
some: 'A STORE',
thing: 'Summer',
here: '1:32'
},
{
some: 'Computer Store',
thing: 'fix',
here: '2:14'
},
{
some: 'Store 2',
thing: 'MTG',
here: '0:47'
}
]
}
})
I have a list of several objects and at the beginning I want to load just some of them. I took a look at the statement ng-repeat, but I need to have a structure like this:
<table class="table table-hover table-hidden">
<thead>
<tr>
...
</tr>
</thead>
<tbody>
<tr ng-repeat-start="object in objects | filter:query | orderBy:predicate:reverse" ng-init="isCollapsed=true">
...
</tr>
<tr ng-repeat-end class="more">
...
</tr>
</tbody>
</table>
I tried to apply the statement limitTo inside ng-repeat-start, in this way:
<tr ng-repeat-start="object in objects | filter:query | orderBy:predicate:reverse| limitTo: limit" ng-init="isCollapsed=true">
and on controller I wrote:
$scope.limit = 10;
$scope.incrementLimit = function () {
$scope.limit += 10;
};
The function incrementLimit is called by a click on hyperlink
<a href ng-click="incrementLimit()">more</a>
The list of objects is instantiated and filled (with all the elements) when the page loads.
With my approach at the beginnig are loaded and showed first 10 elements correctly, and when I click on "more" the variable $scope.limitis incremented, but on the page nothing happens. Anyone can explain me why?
limitTo is working fine inside ng-repeat-start
Take a look at this
var app = angular.module('myApp', []);
app.controller('Controller', function ($scope) {
$scope.limit = 1;
$scope.incrementLimit = function () {
$scope.limit += 1;
};
$scope.data = [{
title: 'Foo',
text: 'Lorem'
}, {
title: 'Bar',
text: 'Iste'
}, {
title: 'Jon',
text: 'Fat'
}, {
title: 'Los',
text: 'Ice'
}]
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.21/angular.min.js"></script>
<div ng-app='myApp' ng-controller="Controller">
<table border="2" class="table table-hover table-hidden">
<thead>
<tr>...</tr>
</thead>
<tbody>
<tr ng-repeat-start="object in data | filter:query | orderBy:predicate:reverse| limitTo: limit" ng-init="isCollapsed=true" ng-bind="object.title">...</tr>
<tr ng-repeat-end class="more">...</tr>
</tbody>
</table>
more
</div>
Please check the below code and fiddle it is working fine
<body ng-controller="myController">
<button ng-click="incrementLimit()" >increment</button> <br/>
<table class="table table-hover table-hidden">
<thead>
<tr clospan="3">
Head
</tr>
</thead>
<tbody>
<tr ng-repeat-start="friend in objects|limitTo: limit" ng-init="isCollapsed=true">
<td>{{friend.name}}</td>
<td>{{friend.phone}}</td>
<td>{{friend.age}}</td>
</tr>
<tr ng-repeat-end class="more">
<td colspan="3">Footer</td>
</tr>
</tbody>
</table>
</table>
</body>
js code
app = angular.module("app", []);
app.controller("myController", function($scope) {
$scope.objects =
[{
name: 'John',
phone: '555-1212',
age: 10
}, {
name: 'Mary',
phone: '555-9876',
age: 19
}, {
name: 'Mike',
phone: '555-4321',
age: 21
}, {
name: 'Adam',
phone: '555-5678',
age: 35
}, {
name: 'Julie',
phone: '555-8765',
age: 29
}];
$scope.limit = 2;
$scope.incrementLimit = function () {
$scope.limit += 1;
};
});
Fiddle http://jsbin.com/hoguxexuli/1/edit
I solved my problem. Basically I put the hyperlink in a different div, so the controller and the $scope were also different. It was a stupid carelessness.
Thanks in advance for the help. I am pretty new to ember.js, but have setup a basic site that has a few pages with tables that loop through my data. These tables are going to be over 100 rows and have images in each row, so I would like some sort pagination or a partial view so as they scroll it loads (preferred solution). I also want the ability to search the table.
I have used datatables.js before, and that would solve most of my problems, but I am not sure if that would be the best solution with ember because it is not able to just grab the table like it normally does.
I am kind of looking for a direction and what other people may think is the best solution. Any links, or plugins, or example code or whatever would be very helpful. Here are some snippets of my code, just a super basic site:
Index Table
<table class="prdtTable table-striped table-bordered table-hover">
<thead>
<tr>
<th>Title</th>
<th>Demo</th>
<th class="tdLeft">Price</th>
<th>Description</th>
<th># of Columns</th>
</tr>
</thead>
<tbody>
{{#each}}
<tr>
<td class="noWrap">{{#link-to 'theme' this}}{{title}}{{/link-to}}</td>
<td>{{#link-to 'theme' this}}<img {{bind-attr src="image"}} \>{{/link-to}}</td>
<td class="tdCenter">{{price}}</a></td>
<td>{{description}}</td>
<td class="tdCenter">{{columns}}</td>
</tr>
{{/each}}
</tbody>
</table>
Free Page Table
<table class="prdtTable table-striped table-bordered table-hover">
<thead>
<tr>
<th>Title</th>
<th>Demo</th>
<th>Description</th>
<th># of Columns</th>
</tr>
</thead>
<tbody>
{{#each}}
{{#if free}}
<tr>
<td class="noWrap">{{#link-to 'theme' title}}{{title}}{{/link-to}}</td>
<td>{{#link-to 'theme' this}}<img {{bind-attr src="image"}} \>{{/link-to}}</td>
<td>{{description}}</td>
<td class="tdCenter">{{columns}}</td>
</tr>
{{/if}}
{{/each}}
</tbody>
</table>
Index Route
App.IndexRoute = Ember.Route.extend({
model: function() {
return App.PRODUCTS;
}
});
Product Route
App.ProductRoute = Ember.Route.extend({
model: function(params) {
return App.PRODUCTS.findBy('title', params.title);
}
});
Product Array
App.PRODUCTS = [
{
title: 'Title 1',
price: '$0',
free: true,
description: 'long description',
columns: 1,
image: 'images/image.jpg'
},
{
title: 'Title 2',
price: '$0',
free: true,
description: 'Description 2',
columns: 3,
image: 'images/image.jpg'
},
...
Thank you very much for the help! I really appreciate it!
David B