数据表编辑器 - 如何在编辑器表单中动态填充选择列表?

Posted

技术标签:

【中文标题】数据表编辑器 - 如何在编辑器表单中动态填充选择列表?【英文标题】:Datatables Editor - How to dynamically populate select list in Editor form? 【发布时间】:2021-05-20 03:01:17 【问题描述】:

我正在尝试将动态选择列表添加到Datatables Editor 表单。这是我尝试过的:

var discipline_options = [];
  
 $.getJSON('program_data/get_disciplines.php', function (data) 
     $.each(data, function (index) 
         discipline_options.push(
             value: data[index].value,
             label: data[index].text
         );
     );
 editor.field( 'discipline_outcome.discipline_fk' ).update(discipline_options);
 );
 
            var editor = new $.fn.dataTable.Editor( 
                ajax: "program_data/discipline_outcome_data.php",
                table: "#discipline_outcome_table",
                template: '#discipline_outcome_form',
                fields: [  
                    label: "Discipline:",
                    name: "discipline_outcome.discipline_fk",
                    type: "select",
                    placeholder: 'Choose discipline...',
                    placeholderDisabled: false,
                    placeholderValue: 0,
                    options: []
                ,...

get_disciplines.php 脚本是:

$data = array();

$query = "SELECT * FROM discipline";
$result = $connection->query( $query );

while ($row = mysqli_fetch_array($result)) 
    $data[] = array("label"=>$row['discipline'], "value"=>$row['discipline_pk']);


$temp = array('disciplines[].discipline_pk'=>$data);
$json = array('options'=>$temp);
echo json_encode($json);

此脚本返回以下 JSON,但选择列表仍为空:

        
      "options": 
        "disciplines[].discipline_pk": [
          
            "label": "Emergency Medicine",
            "value": "1"
          ,
          
            "label": "General Practice",
            "value": "2"
          ,
          
            "label": "Internal Medicine",
            "value": "3"
          
        ]
      
    

【问题讨论】:

【参考方案1】:

我让它工作了:

var discipline_options = [];
             
$.getJSON("program_data/get_disciplines.php", function(data) 
        var option = ;
        $.each(data, function(i,e) 
            option.label = e.text;
            option.value = e.id;
            discipline_options.push(option);
            option = ;
        );
    
).done(function() 
    editor.field('discipline.discipline_pk').update(discipline_options);
);

var editor = new $.fn.dataTable.Editor( 
                ajax: "program_data/discipline_outcome_data.php",
                table: "#discipline_outcome_table",
                template: '#discipline_outcome_form',
                fields: [  
                    label: "Discipline:",
                    name: "discipline.discipline_pk",
                    type: "select",
                    placeholder: 'Choose discipline...',
                    placeholderDisabled: false,
                    placeholderValue: 0,
                    options: []
                ,...

还有get_disciplines.php:

$data = array();
 
$query = "SELECT * FROM discipline";
$result = $connection->query( $query );
 
while ($row = mysqli_fetch_array($result)) 
    $data[] = array("text"=>$row['discipline'], "id"=>$row['discipline_pk']);

 
echo json_encode($data);

【讨论】:

以上是关于数据表编辑器 - 如何在编辑器表单中动态填充选择列表?的主要内容,如果未能解决你的问题,请参考以下文章

如何在多个文件输入中动态填充文件

如何使用用户的旧信息自动填充编辑表单?

如何使用来自两个不同表的旧信息自动填充编辑表单?

ExtJS 将事件处理程序动态添加到列编辑器

如何在 SwiftUI 中自动填充文本字段?

自动填充 iframe 中的外部表单来自...另一个表单