I am developing an app using HTML5, Knockouk and Jquery.
The UI presents a list of Orders. At first it will show 5 orders, and when a user clicks on a "Show More" button, the next 5 orders will show.
When an Order is clicked, I will show all the activities that are related to that Order and I will be showing the first 10 activites and so on when the user clicks on the "More" button.
I have created a small example based on the requirements. I have created the first step, but I'm not able to create step 2. I am unsure how to create a nested observable array. I have added a sample, which is working for step but not for the second step.
<h1>Using Observable Arrays<br /></h1>
<div id="divActivites">
<!-- ko foreach: Activities -->
<div data-bind="click:$root.showDesc, attr:{'name':name,'id':id}">
<td><span data-bind="text:name"></span></td>
<td><span data-bind="text:id"></span></td>
</div>
<!-- /ko -->
<input type="button" value="Show More" data-bind="click:addData" />
</div>
<div id="divActDec" data-bind="with:selectedData ">
<span data-bind="text:id"></span><br />
<span data-bind="text:name"></span><br />
<span data-bind="text:randomStringDesc()"></span>
</div>
<script type="text/javascript">
function ActivityListModel() {
var self = this;
var total = ko.observable();
self.Activities = ko.observableArray([]);
self.selectedData = ko.observable();
total = 0;
self.addData = function () {
var addtoArray = 5;
var finalVal = total + addtoArray;
for (var i = total+1; i <= finalVal; i++) {
self.Activities.push(
{
name: randomString(),
id: i
}
)
}
total = finalVal;
}
self.showDesc = function (ActivitiesDesc) {
$("#divActivites").hide();
$("#divActDec").show();
self.selectedData(ActivitiesDesc);
}
}
function ActivitiesDesc(data) {
var activitydesc = this;
activitydesc.name = data.name;
activitydesc.actid = data.id;
}
function randomString() {
var value = "";
var letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
for (var i = 0; i < 10; i++) {
value += letters.charAt(Math.floor(Math.random() * letters.length));
}
return value;
}
function randomStringDesc() {
var value = "";
var letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
for (var i = 0; i < 150; i++) {
value += letters.charAt(Math.floor(Math.random() * letters.length));
}
return value;
}
ko.applyBindings(new ActivityListModel());
</script>
Can someone tell me how to create ActivitiesDesc object as an observable array?
Thanks, Jollyguy