cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - ActiveLovehtmlicon-new-collectionicon-personicon-teamlog-outoctocatpop-outspinnerstartv

Pen Settings

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

Quick-add: + add another resource

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

Quick-add: + add another resource

Code Indentation

     

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

            
              <form class="form-horizontal">
						<div class=" form-group"> 
							<label class="control-label col-sm-3"> Favorite Teams:</label>
							<div class="col-sm-9">
								<div class=" list-group" data-bind="foreach: {data: selectedUser().List, as: 'dataItem'}">
									
								    	<div class="list-group-item">
								    	<span class="pull-right" data-bind="click: $parent.removeItem"><i class="glyphicon glyphicon-remove"></i></span>
								    		<span data-bind="text: ($index()+1) + '. ' + dataItem.name"></span>
								    	</div>
								</div>
							</div>
						</div>


						<div class="form-group"> 
							<label class="control-label col-sm-3">Add Team:</label>
							<div class="col-sm-9">
									
								<add-to-list params="list: $root.filteredItems(), optionsText: 'name', submit: addItem, disable: (selectedUser().List().length >= 5), limitMsg: 'Stop it! you have enough favorite teams.'">
								</add-to-list>
							</div>
						</div>

</form>

            
          
!
            
              form { max-width: 600px; margin: 0 auto;}
		.list-group-item {
			cursor: hand;
			border: 1px solid #222;
			box-shadow: inset 0 -2px 0 rgba(0,0,0,.5)
		}

		.list-group-item:hover {
			border-color: #2e6da4;
			background-color: #eaeaea;
		}

		.list-group-item i:hover {
			color: #f00;
		}
		.sortab
            
          
!
            
              		
ko.components.register('add-to-list', {
	viewModel: function(params) {

		this.selectedValue = ko.observable(null);

		this.list = params.list;

		this.limitMsg = params.limitMsg || "List limit reached";

		this.optionsText = params.optionsText || "Text";
		this.optionsValue = params.optionsValue || "ID";
		this.newValue = ko.observable(null);

		this.disable = params.disable;

		this.submitFn = params.submit || function() {console.warn('Add To List Widget: No submit function provided;'); };
		console.log('disable', this.disable());
		this.addToList = function() { 
			var value = this.selectedValue();

			this.selectedValue(null);

			this.submitFn(value);

		}.bind(this);

	},
	template: 
		'<div class="add-to-list-widget"><div class="row"><div class="col-sm-10">' +
		'<select class="form-control" data-bind="options: list(), value: selectedValue, optionsText: optionsText,  optionsCaption: \'Add to list...\', enable: !disable()"></select>' +
		'</div><div class="col-sm-2"><button class="btn" data-bind="enable: (selectedValue() && !disable()), click: addToList">+</button>' +
		'</div></div>' +
		'<div data-bind="visible: disable()"><strong style="color: red; text-align: center" data-bind="text: limitMsg"></strong>' +
		'</div>'
});


function Team(teamID, teamName) {
	this.ID = teamID;
	this.name = teamName;
}


function User(userObj) {
	var self = this;

	self.List = ko.observableArray([]);


}


function ViewModel(userObj) {
	var self = this;

	/* Array to hold data of list */
	self.allDataList = ko.observableArray([]);

	/* Object representing with selectedUser */
	self.selectedUser = ko.observable(new User(userObj));

	/* function to filter the main data list depending on what the user has selected */
	self.filteredItems = function() {
		return ko.utils.arrayFilter(self.allDataList(), function(listItem) {
			var match = -1;
			ko.utils.arrayForEach(self.selectedUser().List(), function(item) {
				if(item.ID == listItem.ID)
					match = 1;
			
			});
			return match <= 0;
		});
		
	};



	/* Function that grabs data list from remote source */
	self.loadList = function(callback) {
	};

	self.addItem = function(item) {
		console.log('adding team: ', item);
		self.selectedUser().List.push(item);
	};

	self.removeItem = function(obj) {
		//var index = self.favTeams.indexOf(obj);
		self.selectedUser().List.remove(obj);
	};

}


var myUser = {
	favTeams: []
};


$(function() {

	var vm = new ViewModel(myUser);
	ko.applyBindings(vm);
	

	$.ajax({
		//url: 'http://mlb.com/lookup/json/named.team_all.bam',
		url: 'https://raw.githubusercontent.com/stljeff1/portfolio/master/Javascript/add-to-list-knockout-component/mlb-teams.json',
		success: function(data) {
			var json = $.parseJSON(data);
			var teams = json.team_all.queryResults.row;
			var count = teams.length;
			teams.forEach(function(team) {
				vm.allDataList.push(new Team(team.team_id, team.name_display_long));
			});

		}
	});



});

            
          
!
999px
Loading ..................

Console