问题
I am using angular datatable to create tables and following is my working plnkr - http://plnkr.co/edit/pQ0TrNEjzyXmvFcIvkSr?p=preview
Here I want to merge two column data to one, i.e. to show Address 1 & Address 2 as Address something like - addr1 - addr2 and also (2) to show image in table column instead of link.
I tried -
DTColumnBuilder.newColumn('addr1' - 'addr2').withTitle('Address 1'),
and DTColumnBuilder.newColumn('addr1 - addr2')
but no luck(throwing error)
Please help me with this. Thanks.
My Script -
var dd = [];
dd = [
{"Img": "http://img.banggood.com/thumb/other_items/upload/2012/liangping/animal%20head%20masks-011%20(4).jpg", "Name": "Tiger Nixon", "Age": "61", "addr1": "234 My addr 1", "addr2": "234 My addr 2"},
{"Img": "http://a.deviantart.net/avatars/a/n/animal-animes.png", "Name": "Garrett Winters","Age": "63", "addr1": "235 My addr 1", "addr2": "235 My addr 2"}
];
$scope.dtColumns = [
DTColumnBuilder.newColumn('addr1').withTitle('Address 1'),
DTColumnBuilder.newColumn('addr2').withTitle('Address 2'),
DTColumnBuilder.newColumn('Img').withTitle('Image'),
DTColumnBuilder.newColumn('Name').withTitle('Name'),
DTColumnBuilder.newColumn('Age').withTitle('Age')
];
$scope.dtOptions = DTOptionsBuilder.newOptions()
.withOption('data', dd);
回答1:
You have to use renderWith on your column angular-datatable documentation
$scope.dtColumns = [
DTColumnBuilder.newColumn('addr1').withTitle('Address 1'),
DTColumnBuilder.newColumn('addr2').withTitle('Address 2'),
DTColumnBuilder.newColumn(null).withTitle('Full Address').renderWith(addressHtml),
DTColumnBuilder.newColumn('Img').withTitle('Image'),
DTColumnBuilder.newColumn('Name').withTitle('Name'),
DTColumnBuilder.newColumn('Age').withTitle('Age')
];
$scope.dtOptions = DTOptionsBuilder.newOptions()
.withOption('data', dd);
function addressHtml(data, type, full, meta) {
return data.addr1 + ' - ' + data.addr2;
}
the updated plunker : http://plnkr.co/edit/T2XMUFxORSy3z7dDgRcc?p=preview
You can add image if you need ...
来源:https://stackoverflow.com/questions/33053739/how-to-combine-data-and-show-image-in-angular-datatable