javascript - jquery jtable 多选下拉菜单

标签 javascript codeigniter jquery-jtable

我一直致力于使用 jquery jtable 创建多选下拉列表。到目前为止this issue很有帮助。下拉列表允许用户选择多个选项并将表单数据发送到服务器。问题是:尽管所有选项都已发送,但仅保存下拉列表中最后选择的选项。最后评论that issue提到这个问题,但建议的修复对我不起作用,并且我收到服务器错误。

//js
LessonsLearnedFields = {
//Other fields left out for brevity
risk_id: {
    key: true,
    list: false
},
cause: {
    title: "Cause",
    type: "multiselectddl",
    width: '70%',
    options: {
         empty: 'empty',
         yes: 'Yes',
         no: 'No'
     }
}
};

function loadViewLessonsLearnedTable(ContainerID, project_id, modify) {
var fields = $.extend(true, fields, LessonsLearnedFields); //copy, don't reference
fields.Responses = getLessonsLearnedResponseChildTable(ContainerID);
$('#' + ContainerID).jtable({
    title: 'Lessons Learned',
    paging: true,
    pageSize: 100,
    sorting: true,
    defaultSorting: 'WBS ASC',
    actions: {
        listAction: config.base_url + 'data_fetch/risks_cause_by_project/' + project_id,
        deleteAction: config.base_url + 'data_fetch/delete_risk/',
        updateAction: config.base_url + 'data_fetch/update_lessons_learned_risk/' + project_id 
    },
    messages: defaultRiskMessages,
    fields: LessonsLearnedFields
    // ,
    // formSubmitting: function(event,data){
    //     $('select[name=cause]', data.form).attr('name','cause[]');
    //         return data;
    //     }
});
$('#' + ContainerID).jtable('load');
}
<小时/>
//Data_Fetch - php
public function update_lessons_learned_risk($project_id, $offset = 0, $limit = 100, $order_by = 'risk_id', $direction = 'ASC') {
    $confirm_member = $this->User_model->confirm_member(true, false);
    if (!$confirm_member['success']) {
        $this->print_jtable_error(self::ERROR_NOT_LOGGED_IN);
        return;
    }
    $risk_id = $this->input->post('risk_id');
    $user_id = $_SESSION['user_id'];
    $this->load->model('Risk_model');
    $permission = $this->Risk_model->initialize($risk_id, $user_id);
    if ($permission != "Admin" && $permission != "Owner" && $permission != "Write") {
        $this->print_jtable_error(self::ERROR_NO_EDIT_PERMISSION);
        return;
    }
    $data['event'] = $this->input->post('event');
    $data['date_closed'] = $this->input->post('date_closed');
    $data['probability'] = $this->input->post('probability');
    $data['cause'] = $this->input->post('cause');
    $data['occurred'] = $this->input->post('occurred');
    $this->load->helper('security');
    foreach ($data as &$val) {
        xss_clean($val);
    }
    if ($this->Risk_model->update_lessons_learned($data) == false) {
        $this->print_jtable_error(self::ERROR_UNKNOWN);
        return;
    } else {
        print json_encode(array('Result' => "OK"));
        return true;
    }
}
<小时/>
//Risk Model - php
public function update_lessons_learned(array $data) {
    if (!$this->initialized)
        return false;
    if (!$this->Project_model->modify($this->user_id, $this->project_id))
        return false;
    //remove excess data from array
    $keys = array('event', 'date_closed',
        'probability', 'cause', 'occurred');
    foreach ($data as $key => $val)
        if (!in_array($key, $keys))
            unset($data[$key]);
    $data['date_of_update'] = date('Y-m-d');
    if (isset($data['date_closed']) && $data['date_closed'] == "0000-00-00")
        unset($data['date_closed']);
    //probablity is set to 100 if risk occurred  
    if (isset($data['occurred']) && $data['occurred'] === 'yes')
        $data['probability'] = 100;
    //probablity is set to 100 if risk occurred
    if (isset($data['occurred']) && $data['occurred'] === 'no')
        $data['probability'] = 0;

    if (!$this->db->where('risk_id', $this->risk_id)->update('risks', $data))
        return false;
    $this->update_priority_effect();
    $this->update_priority_monetary();
    return true;
}

评论中的图片。

非常感谢所有帮助。

最佳答案

我在这一行中缺少[] $data['cause'] = $this->input->post('cause'); 添加缺少的大括号后,如下所示: $data['cause'] = $this->input->post('cause[]'); 我将值转换为字符串,然后存储它。

 $cause_str = implode(",", $this->input->post('cause')); 
        $data['cause'] = $cause_str;

formSubmitting 钩子(Hook)告诉 PHP 后端需要一个数组,这就是它的必要性。

更新-->

我想在使用多选下拉菜单时改进用户体验,因此我实现了一个多选下拉菜单,其中项目旁边有复选框。 I found a great example which helped me a great deal 。这是根据 jquery jtable

的项目类型创建输入的更新方法
_createDropDownListMultiForField: function (field, fieldName, value, record) {
            //Create a container div
                // var $containerDiv = $('<div class="jtable-input jtable-multi-dropdown-input"></div>');
            var $containerDiv = $('<div class="jtable-input jtable-multi-dropdown-input" width="20px;"></div>');

            var $script = $('<script src="/assets/js/prep.js"></script>').appendTo($containerDiv);
            var $style = $('<link rel="stylesheet" type="text/css" href="assets/css/prep.css">').appendTo($containerDiv);

            //Create multi-select element
                // var $select = 
                // $('<select multiple="multiple" class="' + field.inputClass + '" id="Edit-' + fieldName + '" name=' + fieldName + '></select>').appendTo($containerDiv);
            var $dl = $('<dl class="dropdown ' + field.inputClass + '" id="Edit-' + fieldName + '" name=' + fieldName + '></dl>').appendTo($containerDiv);

            var $dt = $('<dt></dt>').appendTo($dl);
            var $a = $('<a href="#"></a>').appendTo($dt);
            var $span = $('<span class="hida">Cause Selector</span>').appendTo($a);
            var $p = $('<p class="multiSel"></p>').appendTo($a);

            var $dd = $('<dd></dd>').appendTo($dl);
            var $multiSelectDiv = $('<div class="mutliSelect"></div>').appendTo($dd);
            var $ul = $('<ul class="dropdown ' + field.inputClass + '" id="Edit-' + fieldName + '" name=' + fieldName + '></ul>').appendTo($multiSelectDiv);

            //Multi
            var options = this._getOptionsForField(fieldName,{
                    record: record,
                    value: value,
                    source: 'list',
                    dependedValues: this._createDependedValuesUsingRecord(record, field.dependsOn)
            });
            //Multi
            if (value) {
                console.log("Value: " + value);
                if (typeof value === 'string') {
                    var values = value.split(',');
                } else { //(Object.prototype.toString.call(value) === '[object Array]') {
                    values = value;
                }

                //add options
                $.each(options, function(index, element) {
                    // $select.append('<option value="' + element.Value + '"' + (element.Value == element.Out ? ' selected="selected"' : '') + '>' + element.DisplayText + '</option>');
                        // $select.append('<option value="' + element.Value + '"' + '>' + element.DisplayText + '</option>');
                    $ul.append('<li><input class="' + field.inputClass + '" id="Edit-' + fieldName + '" name="' + fieldName + '[]" type="checkbox" value="' + element.Value + '"' + '>' + element.DisplayText + '</li>');
                });
                // $.each(values, function(index, element) {
                //     $select.children('option[value="' + element + '"]').attr("selected", "selected");
                // });
            } else {
                $.each(options, function (index, element) {
                        // $select.append('<option id="what?" value="' + element.Value + '">' + element.DisplayText + '</option>');
                    $ul.append('<li><input id="what?" class="' + field.inputClass + '" id="Edit-' + fieldName + '" name=' + fieldName + '[] type="checkbox" value="' + element.Value + '"' + '>' + element.DisplayText + '</li>');    

                });
            }

            return $containerDiv;
        }

perp.jsprep.css 是该 codepen exmaple 中文件的修改版本。

关于javascript - jquery jtable 多选下拉菜单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26393160/

相关文章:

javascript - 仅在浏览器不是 Google Chrome 时显示消息

javascript - 使用 includes 过滤数组

php - Codeigniter 在 mysql 中插入 true 作为 0

mysql - jTable 在使用 node.js 进行 MySQL INSERT 后不显示新行内容

php - JTable PHP 更新不起作用

javascript - 在 jquery jtable 中启用/禁用 'add new' 按钮

javascript - Google map fitbounds 错误(无法读取属性 e3)

javascript - 如果每个字符串包含 jQuery 中的特定字符,则打印每个字符串?

jquery - 在codeigniter中使用jquery ajax更新数据库

php - 在 codeigniter 中加载模型的最佳方法是什么?