Add support for popup editing inside a live grid
authorJoey Schulze <joey@infodrom.org>
Mon, 24 Feb 2014 18:53:16 +0000 (19:53 +0100)
committerJoey Schulze <joey@infodrom.org>
Mon, 24 Feb 2014 18:53:16 +0000 (19:53 +0100)
ajax/ajax.php
lib/mask.php

index 5a7a1ec..2b84c0b 100644 (file)
@@ -528,6 +528,56 @@ function process_file($mask)
   exit;
 }
 
+function process_listedit($mask, $action)
+{
+  global $db;
+
+  if ($_REQUEST['second'] === true)
+    $list = $mask['second'];
+  else
+    $list = $mask['list'];
+
+  $values = array();
+  foreach ($_POST as $k => $v)
+    if (substr($k,0,10) == 'listinput_')
+      $values[substr($k,10)] = $v;
+
+  switch ($action) {
+  case 'upd':
+    $update = sprintf("sys_user='%s', sys_edit=now()", $_SESSION['sys']['login']);
+    foreach ($values as $c => $v)
+      $update .= sprintf(", %s = '%s'", $c, $v);
+    $sql = sprintf("UPDATE %s SET %s WHERE id = %d",
+                  $list['table_edit'],
+                  $update,
+                  $_POST['_k0']);
+
+    $db->query($sql);
+    break;
+  }
+}
+
+if (!empty($_REQUEST['_k0'])) {
+  db_connect();
+
+  foreach ($_POST as $k => $v)
+    if (substr($k,0,5) == 'grid_' && substr($k,-8) == '__action') {
+      $_REQUEST['source'] = substr($k,5,-8);
+      $action = $v;
+    }
+
+  if (substr($_REQUEST['source'],-8) == '__second') {
+    $_REQUEST['source'] = substr($_REQUEST['source'],0,-8);
+    $_REQUEST['second'] = true;
+  } else {
+    $_REQUEST['second'] = false;
+  }
+
+  if (load_mask($_REQUEST['source']) === false) exit;
+  process_listedit($mask, $action);
+  exit;
+}
+
 if (empty($_REQUEST['func']))
   exit;
 
index 936f3e0..3879c31 100644 (file)
@@ -174,15 +174,25 @@ function build_grid($name, $mask, $gridname = false)
   # $opts[] = 'frozenColumns: ' . count($mask['list']);
   $opts[] = 'saveColumnInfo: {width: true, filter: true, sort: true}';
 
+  $canEdit = array_key_exists('table_edit', $mask);
+  if ($canEdit) {
+    $opts[] = 'canEdit: true';
+    $opts[] = "updateURL: 'ajax/ajax.php'";
+  }
+
   $ret[] = sprintf('<table id="grid_%s" class="ricoLiveGrid">', $name);
   $ret[] = '  <tr>';
   $specs = array();
   $fields = array();
   foreach ($mask['list'] as $field => $data) {
     $ret[] = sprintf('  <th>%s</th>', $data['name']);
-    $s = array(sprintf("FieldName: 'input_%s'", $field),
-              sprintf("ColName: 'input_%s'", $data['name']));
-    $s = array();
+    if ($canEdit && array_key_exists('edit', $data)) {
+      $s = array(sprintf("FieldName: 'listinput_%s'", $field),
+                sprintf("ColName: '%s'", $data['name']),
+                $data['edit']);
+    } else {
+      $s = array();
+    }
     if (isset($data['visible']) && $data['visible'] === false) $s[] = 'visible: false';
     if (isset($data['width']) && $data['width'] > 0) $s[] = 'width: ' . $data['width'];
     if (array_key_exists('type', $data)) $s[] = "type: '" . $data['type'] . "'";