I have a form containing an ng-repeat, with some input hidden field that i need to pass to the server.
This is the code of the page:
<form ng-controller="FrmController" method="post">
<div class="orderlist">
<table class="table gas">
<thead>
<tr>
<th scope="col" ng-click="orderByField='image'; reverseSort = !reverseSort">Immagine
<span ng-show="orderByField == 'image'" class="listorder">
<span ng-show="!reverseSort"><i class="fa fa-sort-desc"></i> </span>
<span ng-show="reverseSort"><i class="fa fa-sort-asc"></i></span>
</span>
</th>
[...]
</thead>
<tbody id="gasbody" ng-repeat="product in products | filter: searchQuery | orderBy:orderByField:reverseSort" class="repeated-item">
<tr>
<input type="hidden" name="form-x-id" ng-model="form.form-x-id">
(x represent the index of the ng-repeat)
[...]
<button ng-click="addtoCart();" class="def-btn" type="submit"><span class="glyphicon glyphicon-shopping-cart"></span> Add to cart</button>
</form>
This is the code of the controller:
function FrmController($scope,$http,transformRequestAsFormPost){
$scope.form = {};
$scope.addtoCart = function() {
$http({
method: 'POST',
url: '',
data: $.param($scope.form),
headers: {'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'}
});
}
}
I need to send data in this format:
form-1-id: value
form-2-id: value2
form-3-id: value3
$scope.form = {form-1-id: value,
form-2-id: value2,
form-3-id: value3, ...
... form-n-id: valuen}
where n represent the lenght of ng-repeat.
ecc....
How can i dynamically add field to a $scope binded to a ng-model?
Is it possible?
Thanks
I dont underestant exactly , what do you want ?
Do you want index of ng-repeat?
You can just use $index like this
<input name="form-{{$index}}-id" type="Hidden" ng-model="form[$index]">
Or do you want to push something to your form dynamically? You can use:
$scope.addToForm=function(newItem){
$scope.form.push(newItem)
}
Of course you have to change your form={} , to form=[]
Related
I am able to drag the content but its dropping in the same location instead of intended location.
My goal is to drop at specific location,which isn't happening.Need some help or suggestion on this!!
My test
it('moves the piece when dragged to valid place', function () {
cy.get('[data-cy="dataExplorer" ]').find('ux-accordion-panel').first().click();
cy.get('[data-cy="metricContainer"]').first()
.trigger('mousedown', { button: 0 })
.wait(1000)
.trigger('mousemove', {force: true, x: -370, y: 180, bubbles: false})
.trigger('mouseup', {force: true})
});
My html
<table class="table table-hover">
<tbody uxTabbableList>
<tr *ngFor="let field of fields; let i=index" uxTabbableListItem>
<td class="metric-group" (mouseenter)="activeRowId = field.id"
(mouseleave)="activeRowId = null" [attr.aria-expanded]="activeRowId === field.id"
[cdkDropListConnectedTo]="['mashup-dashboard']" cdkDropList>
<div cdkDrag (cdkDragDropped)="dragDropped($event, field)" data-cy="metricContainer" class="metric-container">
<div data-cy="metricName" class="metric-name">
<div>{{field.name}}</div>
<div *cdkDragPlaceholder class="drop-preview-placeholder"></div>
<div *cdkDragPreview>{{field.name}}</div>
</div>
<div class="add-button-container" [class.flt-show]="activeRowId === field.id">
<button type="button" aria-label="Edit" class="btn button-primary"
[tabindex]="activeRowId === field.id ? 0 : -1" (click)="addMetricPanel(field);">
</button>
</div>
</div>
</td>
</tr>
</tbody>
</table>
Hey guys I'm really new at sails.js and I'm using version 1.0 of it. I just tried to create a CRUD application on adding article to my list and showing them. All goes right but when I want to delete or edit it says 404 error. I am going through this tutorial series and as I know this series is using sails js version 0.12.
I am using mongodb as my database as a default. Data insertion is going well but problem is when I wanted to edit or delete it. Here I think the problem is getting the id value from the url. I skipped the edit portion here and posted the delete portion here.
api/controllers/ ArticleController.js
module.exports = {
delete: function(req, res){
Articles.destroy({id:req.params.id}).exec(function(err){
if(err){
res.send(500,'Database Error');
}
res.redirect('/articles/list');
});
return false;
}
};
api/models/ Articles.js
module.exports = {
attributes: {
title:{
type:'string'
},
body:{
type:'string'
}
},
};
views/pages list.ejs
<h1 class="display-4">Articles</h1>
<table class="table table-striped">
<thead>
<tr>
<th>#</th>
<th>Title</th>
<th></th>
</tr>
</thead>
<tbody>
<% articles.forEach(function(article){ %>
<tr>
<td><%= article.id %></td>
<td><%= article.title %></td>
<td>
<form class="d-inline" action="/articles/delete/<%= article.id %>" method="POST">
<input type="submit" class="btn btn-danger" value="Delete">
</form>
</td>
</tr>
<% }) %>
</tbody>
</table>
config/route.js
module.exports.routes = {
'/': {
view: 'pages/homepage'
},
};
I wanted that when I change my selection in my checkbox the only selected checkbox will be selected and the previous selected checkbox will be uncheck.
My code is working but i really wanted to select only one checkbox.
In my view.
<center><table></center>
<tr>
<th><center>
List of Names</center></th>
<th colspan="3">
Actions
</th></tr>
<tr ng-repeat="role in roles">
<td>
<label>
<ion-checkbox ng-model="isChecked" ng-
change="format(isChecked,role,$index)"
ng-init="isChecked=false"><div class="wew">
{{role}}
</div></ion-checkbox>
</label>
</td>
<td>
<button ng-hide="!isChecked" ng-click="present()">P </button> </td>
<td>
<button ng-hide="!isChecked" ng-click="late()">L</button></td>
<td><button ng-hide="!isChecked" ng-click="absentss()">A</button></td>
<td><button ng-hide="!isChecked" ng-click="delete()">D</button></td>
</tr> </table>
And I think in my controller is the part where i need to change my codes to achieved the correct result.
$scope.isChecked = false;
$scope.selected = [];
$scope.format = function (isChecked, role, index) {
if (isChecked==true) {
$scope.selected.push(role);
}
else {
var _index = $scope.selected.indexOf(role);
$scope.selected.splice(_index, 1);
}
var students = $scope.selected;
console.log(students);
for( var s=0; s<students.length; s++) {
$scope.stud = [
students[s]
]
};
Thank you in advanced! I hope that someone can help in this matter.
Checkboxes are for multiple selections within a group.
Use radio buttons instead.
Example With AngularJs
This is way you can also implement.
<!DOCTYPE html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
<script>
angular.module('app', []).controller('appc', ['$scope',
function($scope) {
$scope.selected = 'other'; //default selection
}
]);
</script>
</head>
<body ng-app="app" ng-controller="appc">
<label>SELECTED: {{selected}}</label>
<div>
<input type="checkbox" ng-checked="selected=='male'" ng-true-value="'male'" ng-model="selected">Male
<br>
<input type="checkbox" ng-checked="selected=='female'" ng-true-value="'female'" ng-model="selected">Female
<br>
<input type="checkbox" ng-checked="selected=='other'" ng-true-value="'other'" ng-model="selected">Other
</div>
</body>
</html>
I'm going to do my best to describe what I'm attempting to do, and what I've done, but, please let me know if you need more information to provide help.
What I'm trying to do is create an html table that has 5 columns and any number of rows. It's a very basic order form that will list literature that we have for sale. The first 4 columns provide information about the item, and the 5th column is a simply text input "qty" field that allows a user to enter the "qty" of the item they wish to purchase. You can see what this looks like here: http://web.go-spi.com/cgl/literature/.
It's important to note that the table is generated dynamically, via a custom loop that I've created in Wordpress. I did it this way to allow us to dynamically add or remove literature items from the table.
The code:
<div class='gf_browser_chrome gform_wrapper' id='gform_wrapper_6' >
<a id='gf_6' name='gf_6' class='gform_anchor' ></a>
<form method='post' enctype='multipart/form-data' target='gform_ajax_frame_6' id='gform_6' action='/cgl/literature/#gf_6'>
<div class='gform_body'>
<div id='gform_fields_6' class='gform_fields top_label description_below'>
<?php $literature_query = new WP_Query( array( 'post_type' => 'literature', 'showposts' => '50', 'orderby' => 'meta_value', 'meta_key' => 'ecpt_brochurerow', 'order' => 'ASC'));
if( $literature_query->have_posts() ):?>
<table cellpadding="0" cellspacing="0" border="0" class="table table-striped table-bordered" id="data">
<thead>
<tr>
<th>Brochure Type</th>
<th><label class='gfield_label' for='input_6_1'>Description</label></th>
<th>Cost</th>
<th>Qty/Pack</th>
<th><label class='gfield_label' for='input_6_2'>Qty</label></th>
</tr>
</thead>
<tbody>
<?php while( $literature_query->have_posts() ): $literature_query->the_post();?>
<tr>
<td><?php echo get_post_meta($post->ID, 'ecpt_brochuregroup', true); ?></td>
<td><?php echo get_post_meta($post->ID, 'ecpt_brochuredescription', true); ?><input type='hidden' class='gform_hidden' name='input_1' id='input_6_1' value="<?php echo get_post_meta($post->ID, 'ecpt_brochuredescription', true); ?>"/></td>
<td><?php echo get_post_meta($post->ID, 'ecpt_brochurecost', true); ?></td>
<td><?php echo get_post_meta($post->ID, 'ecpt_brochureqtyperpack', true); ?></td>
<td><input name='input_2' id='input_6_2' type='text' value='' class='medium'/></td>
</tr>
<?php endwhile;
wp_reset_postdata();
?>
</tbody>
</table>
<?php else: ?>
<div class="alert alert-block">
<p>Sorry, there is currently no literature available. Please check back later.</p>
</div>
<?php endif; ?>
</div>
</div>
<div class='gform_footer top_label'>
<input type='submit' id='gform_submit_button_6' class='button gform_button' value='Submit' tabindex='1' /><input type='hidden' name='gform_ajax' value='form_id=6&title=&description=' />
<input type='hidden' class='gform_hidden' name='is_submit_6' value='1' />
<input type='hidden' class='gform_hidden' name='gform_submit' value='6' />
<input type='hidden' class='gform_hidden' name='gform_unique_id' value='5077469db6b44' />
<input type='hidden' class='gform_hidden' name='state_6' value='YToyOntpOjA7czo2OiJhOjA6e30iO2k6MTtzOjMyOiJjMDlmZjFlZmU1MjMzN2M3M2FkODliMmVjY2Y5YzIxMyI7fQ==' />
<input type='hidden' class='gform_hidden' name='gform_target_page_number_6' id='gform_target_page_number_6' value='0' />
<input type='hidden' class='gform_hidden' name='gform_source_page_number_6' id='gform_source_page_number_6' value='1' />
<input type='hidden' name='gform_field_values' value='' />
</div>
</form>
</div>
<iframe style='display:none;width:0px; height:0px;' src='about:blank' name='gform_ajax_frame_6' id='gform_ajax_frame_6'></iframe>
<script type='text/javascript'>function gformInitSpinner_6(){jQuery('#gform_6').submit(function(){if(jQuery('#gform_ajax_spinner_6').length == 0){jQuery('#gform_submit_button_6').attr('disabled', true).after('<' + 'img id="gform_ajax_spinner_6" class="gform_ajax_spinner" src="/cgl/wp-content/plugins/gravityforms/images/spinner.gif" alt="" />');jQuery('#gform_wrapper_6 .gform_previous_button').attr('disabled', true); jQuery('#gform_wrapper_6 .gform_next_button, #gform_wrapper_6 .gform_image_button').attr('disabled', true).after('<' + 'img id="gform_ajax_spinner_6" class="gform_ajax_spinner" src="/cgl/wp-content/plugins/gravityforms/images/spinner.gif" alt="" />');}} );}jQuery(document).ready(function($){gformInitSpinner_6();jQuery('#gform_ajax_frame_6').load( function(){var contents = jQuery(this).contents().find('*').html();var is_postback = contents.indexOf('GF_AJAX_POSTBACK') >= 0;if(!is_postback){return;}var form_content = jQuery(this).contents().find('#gform_wrapper_6');var is_redirect = contents.indexOf('gformRedirect(){') >= 0;jQuery('#gform_submit_button_6').removeAttr('disabled');var is_form = !(form_content.length <= 0 || is_redirect);if(is_form){jQuery('#gform_wrapper_6').html(form_content.html());jQuery(document).scrollTop(jQuery('#gform_wrapper_6').offset().top);if(window['gformInitDatepicker']) {gformInitDatepicker();}if(window['gformInitPriceFields']) {gformInitPriceFields();}var current_page = jQuery('#gform_source_page_number_6').val();gformInitSpinner_6();jQuery(document).trigger('gform_page_loaded', [6, current_page]);}else if(!is_redirect){var confirmation_content = jQuery(this).contents().find('#gforms_confirmation_message').html();if(!confirmation_content){confirmation_content = contents;}setTimeout(function(){jQuery('#gform_wrapper_6').replaceWith('<' + 'div id=\'gforms_confirmation_message\' class=\'gform_confirmation_message_6\'' + '>' + confirmation_content + '<' + '/div' + '>');jQuery(document).scrollTop(jQuery('#gforms_confirmation_message').offset().top);jQuery(document).trigger('gform_confirmation_loaded', [6]);}, 50);}else{jQuery('#gform_6').append(contents);if(window['gformRedirect']) {gformRedirect();}}jQuery(document).trigger('gform_post_render', [6, current_page]);} );} );</script><script type='text/javascript'> jQuery(document).ready(function(){jQuery(document).trigger('gform_post_render', [6, 1]) } ); </script>
The problem:
1) I'm not sure how to dynamically set the input name and id so that they are sequential as you go down the form. Currently, as the code is set up, I'll only receive the submitted value of the first input no matter how many inputs are filled in.
I appreciate any help you can provide. Thanks!
I have applied bindings to the following view model
var groupDeleteViewModel = {
group: { Id: ko.observable(), Name: ko.observable(), Members: ko.observableArray() },
load: function (item) {
debugger
},
remove: function (item) {
groupDeleteViewModel.group.Id(item.Id());
groupDeleteViewModel.group.Name(item.Name());
groupDeleteViewModel.group.Members(item.Members());
$("#groupDelete").show();
},
cancel: function () {
$("#groupDelete").hide();
}
}
the remove function loads the view with the data that has been passed in item to the remove function.
<div id="groupDelete" class="pop-window filter-view">
<h2>
Delete Group
</h2>
<table>
<tr>
<th>
Name
</th>
<td>
<span data-bind="text:group.Name" />
</td>
</tr>
<!--ko foreach: group.Members-->
<tr>
<th>
</th>
<td>
<div data-bind="text:Name" class="grey-border">
</div>
</td>
<td>
</td>
</tr>
<!--/ko-->
</table>
<span class="centeralign">
<input type="button" value="Delete" data-bind="click: remove" class="delete" />
<input type="button" value="Cancel" data-bind="click: cancel" />
</span>
</div>
I don't want to keep mapping each element of item to each element of groupDeleteViewmodel.group. I have done it at a lot of other places also in the code which has made the code really messy. I want to use the ko.mapping plugin to do the same thing.
Till now what I have tried is -
remove:function (item){
var data = ko.mapping.toJS(item);
ko.mapping.fromJS(data, groupDeleteViewModel.group);
$("#groupDelete").show();
}
But this just does not work. i really don't know why. It should have worked ideally.
Can someone tell what is the right way of using ko.mapping in such a case?
Thanks.
The mapping plugin is looking for mapping data already present in groupDeleteViewModel.group, but it doesn't find it, because you didn't initialize groupDeleteViewModel.group using the mapping plugin.
So instead of initializing group like you did:
group: { Id: ko.observable(), Name: ko.observable(), Members: ko.observableArray() }
initialize it using the mapping plugin:
group: ko.mapping.fromJS({ Id: undefined, Name: undefined, Members: [] })
And this is me fiddling around with your code: fiddle