gpt4 book ai didi

javascript - Angular 数据表添加 "tree view"

转载 作者:行者123 更新时间:2023-11-28 08:00:53 25 4
gpt4 key购买 nike

我需要一些帮助,如何修改 ng 表以添加树功能 - 以使其他嵌入式 JSON 数组可在主节点下扩展。

<html>
<head>
<script data-require="angular.js@*" data-semver="1.2.0-rc3-nonmin" src="http://code.angularjs.org/1.2.0-rc.3/angular.js"></script>
<script data-require="ng-table@*" data-semver="0.3.1" src="http://bazalt-cms.com/assets/ng-table/0.3.1/ng-table.js"></script>

<link data-require="ng-table@*" data-semver="0.3.1" rel="stylesheet" href="http://bazalt-cms.com/assets/ng-table/0.3.1/ng-table.css" />
<link data-require="bootstrap-css@*" data-semver="3.0.0" rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap.min.css" />

<link rel="stylesheet" href="style.css" />
<script src="script.js"></script>
</head>

<body ng-app="main" ng-controller="DemoCtrl">


<button ng-click="tableParams.sorting({})" class="btn btn-default pull-right">Clear sorting</button>
<p><strong>Sorting:</strong> {{tableParams.sorting()|json}}

<table ng-table="tableParams" class="table">
<tr ng-repeat="user in $data">
<td data-title="'Name'" sortable="'name'">
{{user.name}}
</td>
<td data-title="'Age'" sortable="'age'">
{{user.age}}
</td>
</tr>
</table>

</body>
</html>

然后在我想要修改 JSON 数据源的地方编写代码:

var app = angular.module('main', ['ngTable']).
controller('DemoCtrl', function($scope, $filter, ngTableParams) {
var data = [{name: "Moroni", age: 50},
{name: "Tiancum", age: 43},
{name: "Jacob", age: 27},
{name: "Nephi", age: 29},
{name: "Enos", age: 34},
{name: "Tiancum", age: 43},
{name: "Jacob", age: 27},
{name: "Nephi", age: 29},
{name: "Enos", age: 34},
{name: "Tiancum", age: 43},
{name: "Jacob", age: 27},
{name: "Nephi", age: 29},
{name: "Enos", age: 34},
{name: "Tiancum", age: 43},
{name: "Jacob", age: 27},
{name: "Nephi", age: 29},
{name: "Enos", age: 34}];

$scope.tableParams = new ngTableParams({
page: 1, // show first page
count: 10, // count per page
sorting: {
name: 'asc' // initial sorting
}
}, {
total: data.length, // length of data
getData: function($defer, params) {
// use build-in angular filter
var orderedData = params.sorting() ?
$filter('orderBy')(data, params.orderBy()) :
data;

$defer.resolve(orderedData.slice((params.page() - 1) * params.count(), params.page() * params.count()));
}
});
});

这个例子在这里:[ http://plnkr.co/edit/5SQ2YB?p=info][1]

我的自定义 JSON 如下所示:

[{
"name": "Tiancum",
"age": "43",
"favoriteCars": [{
"name": "Audi",
"engines": [{
"type": "diesel",
"size": "big"
},
{
"type": "gas",
"size": "small"
}]
},
{
"name": "Subaru",
"engines": [{
"type": "diesel",
"size": "big"
},
{
"type": "gas",
"size": "small"
}]
}]
},
{
"name": "JAcob",
"age": "54",
"favoriteCars": [{
"name": "BMW",
"engines": [{
"type": "diesel",
"size": "big"
},
{
"type": "gas",
"size": "small"
}]
},
{
"name": "VW",
"engines": [{
"type": "diesel",
"size": "big"
},
{
"type": "gas",
"size": "small"
}]
}]
}]

我想要实现的是使嵌入的 JSON 数组可扩展,如下所示:[http://jsfiddle.net/eu81273/8LWUc/18/][2]

有什么快速解决这个问题的想法吗?

最佳答案

[
{
"BodyId": 1,
"TrId": null,
"TdId": null,
"Description": "Text Text"
},
{
"BodyId": 2,
"TrId": null,
"TdId": null,
"Description": "Text Text"
},
{
"BodyId": 3,
"TrId": null,
"TdId": null,
"Description": "Text Text"
},
{
"BodyId": 1,
"TrId": 1,
"TdId": null,
"Description": "Text Text"
},
{
"BodyId": 1,
"TrId": 2,
"TdId": null,
"Description": "Text Text"
},
{
"BodyId": 2,
"TrId": 1,
"TdId": null,
"Description": "Text Text"
},
{
"BodyId": 2,
"TrId": 2,
"TdId": null,
"Description": "Text Text"
},
{
"BodyId": 3,
"TrId": 1,
"TdId": null,
"Description": "Text Text"
},
{
"BodyId": 3,
"TrId": 2,
"TdId": null,
"Description": "Text Text"
},
{
"BodyId": 1,
"TrId": 1,
"TdId": 1,
"Description": "Text Text"
},
{
"BodyId": 1,
"TrId": 1,
"TdId": 2,
"Description": "Text Text"
},
{
"BodyId": 1,
"TrId": 2,
"TdId": "1",
"Description": "Text Text"
},
{
"BodyId": 2,
"TrId": 1,
"TdId": 1,
"Description": "Text Text"
},
{
"BodyId": 2,
"TrId": 1,
"TdId": 2,
"Description": "Text Text"
},
{
"BodyId": 2,
"TrId": 2,
"TdId": 1,
"Description": "Text Text"
},
{
"BodyId": 3,
"TrId": 3,
"TdId": 1,
"Description": "Text Text"
}
]

所以给出这个 JSON,我试图实现这个结构:

Data Structure

感谢所有帮助!

关于javascript - Angular 数据表添加 "tree view",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25410860/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com