0

edit/operacoes.php

header('Content-Type: application/json');
include('../../lib/mysql.class.php');

$db = new MySQL();
if($db->Error()) $db->Kill();

//VARIÁVEIS DA FORM
$id = intval($_POST['id']);

$sql = "SELECT * FROM operacoes WHERE id=$id";
if(!$db->Query($sql)) $db->Kill();

$db->MoveFirst();
$row = $db->Row();
$data['nome'] = $row->nome;
$data['produto'] = $row->produto;
$data['componente'] = $row->componente;
$data['materiaprima'] = $row->materiaprima;

echo json_encode($data);

jQuery:

$.post('edit/operacoes.php',{id:id},function(data){
    $('div.form-container h1.title').text("Editar Existente");
    $('input#idbd').empty().val(id);
    $('input#nome').empty().val(data.nome);
    $('input#produto').attr('checked', (data.produto==1)?true:false);
    $('input#componente').attr('checked', (data.componente==1)?true:false);
    $('input#materiaprima').attr('checked', (data.materiaprima==1)?true:false);
    $('div#addbuttons').css('display','none');
    $('div#editbuttons').css('display','block');
    showPopup();
});

HTML:

<tr>
    <td colspan="3" class="input listas">
        <select multiple="multiple" name="escolha_ferramentas" id="escolha_ferramentas" size="15" class="list">
            <option value="3">Ferramenta 3</option>
            <option value="4">Ferramenta 4</option>
            <option value="5">Ferramenta 5</option>
            <option value="6">Ferramenta 6</option>
        </select>
        <div class="list-controls">
            <input type="button" value=">" id="next-ferramentas"><br><br>
            <input type="button" value="<" id="prev-ferramentas">
        </div>
        <select multiple="multiple" name="ferramentas[]" id="ferramentas" size="15" class="list">
        </select>
        <div style="clear:both;"></div>
    </td>
</tr>

This works great, except for the selects.

The logic is, the ids that are in the BD (a different one), are the options that need to be in the right select and removed from the left select (append to the right select and remove from the left select).

My question is, how, using json, can I achieve this? Get the ids to the jquery and then loop them to move the options from one select to another?

Edit I created another php file just to give the ids I need. PHP returns ["2","3","4"]. How do I loop through that in jQuery.post function(data){loophere}

4

2 回答 2

1

我会从 PHP 传递一个字符串数组(json 看起来像['string','string','string'],然后在其上使用一个循环来指示将哪些字符串放在“另一边”。

我还没有看到你从服务器返回的那些,所以也许传递元素的语言翻译让我失望,但很明显你正在制作其中之一:

    A           B
[       ]   [       ]
[       ] > [       ]
[       ] < [       ]
[       ]   [       ]

只需将需要从 A 中删除的元素作为字符串数组传递到 B 中,然后循环遍历数组元素。

于 2012-06-19T16:12:48.413 回答
0

好吧,有几种选择可以实现您想要的。

在您的 ajax 调用中,我会将左侧选择中的选定 ID 和右侧选择中的 id 数组传递给 php,然后在返回 php 时,您将获得一个对象,其中包含要在左侧选择中填充的数据正确的选择再次首先清空它们,因此您永远不会感到困惑,并且可以控制每个选择上的元素。

于 2012-06-19T16:25:15.897 回答