JqGrid PHP: how to implement a righ-click context-menu? - php

How can I implement a righ-click context-menu in JqGrid for PHP ?
I am trying this solution by Oleg, but it is not working.
I would like to get this:
grid.php snippet:
$rightclick = <<<RIGHTCLICK
function () {
$("tr.jqgrow", this).contextMenu('myMenu1', {
bindings: {
'edit': function (trigger) {
// trigger is the DOM element ("tr.jqgrow") which are triggered
grid.editGridRow(trigger.id, editSettings);
},
'add': function ( /*trigger*/ ) {
grid.editGridRow("new", addSettings);
},
'del': function (trigger) {
if ($('#del').hasClass('ui-state-disabled') === false) {
// disabled item can do be choosed
grid.delGridRow(trigger.id, delSettings);
}
}
},
onContextMenu: function (event /*, menu*/ ) {
var rowId = $(event.target).closest("tr.jqgrow").attr("id");
//grid.setSelection(rowId);
// disable menu for rows with even rowids
$('#del').attr("disabled", Number(rowId) % 2 === 0);
if (Number(rowId) % 2 === 0) {
$('#del').attr("disabled", "disabled").addClass('ui-state-disabled');
} else {
$('#del').removeAttr("disabled").removeClass('ui-state-disabled');
}
return true;
}
});
}
RIGHTCLICK;
$grid->setGridEvent('loadComplete ', $rightclick);
Is there any way to get a context menu in JqGrid for PHP ?

First of all your code have unneeded space: 'loadComplete ' instead of 'loadComplete'.
I can repeat one more time that I don't use PHP myself and don't use setGridEvent of JqGrid for PHP too. So I can only guess that $grid->setGridEvent probably don't forward this correctly. In the case you can use setGridParam to set callback dynamically (see the answer) or to use jqGridLoadComplete event instead of loadComplete callback (see the answer).

Related

How to use a PHP function for multiple calls within the same PHP page?

I am trying to use a php function to get the prices of a tola (11.664 grams) at an order status page. The function uses a php page 'priceApi4CurCtrl.php' that fetches the price data from a website using an external API. My function is as follows:
function tolaPrice($cur_pick) {
require('priceApi4CurCtrl.php');
if($cur_pick == 'pkr') {
$tola_price = $bitprice_pkr*10*11.664;
return $tola_price;
} elseif($cur_pick == 'usd') {
$tola_price = $bitprice_usd*10*11.64;
return $tola_price;
} elseif($cur_pick == 'aed') {
$tola_price = $bitprice_aed*10*11.64;
return $tola_price;
}
}
// Succeeds for the first call as under
$cur_pick = 'pkr';
echo tolaPrice($cur_pick);
// Fails for the second call as under
$cur_pick = 'aed';
echo tolaPrice($cur_pick);
The function works fine for the first call using echo tolaPrice($cur_pick). However, it fails all subsequent calls and hence I am unable to complete the order status of second and subsequent orders.
I am not sure how to work around this.
Instead of trying to wrap an if else loop in a function, I simply calculated the prices in a separate file named tola_price.php as follows:
include('priceApi4CurCtrl.php');
$tola_price_pkr = $bitprice_pkr*10*11.664;
$tola_price_usd = $bitprice_usd*10*11.64;
$tola_price_aed = $bitprice_aed*10*11.64;
And then called the tola_price.php within my script with if else loop as follows:
require_one('tola_price.php');
if($cur_pick == 'pkr') {
$tola_price = $tola_price_pkr;
} elseif($cur_pick == 'usd') {
$tola_price = $tola_price_usd;
} elseif($cur_pick == 'aed') {
$tola_price = $tola_price_aed;
}
And then used the prices to build further script.
Thanks to those who offered help

ngIf doesn't update with variable

I have a very simple ng-if construction using two MD buttons and one AngularJS scope variable isPlaying. However, the ng-if doesn't seem to work along with the variable. I can see the variable changing as I click the buttons, using console tools, however no changes to the DOM. Strangely enough the ng-if does seem to trigger when I hover other Angular components, such as buttons in the main nav.
HTML/MD/PHP:
<md-button class="md-icon-button" ng-if="!isPlaying" ng-click="play()" aria-label="Play">
<md-icon md-svg-src="<?php echo get_stylesheet_directory_uri(); ?>/img/icon/ic_play_arrow_black_24px.svg"></md-icon>
</md-button>
<md-button class="md-icon-button" ng-if="isPlaying" ng-click="pause()" aria-label="Pause">
<md-icon md-svg-src="<?php echo get_stylesheet_directory_uri(); ?>/img/icon/ic_pause_black_24px.svg"></md-icon>
</md-button>
JS:
$scope.play = function () {
player.playVideo();
$scope.isPlaying = true;
}
$scope.pause = function () {
player.pauseVideo();
$scope.isPlaying = false;
}
The problem was being caused by a function wrapped inside a timeout block. I replaced the setTimeout with $timeout and everything started working fine:
$scope.isPlaying = false;
$scope.videoTime = 0;
function onPlayerStateChange() {
console.log(player);
if (player.getPlayerState() === 1)
$scope.isPlaying = true;
else
$scope.isPlaying = false;
$timeout(onPlayerStateChange, 500);
}

Laravel - share and update variable across controller functions

I am trying to share and update a variable ($shipval) in my controller functions in a Laravel project.
Controller:
<?php
class PaymentController extends BaseController {
var $shipval='0';
function postCheckout() {
// ship_cost gets its value from a select element
$ship_cost= Input::get('shipping');
// below its supposed to update the global var $ship_cost value accordingly
if ($ship_cost == 0) {
$shipval = '0';
}
if ($ship_cost == 1) {
$shipval = '4.99';
}
if ($ship_cost == 2) {
$shipval = '8.99';
}
}
function postPayment() {
dd($this->shipval); //At the moment outputs 0
}
}
The output of the dd($this->shipval); should get updated in the postCheckout() function. Instead is just printing the initial value set above (0). I also, I tried to have $this->shipval == '4.99' inside the if statements, but still is not updating the value. Is it possible to do so in Laravel 4?
You should use $this->shipval instead of $shipval. So it would be like this for example:
$this->shipval = '4.99';

Bootstrap - How to save the state of collapse in a SESSION

I use Bootstrap and I would like to save the current state of collapse (open or close) in a session variables PHP (not in a cookie).
Could you give me :
- an example code to save the current state in a session variable PHP.
- and an example code to open or not the collapse (depends on the state stored in the session variable) when the page loads.
Thank you very much
First of all, do you really need to store it into session? Does some PHP script work with that value?
Both cases:
You must store somewhere in JavaScript variable the state of collapsed items:
var collapsed = false; // the default value
$('.collapse').on('hide.bs.collapse', function () {
collapsed = true; // on hide, collapsed is true
})
$('.collapse').on('show.bs.collapse', function () {
collapsed = false; // on show, collapsed is false
})
Yes, i need to store it into session:
At each request, you must add the collapsed variable and pass it for example trhough GET method:
$('a').on('mousedown', function() {
var c = collapsed ? 1 : 0;
var href = $(this).attr('href');
if(href.indexOf('?') !== -1) {
$(this).attr('href', href + '&collapsed=' + c);
else {
$(this).attr('href', href + '?collapsed=' + c);
}
});
And somehow save it to session
$_SESSION['collapsed'] = $_GET['collapsed'];
No, i don't need to store into session:
Most modern browsers now have localStorage variable, which is something like session in JavaScript.
Save into variable (in event handlers for example):
if(typeof(Storage)!=="undefined")
{
window.localStorage.setItem('collapsed', collapsed); // saves with no expiration
code.sessionStorage.setItem('collapsed', collapsed); // saves until browser is closed
}
else
{
// Sorry! No Storage support..
}
Load in some startup script:
if(typeof(Storage)!=="undefined")
{
collapsed = window.localStorage.getItem('collapsed'); // again choose one
collapsed = code.sessionStorage.getItem('collapsed');
if(collapsed) {
$('.collapse').collapse('show');
} else {
$('.collapse').collapse('hide');
}
}
else
{
// Sorry! No Storage support..
}
There may be other solutions, but these are the only i can think about. :)
All codes require jQuery
I would do something like this on the PHP side of things. It allows for storing and retrieving the state of multiple ID:s. Session handling is not included; the functions assume there is an active session going. Also, the functions are static since there is no real need to instantiate a class. And it makes for simpler usage.
class Collapse
{
public static function set_state($id = null, $state = null)
{
if ($id === null || $state === null || !is_numeric($state)) {
return false;
} else {
$state = ($state == 0 ? 0 : 1);
$_SESSION['collapse_state'][$id] = $state;
}
}
public static function state($id = null)
{
if ($id === null) {
return false;
} else {
return $_SESSION['collapse_state'][$id];
}
}
}
// --- Sets the state for chosen ID.
Collapse::set_state('info', 0);
// --- Returns the state of the ID.
Collapse::state('info');
Use this class together with the JavaScript proposed by Tomáš Tomíík Blatný and you'll be able to save your states. I'd probably use POST instead of GET though, to make the URL:s look a bit nicer. This can be done by populating a hidden form field or by sending an AJAX request to a PHP-script that only handles the collapse states.

PHP dynamic link list breaks ajax

I have this list of things generated from an array, where the array value maps to the array key, which maps to a query in the database. Now the problem I'm facing is that when this list is included in my ajax portion, it only works for one click before the page refreshes. What gives?
I'm using the HTML5 History API method to change the url upon clicks by the href="" portion of the link.
I'm somewhat new to jQuery, which is odd since I'm a front end developer primarily but I just never got the time to catch up, but my link list breaking this code just doesn't make sense to me.
$("a[rel='ajax']").click(function(e){
//e.preventDefault();
pageurl = $(this).attr('href');
$.ajax({url:pageurl+'?rel=ajax',success: function(data){
$('#ajax')
.load(pageurl + ' #resultati');
}});
if(pageurl!=window.location){
window.history.pushState({path:pageurl},'',pageurl);
}
return false;
});
The links are generated using this function in PHP, I'm using the laravel framework if that makes any difference.
public static function filterlink ($filter, $what)
{
if (URI::segment(2) == 'make')
{
$first = URI::segment(3);
$next = URI::segment(4);
}
else
{
$first = URI::segment(2);
$next = URI::segment(3);
}
// what == 0 is the filter, what == 1 is the +filter. This reffers to the link being created.
switch (substr($first, 0, 1))
{
case ' ': // example.ex/make/[+]current starts with a plus sign.
if($what == 0)
//new no+ filter.
{
return HTML::link('make/'.self::slug($filter.'/'.$first), $filter, array('rel' => 'ajax'));
// example.ex/make/+current => example.ex/make/newfilter/+current
}
elseif($what == 1)
//new +filter
{
return HTML::link('make/+'.self::slug($filter), $filter, array('rel' => 'ajax'));
// example.ex/make/+current+replaced+by+newfilter
}
break;
default:
if($what == 0)
// new no+ filter.
{
if(!empty($next))
{
return HTML::link('make/'.self::slug($filter.'/'.$next), $filter, array('rel' => 'ajax'));
}
else
{
return HTML::link('make/'.self::slug($filter), $filter, array('rel' => 'ajax'));
}
}
elseif($what == 1 && (!empty($first)))
{
return HTML::link('make/'.self::slug($first.'/+'.$filter), $filter, array('rel' => 'ajax'));
}
elseif($what == 1 && (empty($first)))
{
return HTML::link('make/+'.self::slug($filter), $filter, array('rel' => 'ajax'));
}
}
}
I've tried putting my link lists outside the ajax field, which keeps the ajax flow going without any boring page refreshes, but my link urls don't update, obviously, and I'm only able to construct one-ended links, breaking the pages dual filter functionality.
Any help would be great.
If you're actually loading HTML content with links that you want to be AJAX friendly, then you'll need to use .on so that the new links also get taken over by your jQuery function.
$(document).on('click',"a[rel='ajax']",function(e) {
Working sample here:
http://jsfiddle.net/hansvedo/cJW54/
Just a quick thought, what about placing preventDefault where the pushState function is? Or even just uncommenting it where it is? preventDefault prevents the normal linking action from happening.
if(pageurl!=window.location){
e.preventDefault();
window.history.pushState({path:pageurl},'',pageurl);
}

Categories