storing multiple objects using HTML5 indexDB - angularjs

I have JSON object in the below format,
$scope.indexData = {
"custid": "1",
"addresses": [
{
"addressType": "P",
"address1": ""
},
{
"addressType": "M"
}
],
"personalDetails": {
"title": "",
"name": ""
}
}
I want to store the object using HTML5 indexDB. how do i store the object?
I tried in the following way, but no luck.
var db;
var request = window.indexedDB.open("newDatabase", 1);
request.onupgradeneeded = function(event) {
var db = event.target.result;
var objectStore = db.createObjectStore("customers");
for (var i in $scope.indexData) {
objectStore.add($scope.indexData[i]);
}
}
I am getting the following error saying: Uncaught DataError: Failed to execute 'add' on 'IDBObjectStore': The object store uses out-of-line keys and has no key generator and the key parameter was not provided.

You are specifying a keypath which instruct the store to use in-line keys. your code maybe like this:
var db;
var request = window.indexedDB.open("newDatabase", 1);
request.onupgradeneeded = function(event) {
var db = event.target.result;
var objectStore = db.createObjectStore("customers",{keyPath: "isbn"});
for (var i in $scope.indexData) {
objectStore.add({i:$scope.indexData[i], isbn:i});
}
}

Related

Getting specific Item from local storage with angular

I want to retrieve a specific data from my object stored in local storage using angular. Here is my Object:
{
"user":{
"login":"lara",
"grade":"A"
},
"profiles":[
{
"profile":"admin",
"application":"management",
}
]
}
I want to retrieve profile information (admin) from the list of profiles.
I tried using localStorage.getItem().
setItem in local storage
const data = { your data object here }
SET ITEM
localStorage.setItem("data", JSON.stringify(data));
GET ITEM
let val = JSON.parse(localStorage.getItem('data')).profiles[0].profile;
console.log({val});
You can try with this solution
let data = {
"user": {
"login": "lara",
"grade": "A"
},
"profiles": [
{
"profile": "admin",
"application": "management",
}
]
}
for set data in local storage
localStorage.setItem("data", JSON.stringify(data));
for getting data from local storage
let val = JSON.parse(localStorage.getItem('data'));
if (val) {
let adminProfile = val.profiles.find((data) => data.profile == "admin");
console.log(adminProfile);
}
Try by this..
Use this while storing:
var data = {
"user":{
"login":"lara",
"grade":"A"
},
"profiles":[
{
"profile":"admin",
"application":"management",
}
]
}
localStorage.setItem("data", JSON.stringify(data));
while getting:
let val = JSON.parse(localStorage.getItem('data'));
let profile = val.profiles[0].profile;

Get Previous Processed Document in MongoDb

I have the following collection structure:
{
"col1": "col1_value1",
"col2": "col2_value1"
}
{
"col1": "col1_value2",
"col2": "col2_value2"
}
How do I get the following result in MongoDB:
{
"previous_col1": "", // init value
"col1": "col1_value1",
"previous_col2": "",
"col2": "col2_value1"
}
{
"previous_col1": "col1_value1", // previous value
"col1": "col1_value2",
"previous_col2": "col2_value1",
"col2": "col2_value2"
}
In SQL, I can achieve the same result by defining two variables #previous_col1 and #previous_col2 for keeping previous rows.
This mongo shell query does that:
var cur = db.collection.find();
var result_docs = [ ];
var prev_col1 = "", prev_col2 = "";
while (cur.hasNext()) {
let thisDoc = cur.next();
thisDoc.previous_col1 = prev_col1;
thisDoc.previous_col2 = prev_col2;
prev_col1 = thisDoc.col1;
prev_col2 = thisDoc.col2;
result_docs.push(thisDoc);
}
The result_docs array has all the documents updated with previous document's values.

How dynamically transform my "Object" to List in ng-model at view

I'm trying to transform my object to list dynamically, so I'm building at view instead of declaring at controller.
I don't want to declare like this: custom_fields.title_field.type_text_field = [] because the title_field is built dynamic, it could be any kind of text like full_name
My json as is:
"custom_fields":{
"title_dynamic_generate_field":{
"type_text_field":{
"name":"John",
"first_name":"Wick"
},
"type_boolean_field":{
"is_badass": true,
"is_good_movie": true
},
"type_select_field": {
"this_select": 1,
"i_got_this": "nope i didnt got this"
}
},
And to be:
"custom_fields":{
"title_dynamic_generate_field":{
"type_text_field":[{
"name":"John",
"first_name":"Wick"
}],
"type_boolean_field":[{
"is_badass": true,
"is_good_movie": true
}],
"type_select_field": [{
"this_select": 1,
"i_got_this": "nope i didnt got this"
}]
},
the object I'm trying to transform into array is type_text_field which can be dynamic too, like type_date_field or type_select_field and so on.
My ng-model is like this:
ng-model="objectApp.application.applicant.custom_fields[layout.dynamic_title][input.type][input.variable]"
the [input.type] is that I'm trying to transform into array, how can I achieve this? I tried to use $index, but got strange results.
We can do it by 2 solutions:
There is a question about your task:
? how you want handle if we have more than one type_text_field in title_dynamic_generate_field? because you want to convert it to "type_text_field":[{},...]
however my answers about the question are:
If we know what's the dynamic params which we want to send theme as json, i mean if we know what is the key of title_dynamic_generate_field or type_text_field, we do as this sample:
var data = {
"custom_fields": {
dynamicParamIs1: 'title_dynamic_generate_field',
dynamicParamIs2: 'type_text_field',
"title_dynamic_generate_field": {
"type_text_field": {
"name": "John",
"first_name": "Wick"
}
}
}
}
var paramHelper1 = json.custom_fields[json.custom_fields.dynamicParamIs1];
var paramHelper2 = json.custom_fields.dynamicParamIs2;
var solutionA = function (object, as) {
var array = [];
for (var key in object) {
var newObject = object[key];
array.push(newObject);
}
object[as] = array;
}
solutionA(paramHelper1, paramHelper2);
We changed a model of our json which can help us to detect (find) the keys
If we don't know what is the dynamic params are, we do as this:
var data = {
"custom_fields": {
"title_dynamic_generate_field": {
"type_text_field": {
"name": "John",
"first_name": "Wick"
}
}
}
}
var solutionB = function (json) {
var array = [];
for (var key in json) {
var j1 = json[key];
for (var key2 in j1) {
var j2 = j1[key2];
for (var key3 in j2) {
var fullObject = j2[key3];
array.push(fullObject);
j2[key3] = array;
}
}
}
}
solutionB(data);
This sample is manual which we use nested for to detect the keys name

How do I get my Filters from Filtersfeature

I am using FiltersFeature:
this.features = {
ftype: 'filters',
encode: false,
local: true
};
I would like to access my current filters on my table. Ie. not the filter configuration object, but what the changes user has done to the table (I want to save this in the database).
There is a filters object on grid, but that just gives me the filter configuration from all the columns.
handler: function (btn) {
var grid = btn.up('grid');
grid.filters
}
I need the actual values. I'm looking for something like this:
var v = {
filter: {
column: 'name',
value: 'bob'
},
filter: {
column: 'date',
value1: '11.11.11',
value2: '12.12.12'
}
}
Anyone know where i can get this info?
The answer I found out was to use this method :)
myTableGrid.filters.getFilterData()
I get something like this :
v = [{
"field": "myColumn1",
"data": {
"type": "string",
"value": "anna"}
}, {
"field": "myColumn2_fixedvalue",
"data": {
"type": "list",
"value": [60]}
}]
Filter does not contain any values.
You should parse actual values from the grid.
var columnTitle = [];
var fieldName = [];
var data = [];
var visibleColumns = grid.query('gridcolumn:not([hidden])');
visibleColumns.forEach(function(c) {
columnTitle.push(c.text);
fieldName.push(c.dataIndex);
});
for (rowIndex = 0; rowIndex < grid.store.getCount(); rowIndex++) {
var row = grid.getView().getRow(rowIndex);
var fieldValues = Ext.fly(row).query('div.x-grid-cell-inner');
var map = {}
for (colIndex = 0; colIndex < fieldName.length; colIndex++) {
map[fieldName[colIndex]] = fieldValues[colIndex].textContent;
}
data.push(map);
}
As a result, you will get an array of objects, something like
[{"user.login":"iivanov","email":"ivanov#mail.com"},{"user.login":"ppetrov","email":"petrov#mail.com"},{"user.login":"plosev","email":"elk#gmail.com"}]

getColumnArray equivalent for parsed JSON object arrays in Smartface.io Framework?

I got a Json file like;
{
"Cities": [
{
"Name": "London",
"Country": "UK"
},
{
"Name": "Rome",
"Country": "ITA"
},
{
"Name": "Antalya",
"Country": "TR"
}
]
}
How can I get City Names as an array like ["London","Rome","Antalya"] without doing;
var tempJSON = JSON.parse(jsonCities);
var arrayCityNames = [];
for (var i = 0; i < tempJSON.Table.length; i++){
arrayCityNames[i] = tempJSON.Table[i].Name;
}
if tempJSON was a Dataset we could use getColumnArray
arrayCityNames = Data.Dataset.getColumnArray("Name");
Is there any built in method to do this for parsed JSON's?
Please keep in mind that the question is related to Smartface.io Framework, not jquery itself
Try this:
var tempJSON = JSON.parse(jsonCities);// here you load your JSON
var arrayCityNames = []; // your output array
var cityArray = tempJSON['Cities']; // enter Cities array
for (var i = 0; i <cityArray.length; i++){ // iterate over your list
arrayCityNames.push(cityArray[i]['Name']); // add to list name of your city list
}

Resources