jqGrid:“多合一";接近宽度jqGridEdit类>如何设置复合主键? [英] jqGrid : "All in One" approach width jqGridEdit Class > how to set a composite primary key?

查看:95
本文介绍了jqGrid:“多合一";接近宽度jqGridEdit类>如何设置复合主键?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何为多合一"方法(在JS文件中定义的网格,以及在php文件中使用jqGridEdit类的数据)设置复合主键? 对我来说,表T的复合主键是一个基本主键,它由属于该表T的某些字段定义!

How to set a composite primary key for a "All in One" approach (grid defined in JS file, and data using jqGridEdit Class in php file) ? Please, for me a composite primary key of a table T, is a elementary primary key that is defined with some fields belong to this table T !

这是我的测试,但我没有数据,也无法使用CRUD操作:

Here is my test, but i get no data and cannot use the CRUD operations :

  • 在我的JS文件中,我有以下代码行:

  • In my JS file i have this lines code:

...
colModel:[
   {name:"index",index:"index",label:"index"}, // <= THAT'S JUST THE INDEX OF MY TABLE         
   {name:"user",index:"user",label:"user",key:true}, // <= A PART OF MY COMPOSITE PRIMARY KEY
   {name:"pwd",index:"pwd",label:"pwd",key:true},    // <= A PART OF MY COMPOSITE PRIMARY KEY
   {name:"state",index:"state",label:"state",key:true}, // <= A PART OF MY COMPOSITE PRIMARY KEY
   ... <= AND SO ON
   url:"mygrid_crud.php",
   datatype:"json",
   jsonReader:{repeatitems:false},
   editurl: "mygrid_crud.php",
   prmNames:{"id":"index"} // <= WHAT I NEED TO WRITE HERE ???
  ...

  • 在我的php文件(mygrid_crud.php)中:

  • In my php file (mygrid_crud.php) :

    ...
    $grid = new jqGridEdit($conn);
    $query = "SELECT * FROM mytable WHERE user='$user' and pwd='$pwd' and state='$state'..."; // <= SELECT * it's ok or i need to specify all fields i need ? 
    $grid->SelectCommand = $query;
    $grid->dataType = "json";
    $grid->table = 'mytable';
    $grid->setPrimaryKeyId('index'); // <= WHAT I NEED TO WRITE HERE ???
    ...
    $grid->editGrid();
    

  • 请告诉我有什么问题,以及在这种方法中如何设置复合主键!?

    Please, say me what is wrong, and how to do to set a composite primary key in this approach !?

    非常感谢您的巡回演出.

    Thank you so much for tour responses.

    因此,这是经过大量努力后得到的解决方案;-(

    So, here is the solution i get after a lot of efforts ;-(

    这可能不是最佳解决方案,但是添加,编辑和Dell操作效果很好!

    It's maybe not the optimal solution, but add, edit and dell operations work well !!!

    这里是带有COMPOSITE PRIMARY KEY的表定义的示例:

    Here is an example of a table definition with a COMPOSITE PRIMARY KEY :

    CREATE TABLE `chat` (
      `number` int(11) NOT NULL AUTO_INCREMENT,
    
      `user` varchar(30) NOT NULL,
      `pwd` varchar(100) NOT NULL,
      `subject` varchar(100) NOT NULL,
      `time` datetime NOT NULL,
      `recipient` varchar(100) NOT NULL,
    
      `message` varchar(1000) DEFAULT NULL,
    
      PRIMARY KEY (`user`, `pwd`, `subject`, `time`, `recipient`),
      UNIQUE KEY `number` (`number`)
    ) 
    

    因此,要使用网格管理此类表,我将执行以下操作.

    So to manage such table with a grid, i do as follows.

    在我的HTML文件中,我有以下代码行:

    In my HTML file, i have this lines of code :

    ...
    <table id='grid_chat'></table>
    <div id='pager_chat'></div>
    ...
    

    在我的PHP文件中,我有以下代码行:

    And in my PHP file, i have this lines of code :

     ...
    $grid = new jqGridRender($conn);
    …
    if($oper=="edit"){…}
    elseif($oper=="del"){…}
    …
    $grid->setPrimaryKeyId('number');
    …
    // here i get the primary key for the new message i want to add 
    $chat = $_SESSION["chat"];
    $user = $chat["user"];
    $pwd = $chat["pwd"];
    $subject = $chat["subject"];
    …
    $query = "SELECT * FROM chat WHERE user='$user' and pwd='$pwd' and subject='$subject'...";
    $grid->SelectCommand = $query;
    …
    // for the UNIQUE KEY (used for edit and dell operations)
    $grid->setColProperty("number", array("hidden"=>true)); 
    
    // for the COMPOSITE PRIMARY KEY (used for add operation)
    $grid->setColProperty("user", array("hidden"=>true,"editoptions"=>array("value"=> $user),"editrules"=>array("required"=>true)));
    $grid->setColProperty("pwd", array("hidden"=>true,"editoptions"=>array("value"=> $pwd),"editrules"=>array("required"=>true)));
    $grid->setColProperty("subject", array("hidden"=>true,"editoptions"=>array("value"=> $subject),"editrules"=>array("required"=>true)));
    $grid->setColProperty("time", array("hidden"=>true,"editoptions"=>array("value"=> $time),"editrules"=>array("required"=>true)));
    $grid->setColProperty("recipient", array("hidden"=>true,"editoptions"=>array("value"=> $recipient),"editrules"=>array("required"=>true)));
    
    // for the message
    $grid->setColProperty("message", array("classes"=>"multiligne","label"=>"Message","width"=>400,"edittype"=>"textarea","editrules"=>array("required"=>true)));
    
    // Set a new user datetime format using PHP convensions (for the php time variable)
    $grid->setUserTime('Y-m-d H:i:s');
    …
    

    这不是全合一"的方法,但是效果很好!

    It's not the "All in One" approach, but it works well !!

    如果有人看到需要优化的内容,请告诉我!!!

    If somebody see something to optimize, please tell me back !!!

    此致

    素质

    推荐答案

    首先,在您发布的colModel的定义中有些奇怪.例如,像colModel":[这样的文本肯定是错误的.如果代码包含很多错误(可能是格式错误,但是读者不知道您的原始代码),那么很难对代码说些什么.如果您在格式化数据时遇到问题,只需张贴您所拥有的内容,其他人就可以重新格式化您的问题.

    first of all it is something strange in the definition of colModel which you post. For example, the text like colModel":[ is definitively wrong. It is difficult to say something about the code if it contain a lot of errors (probably errors of formating, but a reader don't know your original code). If you have problems to format the data, just post what you have and other people will be able reformat your question.

    现在关于您的主要问题. key:true在一行中多次使用是错误的方式.在 http://www.trirand.com/jqgridwiki/doku.php上吗?您可以在key参数的说明中找到id = wiki:colmodel_options

    Now about your main question. Usage of key:true in more as one row is wrong way. On http://www.trirand.com/jqgridwiki/doku.php?id=wiki:colmodel_options you can find in the description of key parameter following:

    如果服务器没有ID, 可以将其设置为id 唯一的行ID.只有一栏可以 具有此属性.如果还有更多 网格找到第一个键 一个和第二个被忽略.

    In case if there is no id from server, this can be set as as id for the unique row id. Only one column can have this property. If there are more than one key the grid finds the first one and the second is ignored.

    jqGrid需要有一个ID,以区分网格的另一行.您可以用任何1,2,3等ID(不是您的真实ID)填充jqGrid.如果所有包含组成复合主键的信息的列都在colModel中具有选项editable:true,则这些列中的值将在所有编辑操作中发送到服务器,并且您将能够在自己的表中构建相应的SELECT语句. mygrid_crud.php文件.

    jqGrid need have an id to distinguish one row of grid from another one. You can fill jqGrid with any ids like 1,2,3 etc which are not your real ids. If all columnt which have information which compose a composite primary key have option editable:true in colModel then the values from these columns will be send to the server at all edit operation and you will be able to build the corresponding SELECT statement in your mygrid_crud.php file.

    如果不想显示仅需要构建组合键的某些列,则可以在colModel

    If you want not display some columns which you need only to build the composite key you can use for this columns following options in the colModel

    hidden: true, editable: true, editrules: { edithidden: false }, hidedlg: true
    

    这将使用户看不到列,但在所有编辑操作中,数据将被发送到服务器.

    It will make columns invisible for user, but the data will be send to server at all edit operations.

    再说一句结束.不要在中设置默认值.例如,{name:"index", index:"index", label:"index"}的所有三个字段都相同.因此,您可以将所有内容减少为{name:"index"}

    One more small remarks an the end. Don't set default values in the . For example {name:"index", index:"index", label:"index"} has all three fields the same. So you can reduce all to {name:"index"}

    这篇关于jqGrid:“多合一";接近宽度jqGridEdit类&gt;如何设置复合主键?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆