Old cookies vanish when add new cookies angularjs - javascript

I'm try add cookies . It's work well but when I refresh page and add new cookies , old cookies disappear , and cookies.length not update . It update after i refresh page . Here is my code
vm.cart = [];
vm.add = function(tensp,gia,img){
vm.cart.push({tensp:tensp,gia:gia,img:img,sl:1})
$cookies.putObject('cart',vm.cart);
}
And display in html .
vm.cookies = $cookies.getObject('cart');
{{vm.cookies.length}}
Where is my wrong . Please help me . Here is my plnkr
https://plnkr.co/edit/StURuPuEBr8ykIUPdvBV?p=preview

Whenever you refresh the page. vm.cart will be set to []. And when you click "Add", one item will be added to vm.cart. vm.cart now contains one item. When vm.cart is put into cookies, all existing items in cookies will be overwritten.
If you want to keep all items in cookies, put items directly to cookies and retrieve items directly from cookies as follows
var item = {
id: id,
masp: masp,
tensp: tensp
};
var cart = $cookies.getObject('cart');
if (cart) {
var items = JSON.parse(cart);
items.push(item);
$cookies.putObject('cart', JSON.stringify(items));
} else {
$cookies.putObject('cart', JSON.stringify([ item ]));
}
Here's a demo.

Related

send the value of the field from an interactive grid to another page and store it in an item with Javascript and Oracle Apex

Good evening, I currently save the value of a field of the grid in an item (P5_VALUE),send it to another page (page 6) and store it in an item (P6_VALUE) through a button
Action: Redirect to Page in this application.
Target: Page in this application , Page: 6, set Items: NAME: P5_VALUE, VALUE: P6_VALUE, Action: Reset Pagination. This is the url that is generated: http://ip:port/ords/app/system/page?p5_value=61&clear=RP&session=12518184703789
But now I want to do the same with APEX$ROW_SELECTOR. With this code I have been able to add an action to the grid and get the value of the field but I do not know how to send it to the other page and assign the value to the other item
var view = apex.region("ig-emp").widget().interactiveGrid("getViews", "grid"),
menu$ = view.rowActionMenu$;
menu$.menu("option").items.push({
type:"action",
label:"Get Emp No",
icon: "fa fa-close",
action: function(menu, element) {
var record = view.getContextRecord( element )[0];
var val1 = view.model.getValue(record, "EMPNO");
}
});
Por favor su ayuda, gracias
Use the below approach to achieve this:
Let me start by adding some more lines to your code.
var view = apex.region("ig-emp").widget().interactiveGrid("getViews", "grid"),
menu$ = view.rowActionMenu$;
menu$.menu("option").items.push({
type:"action",
label:"Get Emp No",
icon: "fa fa-close",
action: function(menu, element) {
var record = view.getContextRecord( element )[0];
var val1 = view.model.getValue(record, "EMPNO");
apex.page.submit({
request:"NAVIGATE",
set:{"P1_EMPNO":val1},
showWait: true
});
}
});
Note: in the above code replace P1_EMPNO with your page item name.'
Create a Branch -> Type: Page or URL(Redirect).
Select the page number to redirect and set the value of the current page item to the new page item. Refer below screen shot for example.
In the Server-side condition, select the Type as Request = Value and provide the value as NAVIGATE. Refer below screen shot.

How to do update in listing for web by using firebase

I am creating admin panel in website and I am using firebase as a database in backend.I am able to display listing but when I click on the particular listing there status should change from 'pending' to 'accept' but it doesnt.I dont know where I did mistake.Please give suggestion and I attach js file and database screenshot
pl.js
var firebaseheadingRef = firebase.database().ref().child("user");
firebaseheadingRef.on('child_added',datasnapshot=>{
var title= datasnapshot.child("listing").child("title").val();
var userid= datasnapshot.child("username").val();
var type= datasnapshot.child("listing").child("title").val();
var publisheddate= datasnapshot.child("listing").child("publish").val();
var expirydate= datasnapshot.child("listing").child("expire").val();
$("#tablebody").append("<tr><td>"+title+"</td><td>"+userid+"</td><td>"+type+"</td><td>"+publisheddate+"</td><td><button type=button id=accept onclick=accept()>Accept</button><button type=button>Reject</button></td></tr>");
});
function accept()
{
firebaseheadingRef.on('child_changed',datasnapshot=>{
datasnapshot.child("listing").child("status").update({"status":"accept"});
setCommentValues(postElement, data.key, data.val().text, data.val().author);
});
}
database
listing display picture where I click on accept button then update of status should done
There are two places where you need to change your code:
First, in the code that generates the table, you have to pass the id of the node to the function call, as follows. You get the node id with the key property of the DataSnapshot.
.....
$("#tablebody").append("<tr><td>"+title+"</td><td>"+userid+"</td><td>"+type+"</td><td>"+publisheddate+"</td><td><button type=button id=accept onclick=accept('" + datasnapshot.key + "')>Accept</button><button type=button>Reject</button></td></tr>");
...
And secondly you have to write your accept() function in such a way it updates the database value, with the set() method. Like the following
function accept(userId) {
var nodeRef = firebase.database().ref("/user/" + userId + "/listing/status");
return nodeRef.set('accept');
}

jQuery 'change' doesn't show most up-to-date data

I have a jQuery change function that populates a dropdown list of Titles from the user selection of a Site dropdown list
$("#SiteID").on("change", function() {
var titleUrl = '#Url.Content("~/")' + "Form/GetTitles";
var ddlsource = "#SiteID";
$.getJSON(titleUrl, { SiteID: $(ddlsource).val() }, function(data) {
var items = "";
$("#TitleID").empty();
$.each(data, function(i, title) {
items +=
"<option value='" + title.value + "'>" + title.text + "</option>";
});
$("#TitleID").html(items);
});
});
The controller returns JSON object that populates another dropdown list.
public JsonResult GetTitles(int siteId)
{
IEnumerable<Title> titleList;
titleList = repository.Titles
.Where(o => o.SiteID == siteId)
.OrderBy(o => o.Name);
return Json(new SelectList(titleList, "TitleID", "Name"));
}
The markup is:
<select id="SiteID" asp-for="SiteID" asp-items="#Model.SiteList" value="#Model.Site.SiteID" class="form-control"></select>
<select id="TitleID"></select>
The problem is that the controller method is only touched on the FIRST time a selection is made. For example,
The first time SITE 1 is selected, the controller method will return the updated list of Titles corresponding to SITE 1
If SITE 2 is selected from the dropdown, the controller will return the updated list of Titles corresponding to SITE 2
The user adds/deletes Titles in the database corresponding to SITE 1
User returns to the form and selects SITE 1 from the dropdown. The list still shows the results from step 1 above, not the updates from step 3
If I stop debugging and restart, the selection will now show the updates from step 3.
Similar behavior described in jQuery .change() only fires on the first change but I'm hoping for a better solution than to stop using jQuery id's
The JSON response is:
[{"disabled":false,"group":null,"selected":false,"text":"Title2","value":"2"},{"disabled":false,"group":null,"selected":false,"text":"Title3","value":"1002"},{"disabled":false,"group":null,"selected":false,"text":"Title4","value":"2004"},{"disabled":false,"group":null,"selected":false,"text":"Title5","value":"3"},{"disabled":false,"group":null,"selected":false,"text":"Title6","value":"9004"}]
The issue was that the JSON result was being read from cache as #KevinB pointed out. This was fixed by adding the following line within the change function
$.ajaxSetup({ cache: false });

How to submit dynamically created hidden variables using Jquery

I have created a dynamic table. DEMO to my project.
I have placed this dynamic table in the form under a div named 'box'
<div id="box">
</div>.
I am creating dynamic hidden variables table using Jquery which I need to store in DB. This is how I am creating the hash to submit to server.
criteria = $('form_name').serialize(true);
criteria = Object.toJSON(criteria);
// Build the object to store the parameters for the AJAX post request
parameters = {
title : $('report_title').value,
description : $('report_description').value,
criteria : criteria
}
// Make the AJAX post request
new Ajax.Request( URL, {
method: 'post',
parameters: parameters,
onSuccess: function( response ) {
$('messageSpan').innerHTML = response.responseText;
$('spinner').style.display='none';
}
});
I am not able to capture the dynamically created values in the criteria.
How to solve this?
In the dynamically created section, I tried adding a submit button and see if the values can be fetched. I am able to fetch and iterate all the hidden variables.
$('#jquerysaveButton').click(function(){
jsonObj = [];
$("input[id=rubric_cell]").each(function () {
var id = "cell_" + row + "_" + col;
item = {}
item["id"] = id;
item["selected_rubric"] = $(this).val();
jsonObj.push(item);
});
console.log(jsonObj); //I am getting the required values here
});
How to get these values in the criteria = $('form_name').serialize(true);. Am I doing some thing wrong? Please help me. thanks in advance.
DEMO to my project
You need to make sure that your hidden input fields have a name attribute set otherwise $.serialize will not process them.

How to remove the item in session using javascript for codeigniter?

I am working on a shopping cart php(codeigniter) project. So I have add the item in the session like the code following. The problem is, I would like to remove the item in session in the checkout page.
But of course I can not call the php function to remove the session in javascript , that means , when the remove button is click , how can I do (not restrict to use ajax, simple is the best), I can remove the item in session ? Thanks
if ($this->form_validation->run()) {
if ($this->session->userdata('purchase') !== false)
$purchase_list = $this->session->userdata('purchase');
else
$purchase_list = array();
$purchase = array(
'id' => $product[0]['id'],
'quantity' => $this->input->post('quantity')
);
if ($this->input->post('opt1') !== false)
$purchase['opt1'] = $this->input->post('opt1');
if ($this->input->post('opt2') !== false)
$purchase['opt2'] = $this->input->post('opt2');
array_push($purchase_list, $purchase);
$this->session->set_userdata('purchase', $purchase_list);
redirect('cart');
}
You can define a function in your controller to unset the session e.g.
class Controllername extends CI_Controller {
function index(){}
function reset_session(){
$sesion_variable = 'name_of_session';
$this->session->unset_userdata($session_variable);
}
}
And you can call that via CURL or ajax. On checkout page
from CodeIgniter's session class documentation
This assumes you understand your cart and will know where to actually unset session data in your checkout scenery.
you can set the value of desired item in your session using this syntax:
$this->session->set_userdata('purchase', null); // will set the purchase session data to null
you only know which key to set to null though

Categories