I am learning cakePHP 1.26.
I got a HTML Select tag with some options like this:
<form method="post" action="/testing">
<table border="1">
<tr>
<td>
<select name="data[Test][number]">
<option name="editquote" value="[29,1]">One</option>
<option name="editquote" value="[24,2]">Two</option>
</select>
</td>
<tr>
<td>
<input type="submit" value="Send" class="mybutton">
</td>
</tr>
</table>
</form>
I selected option One and submitted the form.
Here is the result from the cakePHP built-in function, Debug()
Array
(
[Test] => Array
(
[number] => [29,1]
)
)
I tried using the following code to get the two numbers from the data (i.e. 29 and 1 in this example) but failed to do it
$myData=$this->data;
$myData['Test']['number']; // [29, 1]
What should I do to get the two numbers separately?
You could try this with PHP explode.
$numbers = explode(',', trim($myData['Test']['number'], '[]'));
$numbers[0]; //29
$numbers[1]; //1
Related
I have to update the value of multiple Inputs using ng-repeat. i usually get the value with JQuery $(class/id).val() and get its value. but now i have no idea how to access the input values since i only have one id for each. (i have like 20 input in the table)
View:
<tr ng-repeat="i in list">
<td><input list="itemNames" class="item_name" ng-model="i.item_name" value="{{i.item_name}}" type="text"/></td>
<datalist id="itemNames">
<option ng-repeat="ii in list" class="idI" ng-model="ii.idI" data-item="{{ii.idI}}" value="{{ii.item_name}} {{ii.idI}}">
</datalist>
<td><input class="quantity" ng-model="i.quantity" value="{{i.quantity}}" type="number"/></td>
<td><input class="price" ng-model="i.price" value="{{i.price}}" type="number"/></td>
<tr>
<td ng-click="updateAll()">UPDATE</td>
</tr>
</tr>
I expect to store all values in an arrays, but what i got is only values of the first row.
JS:
$scope.updateAll=function(){
// getting vallues
var item_name=$(".item_name").val();
var quantity=$(".quantity").val();
var price=$(".price").val();
}
I think your list is a scope variable. So, in controller its defined as $scope.list. You need not to think about id as you are using angular code, angular framework will do it for you.
As you used the two way data binding with ng-model, so any change to the bind variable will be immediately visible to controller and html view.
The use of ng-model ensures the immediate update to the $scope.list variable.
For example, if you add any text for "item_name" in the control index 0 from html view, the variable $scope.list[0].item_name will be automatically updated, also the change in $scope.list[0].item_name = "Some name" in controller will be automatically reflected in the view.
So, your given code can be re-written as following:
<tr ng-repeat="i in list">
<td><input list="itemNames_{{$index}}" class="item_name" id="txt_name_{{$index}}" ng-model="selectedValue" ng-change = "getSelectedId(selectedValue, $index)" type="text"/></td>
<datalist id="itemNames_{{$index}}">
<option ng-repeat="ii in list" class="idI" ng-model="ii.idI" data-item="{{ii.idI}}" value="{{ii.item_name}} {{ii.idI}}">
</datalist>
<td><input class="quantity" ng-model="i.quantity" value="{{i.quantity}}" type="number"/></td>
<td><input class="price" ng-model="i.price" value="{{i.price}}" type="number"/></td>
<tr>
<td ng-click="updateAll()">UPDATE</td>
</tr>
The javascript method can be written as:
var getSelectedId = function(selectedValue, index) {
var val = document.getElementById('txt_name_' + index).value;
var text = $('#itemNames_' + index).find('option[value="' + val + '"]').attr('data-item');
alert(text);
}
i have a table with ng-repeat where it has a colunm with filter as follows:
<tbody>
<tr ng-repeat="objin listOfObj |filter: searchObj">
{{ (listAllElmt | filter:{idElt: obj.id})[0].nameElt || ''}}
</tr>
</tbody>
the search part is done with the following instruction:
<label >Elmt label</label>
<input type="text" ng-model="searchObj.id">
Here the user must write in the input the id in order that filter:searchObj return a result, however, it must write the name which is a logic way and not putting the id.
How can i fix this issue?
I used another variable where i created it in html as follows:
<label >Elmt label</label>
<input type="text" ng-model="searchObj.id">
<tbody>
<tr ng-repeat="obj in listOfObj |filter: searchObj">
{{objInterm = obj;
objInterm.id = (listAllElmt | filter:{idElt: obj.id})[0].nameElt || '';
;''}}
{{ objInterm.id}}
</tr>
</tbody>
I'm using AngularUI's ui-mask directive to mask phone numbers. We'd like to save both the masked and unmasked value to the database.
I see the docs have the option to use the model-view-value="true" to store the $viewValue as the model giving us the masked value.
However since we'd like both I'd prefer a way to leave this false and access the $viewValue on my own. I see in the demo however this is for a single input. It looks like it is bound to the name of the inputs.
My issue is this is a ng-repeat and so the names vary. I got it working for a single item (you can see in the first table it works).
The last <td> is where I'm trying to show the $viewValue. I'm also trying to pass it in to the setPhoneValue function but it is undefined there also.
Edit: Created a fiddle: https://jsfiddle.net/n35u7ncz/
<form name="phone_form" id="crm_phonegrid" ng-app="crm_phonegrid" ng-controller="phoneCtrl">
<table cellpadding="2">
<tr>
<td><input type="text" name="new_phone" placeholder="Number" ng-model="addNumber.phoneNumber" onfocus="onFocusUpdateMask()" ui-mask="{{mask}}" ui-mask-placeholder ui-mask-placeholder-char="_" /></td>
<td><select name="phoneTypeSelect" ng-model="addNumber.phoneType" ng-options="option.name for option in phoneTypeOptions track by option.Value"></select></td>
<td><input type="button" ng-click="addNumber()" value="Add" /></td>
<td>{{phone_form.new_phone.$viewValue}}</td>
<td>{{addNumber.phoneNumber}}</td>
</tr>
</table>
<table cellpadding="2">
<thead>
<tr>
<th>Phone Link</th><th>Phone Number</th><th>Phone Type</th><th>Primary Flag</th><th>Mask</th><th>View</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="number in numbers">
<td><a style="color:#1160B7;text-underline-position:below" href="" ng-click="openPhoneRecord(number)">{{ number.crm_phonenumber }}</a></td>
<td><input type="text" id="crm_phonegrid_number" name="phone_number_input" model-view-value="true" ng-model="number.crm_phonenumber" ng-change="setPhoneValue(number)" ui-mask="{{number.crm_crm_country_crm_phonenumber.crm_PhoneMask}}" ui-mask-placeholder ui-mask-placeholder-char="_" /></td>
<td><select name="phoneTypeSelect" ng-model="number.crm_phonetypecode" ng-change="setValue(number)" ng-options="option.name for option in phoneTypeOptions track by option.Value"></select></td>
<td><input type="radio" name="primary" id="primaryRadio" ng-checked="number.crm_isprimary" ng-model="number.crm_isprimary" ng-change="setValue(number)" ng-value="number.crm_isprimary" ng-click="setFlag(number)" /></td>
<td>{{number.crm_crm_country_crm_phonenumber.crm_PhoneMask}}</td>
<td>View: {{phone_form.phone_number_input.$viewValue}}</td>
</tr>
</tbody>
</table>
</form>
We got this working. The trick was to use the $index to generate a unique Id for the element and then pass that to the controller.
I've updated the fiddle here: https://jsfiddle.net/n35u7ncz/3/
The general idea is that the input field needs to be tagged and have an ng-change:
<input type="text" id="phone_input_id" name="phone_input_name_{{$index}}" ng-model="number.crm_phonenumber" ng-change="setValue(number, $index)" ui-mask="{{number.crm_crm_country_crm_phonenumber.crm_PhoneMask}}" ui-mask-placeholder ui-mask-placeholder-char="_" />
And then:
$scope.setValue = function(item, index)
{
item.crm_phonenumbermasked = $scope.phone_form["phone_input_name_" + index].$viewValue;
}
I want to set default value of e.name to "ALL" using ng-init
Here is my TEMPLATE code :
<tr ng-repeat="e in employees" ng-init="employees[0] = 'ALL'">
<td>{{ $index + 2}}</td>
<td>{{ e.name}}</td>
<button type="button" ng-click="select(employee)">
Select
</button>
</td>
</tr>
DIRECTIVE link
scope.employees = EmployeeService.query();
Here I want to set default value of e.name to ALL before generating dynamic data
I am getting dynamic data properly in employees and {{ e.name}} from directive
but here I want to show ALL in same <td> before getting dynamic data
If you're just trying to display ALL before data is retrieved, you could consider using an or operator.
<tr ng-repeat="e in employees">
<td>{{$index + 2}}</td>
<td>{{e.name || 'ALL'}}</td>
<button type="button" ng-click="select(employee)">
Select
</button>
</td>
</tr>
However, if you actually need ALL to be a value, I would strongly suggest setting it to the default value where ever you model employees.
I have a create page in which there is a textbox and dropdown, I want to create a item for each dropdown value. After creation of single item it redirects to edit page from there we have to go to create page and create item with second dropdown value iteratively.
I am trying to iterate over the dropdown values and trying to create item under each condition but the test exists after the intial item creation and it does not create item for second dropdown value.
describe('When create case form is valid', function(){
it('Should be creating cases', function(){
browser.get("xyz.com");
browser.waitForAngular();
element(by.id("create")).click();
element.all(by.options('caseSubType.specID as caseSubType.specName for caseSubType in vm.newCaseSubTypes')).each(function(elem, index) {
elem.getText().then(function (text) {
elem.click();
element(by.model('vm.newCaseSummary')).sendKeys('test');
$('button[ng-disabled="!vm.hasClientIDForNewCase()"]').click();
browser.waitForAngular();
browser.get("xyz.com");
browser.waitForAngular();
element(by.id("create")).click();
})
})
});
});
HTML for the create page is:
<table>
<tr>
<td>Summary</td>
<td><input type="text" class="form-control input-sm" id="txtNewCaseSummary" ng-model="vm.newCaseSummary" ng-disabled="!vm.hasClientIDForNewCase()" name="caseSummary" required></td>
</tr>
<tr>
<td>Type</td>
<td>
<select class="form-control input-sm" ng-model="vm.newCaseSubTypeID" ng-options="caseSubType.specID as caseSubType.specName for caseSubType in vm.newCaseSubTypes" name="caseSubType">
</td>
</tr>
<tr>
<td>
<button id="create" type="submit" class="btn btn-primary" ng-disabled="!vm.hasClientIDForNewCase()">Create New Case</button>
</td>
</tr>
</table>
Can anyone suggest some other approach if this is not the correct approach or please provide a solution with current approach.
I would suggest doing it recursively :
var cases = element.all(by.options('caseSubType.specID');
createCases(cases, 0);
var createCases = function(cases, i) {
if (i = cases.length)
return;
browser.get("xyz.com");
//browser.waitForAngular(); NOT needed, if it's an angular page
element(by.id("create")).click();
cases[i].click();
element(by.model('vm.newCaseSummary')).sendKeys('test');
$('button[ng-disabled="!vm.hasClientIDForNewCase()"]').click();
createCases(cases, i + 1);
}