Angular ng-repeat list value spliting - javascript

I'm storing below data in localstorage and i wanted to retrieve these values in html page for search history(assume search history is an list).
hotelSearchCriteria = {
destination : location,
datesInfo:{
checkInDate : '2017/05/22',
checkOutDate: '2017/05/30'
}
}
localStorage.setItem("hotelSearchCriteria", JSON.stringify(hotelSearchCriteria));
var storedInfoArray = JSON.parse(localStorage.getItem('hotelSearchCriteria'));
if(storedInfoArray != null){
$scope.storedInfoArrayAngular = storedInfoArray;
}
I want to retrieve these value using ng-repeat and i need to split checkInDate and checkOutDate as follows "05/22 (Mon) - 05/30 (Tue)"
I can able store data in localStorage successfully as a list but i'm facing issue in retrieving these list of values. How can i split dates in ng-repeat ?
<li ng-repeat="storedInfo in storedInfoArrayAngular">
<p>{{storedInfo.destination}}</p>
<span>{{storedInfo.checkInDate}}</span> - <span>{{storedInfo.checkOutDate}}</span>
</li>
Expected result
Search History
Singapore
05/22 (Mon) - 05/30 (Tue)
Tokyo
05/22 (Mon) - 05/30 (Tue)
Anybody can help me out ? Thanks in advance

It looks like checkInDate and checkOutDate are properties of the storedInfo.datesInfo, so you should use the full object path when referring to it:
<li ng-repeat="storedInfo in storedInfoArrayAngular">
<p>{{storedInfo.destination}}</p>
<span>{{storedInfo.datesInfo.checkInDate}}</span> - <span>{{storedInfo.datesInfo.checkOutDate}}</span>
</li>
And to format your dates as 05/22 (Mon) you can use the date filter as follows:
{{ storedInfo.datesInfo.checkInDate | date: "MM/dd (EEE)" }}

Related

Comparing '==' in an IF statement is not working Google Sheets script

The objective is to compare ColA in the Orders sheet with ColF in the Ordered Items sheet, if they match grab the email from ColB.
The script outputs no errors, but it doesn't match the highlighted cells in either sheet.
(note: the items are automatically added by an app, so the formatting of the cells are default and need to keep it that way as I'm using the last 6 digits as the order reff eg; 49.263Z)
Orders sheet
Ordered Items sheet
function getEmailFromOrderedItemToOrders(){
var orders = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Orders');
var lr = getLastRow(orders, "A1:G");
Logger.log(lr); //LastRow index
//Get last 'OrderID (Paid at)' value from 'Orders' sheet
var orderIdCol = 1;
var orderId = orders.getRange(lr, orderIdCol).getValue();
Logger.log(orderId); //LastRow 'orderId' value
//Match 'orderId' to 'orderId' in 'Ordered Items' and return col 1
var items = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Ordered Items');
var itemsData = items.getDataRange().getValues();
Logger.log(itemsData[0][1]); //'Purchase Email' col
Logger.log(itemsData[0][5]); //'Paid at' col
for(var i = 0; i<itemsData.length;i++){
if(itemsData[i][5] == orderId){ //Issue here: not comparing values as a match
var email = itemsData[i][1];
Logger.log(email); //Does not print
return i+1;
}
}
}
In javascript when you compare two new Date, you will be getting the value as false.
Reason is when you have two dates, its basically two different objects. So One object is not equal to another object when you use == or ===. So the simple and better way is converting the date to a number using .getTime() that will return how many milliseconds passed from that date and 00:00:00 of Jan 1, 1970.
Sample code snippet
console.log(new Date() === new Date())
console.log(new Date().getTime() === new Date().getTime())
This can be simply solved by using a Sheets Formula which. What you would do is:
=INDEX( 'Ordered Items!B:B', MATCH(A2, 'Ordered Items!:F:F,0) )
The formula basically says:
Return the Email Column
Find the Index (# of the row) where the value of A2 is in column Ordered Items F:F
See here for a tutorial on it: https://www.youtube.com/watch?v=9sLWDjAEuyc

AngularJS: filterting/searching based on formatted date

I am having a table in AngularJS which I used filter to do the searching functionality.
<tr ng-repeat="order in orders | orderBy:sortType:sortReverse |filter:searchValue">
In the table, I have a date field which I formatted and displayed in MM/dd/yyyy:
<td>
{{order.OrderDate | date:"MM/dd/yyyy"}}
</td>
The issue is, the original date type is something like: '2015-10-30' and the filtering will be based on that. So if I search for 10/30/2015, it will return nothing. Is there any way to resolve this?
Declare a method in scope
Like this
$scope.filterValue=function(obj){
return $filter('date')(obj.OrderDate, 'MM/dd/yyyy') == $filter('date')($scope.searchValue, 'MM/dd/yyyy')
}
Add this method to your html's filter
Like this
<tr ng-repeat="order in orders | orderBy:sortType:sortReverse |filter:filterValue">
N:B:
you have to inject $filter in your controller
Try like this
Working Demo
script
var app = angular.module('myApp', []);
app.controller('Controller', function ($scope) {
$scope.order = {};
$scope.order.OrderDate = "2015-10-30";
$scope.parseDate = function(input) {
var parts = input.split('-');
return new Date(parts[0], parts[1] - 1, parts[2]);
};
});
html
<div ng-app='myApp' ng-controller="Controller">
{{parseDate(order.OrderDate) | date:"MM/dd/yyyy"}}
</div>
output
10/30/2015
or if you can return additional column in record set as formatted date
JobStartDate = Convert.ToDateTime(Record[5]),
JobStartDateSearch = Convert.ToDateTime(Record[5]).ToShortDateString(),
Where JobStartDate represents actual date object
JobStartDateSearch will represent formatted date string. So in search textbox if you type date string e.g. 5/14/2018, that will match with JobStartDateSearch and record will be filtered.

How do I parse text into a JSON object?

I have a dhcp lease file with the following example entries:
lease 172.16.20.11 {
starts 4 2014/10/09 18:33:57;
ends 4 2014/10/09 18:43:57;
cltt 4 2014/10/09 18:33:57;
binding state active;
next binding state free;
rewind binding state free;
hardware ethernet XX:XX:XX:XX:XX:XX;
client-hostname "phone";
}
I am trying to find a way to convert the information into JSON so I can use in Dojo.
I would like the output to be like
{"leases": ["address":"172.16.20.11", "starts":"2014/10/09 18:33:57", "ends":"2014/10/09 18:43:57","
client-hostname":"phone"]}
Is there a way to do this?
Thanks,
Tim T
var str = 'lease 172.16.20.11 { starts 4 2014/10/09 18:33:57; ends 4 2014/10/09 18:43:57; cltt 4 2014/10/09 18:33:57; binding state active; next binding state free; rewind binding state free; hardware ethernet XX:XX:XX:XX:XX:XX; client-hostname "phone"; }';
var res = str.split(/[\s;]+/); // regex match spaces and semicolons
// Create your leases array with a lease object from the parsed string
var leases = {leases:[{
address: res[1],
starts: res[5] + " " + res[6],
ends: res[9] + res[10],
client_hostname: res[30].split('"')[1]
}]};
var json = JSON.stringify(leases); //convert the array of leases to json string
[EDIT] client-hostname must be client_hostname because of variable name restrictions
[EDIT] changed leases to be an object with an array property to more closely match your desired output
[EDIT] parsed phone from "phone" for client_hostname

JavaScript function to re-arrange date format

I'm working on some analytics, using Google Tag Manager.
We have a datalayer on the site, one of the value's is a date in format DDMMYYYY.
For a media tag I need to change this date to YYYY-MM-DD.
I'm trying something like this as an example: but I can't get it to work.
I am a total novice with Javascript
(function(){
var d = {{Departure_Date}}
var u = {{Departure_Date}}.replace(/(\d{2})(\d{2})(\d{4})/,'$3$2$1')
return u
})()
Here {{Departure_Date}} is a variable that access the dataLayer and pulls out the original date format.
Any help would be greatly appreciated.
[assuming {{Departure_Date}} is a string, like '04032003']
Almost there. Try [ddmmyyyy].replace(/^(\d{2})(\d{2})(\d{4})$/, '$3-$2-$1'). Alternatively, you can slice up the ddmmyyyy string (see snippet)
var res = document.querySelector('#result');
res.innerHTML = 'input: 20052014, output: ' +
'20052015'.replace(/^(\d{2})(\d{2})(\d{4})$/, '$3-$2-$1');
// Alternative:
var inp = '18072013';
res.innerHTML += '<br>input: 18072013, output: ' +
[inp.slice(4), inp.slice(2,4), inp.slice(0,2)].join('-');
<div id="result"><div>

DOM Manipulation help needed

<div id="main">
<div class="navigate">
<ul>
<li>
<span>xm</span> »
</li>
<li>
<span>Admin</span> »
</li>
<li>
<span>change</span>
.......
From this html code I have to get random generated data "random02342=randomvaluetoo320230" in variable, which is random generated both random0234 and random value too. How I can achieve this via Javascript (jquery or not)?
EDIT: random02342 and randomvaluetoo320230 is randomly server-side generated.. so its like every time new hash like 1stHash4324kjkjdas324324=And2ndAnotherHash324324asd23432
This would get it, though it's probably not going to win any awards for elegance:
var m = document.getElementById('main').innerHTML.match(/random(\d+)=randomvaluetoo(\d+)/);
// m[1] and m[2] are the two random values.
based on your comment:
var value = $('selector').attr('href').split(';')[2].split('=');
this will give you value[0] - name, and value[1] - value
However this is only if there 3 query params and the random one is the third
Also this might be usefull Parse query string in JavaScript
var first, second;
var q = $('.navigate ul li a[href*="change=name"]').attr('href').split(';').filter(
function() {
return this.contains('random');
}
).join().replace(/[a-zA-z]/g, '').split('=');
first = q[0];
second = q[1];

Categories

Resources