2011-08-15 32 views
1

我有一些问题试图从Mysql查询中填充editform中的select。我正在阅读一些博客,但我无法找到解决方案。下面我附上一些代码。Jqgrid从MySql查询填充选择

这是collmodel:

jQuery("#grid_pacientes").jqGrid({ 
    url:'conec_paciente.php', 
datatype: "json", 
//loadonce: true, 
    // 
    colNames:['id','Nombre','Apellido','Telefono','Obra Soc'], 
colModel:[ 
    {name:'id_paciente',index:'id_paciente', width:40, sorttype:'int'}, 
    {name:'nombre',index:'nombre', width:100,editable:true,editoptions:{size:10},resizable:false, sorttype:'text'}, 
    {name:'apellido',index:'apellido',width:100, editable: true,resizable:false, sorttype:'text'}, 
    {name:'telefono',index:'telefono',width:100, editable: true,resizable:false, sorttype:'int'}, 
    {name:'id_obra',index:'id_obra',width:100, editable: true,resizable:false, sorttype:'int',edittype:"select" 
      } 
     ], 

最后科尔(id_obra)是,我需要补一个。

这是PHP的页面,在这里我做MySQL查询:

<?php 
include_once 'lib.php'; 
$conexion= mysql_connect($dbhost, $dbuser, $dbpassword); 
mysql_select_db($database, $conexion); 
$result = mysql_query("SELECT id_obra, nombre 
         FROM obras", $conexion) or die(mysql_error()); 
$i=0; 
while($row = mysql_fetch_array($result,MYSQL_ASSOC)) { 
    $responce->rows[$i]['id']=$row["id_obra"]; 
    $responce->rows[$i]['cell']=array($row["id_obra"],$row["nombre"]); 
    $i++; 
} 
echo json_encode($responce); 
?> 

所以......这是所有的代码,我想我需要一个函数,但我不知道该怎么做。 在此先感谢,并通过对不起我的英语

回答

0

@nacho

如果要动态获取的下拉选项你必须为了让指定参数“editoptions-> dataInit”的jqGrid知道从哪里得到数据。

{ 
     name:'id_obra', 
     index:'id_obra', 
     width:100, 
     editable: true, 
     resizable:false, 
     sorttype:'int', 
     edittype:"select", 
     edioptions:{ 
      dataUrl: "http://url_where_data_will_be_gathered" 
     } 
} 

注意editoption对象的添加。

http://url_where_data_will_be_gathered URL返回的数据必须是格式:

<select> 
    <option value='1'>One</option> 
    <option value='2'>Two</option> 
</select> 

锄这个帮助你。

+0

您好,感谢您的帮助,但我想从一个MySQL查询填补选择。如果你检查我做了选择,但我不知道如何将这个查询结果传递给select。 – nacho

+0

@nacho:为了更好地理解你的问题,你能更清楚地知道你想完成什么?你想要的是从远程php(这是将执行mysql代码,你想要帮助)动态地填充选择并显示给用户? – diosney

+0

嗨diosney,再次感谢您的快速回复。这很简单,我想从具有mysql代码的php页面动态地填充选择。我做了你上面告诉我的,但这样select会显示staics值,我想用MySql查询中的字段填充它。 – nacho

2

感谢您的帮助。我找到了解决办法 这是我把你告诉我http://url_where_data_will_be_gathered代码:

<select> 

<?php 
include_once 'lib.php'; 
$conexion= mysql_connect($dbhost, $dbuser, $dbpassword); 
mysql_select_db($database, $conexion); 
$result = mysql_query("SELECT id_obra, nombre 
         FROM obras", $conexion) or die(mysql_error()); 
$i=0; 
while($row = mysql_fetch_array($result,MYSQL_ASSOC)) { 
?> 
<option value ="<?php echo $row['id_obra'] ?>"><?php echo $row['nombre'] ?></option> 
<?php } ?> 

</select> 
+0

嘿,解决方案在哪里? – s4suryapal