2011-05-18 2 views

risposta

15

Hai la risposta? Se no, qui è quello che appare nella jstree google groups

 
    function submitMe(){ 
     var checked_ids = []; 
     $("#server_tree").jstree("get_checked",null,true).each 
      (function() { 
       checked_ids.push(this.id); 
      }); 
      doStuff(checked_ids); 

3

La soluzione proposta da gruppi di Google, tuttavia, non ha funzionato per i nodi parzialmente controllato, nel mio caso. Ho dovuto lasciare get_checked e fare quanto segue per ottenere nodi completamente selezionati e parzialmente selezionati.

$(".sector-tree").find(".jstree-undetermined").each(function(i,element){ checked_ids.push($(element).attr("id")); if ($(this).find(".jstree-undetermined").length == 0) { $(this).find(".jstree-checked").each(function(i, element){ checked_ids.push({$(element).attr("id")); }); } }); // collect the rest of the checked nodes that exist under checked parents $(".sector-tree").find(".jstree-checked").each(function(i, element){ //also includes the ones below 'undetermined' parent var selectedElement = $(element).attr("id"); if (hasItem(selectedElement, checked_ids) < 0) { checked_ids.push(selectedElement); } });
3

Con jQuery si può semplicemente fare:

$('.jstree-checked,.jstree-undetermined').each(function(){ 
    var rawCheckedID = $(this).find('a').attr('id'); 
}); 

questo otterrà l'indeterminato e controllato allo stesso tempo. la soluzione di soumya sopra può essere più efficiente.

12

Ognuno, che ha lavorato con Jstree's, potrebbe affrontare questa domanda: come inviare gli ID di Jstree selezionati nel modulo? ecco la soluzione:

function submitMe() { 
    var checked_ids = []; 
    $('#your-tree-id').jstree("get_checked",null,true).each(function(){ 
     checked_ids.push(this.id); 
    }); 
    //setting to hidden field 
    document.getElementById('jsfields').value = checked_ids.join(","); 
} 

Ora, abbiamo impostato in un campo nascosto:

<input type="hidden" name="jsfields" id="jsfields" value="" /> 
+0

hi, così this.id si riferisce a ciò che exactely? e cosa succede se voglio ottenere il testo di un tag? Grazie. –

+2

Ho capito :) Si riferisce all'ID del tag li. –

+0

+1 per la comprensione :) –

0
//click button show nodes checked 
$(document).on('click','#showme',function() { 
    var checked_ids = []; 
    var checked_ids_meta = []; 
    $('#demo_0').jstree("get_checked",null,true).each(function(i, e){ 
     checked_ids.push($(this).data("param")); // json metadata 
     checked_ids_meta.push($(this).attr("href")); // json attr 
    });  
    console.log(checked_ids) 
    console.log(checked_ids_meta)  
}); 
23

Nell'ultima versione (3.0), l'API è stato cambiato.

Se è necessario semplicemente array di identificazioni selezionate (come negli esempi in questo nodo), ora è molto semplice:

var selectedElmsIds = $('#tree').jstree("get_selected"); 

Se è necessario iterare elementi selezionati, basta passare ulteriore parametro "true".

var selectedElmsIds = []; 
var selectedElms = $('#tree').jstree("get_selected", true); 
$.each(selectedElms, function() { 
    selectedElmsIds.push(this.id); 
}); 
+0

Per me, in 3.3.4 avevo bisogno di usare get_checked, non get_selected. – chip

0

var selectedElmsIds = []; 
 
$("#jstree2").find("li").each(function(i, element) { //also includes the ones below 'undetermined' parent 
 
    if ($(this).attr("aria-selected") == "true") { 
 
    selectedElmsIds.push($(this).attr('id')); 
 
    } 
 
}); 
 
console.log(selectedElmsIds);

0

Questo ho fatto:

function getSelectedItems() 
{ 
    var checked_ids = []; 

    checkedNodes = $("#MyTree").jstree("get_checked", null, true); 

    for(var i = 0; i < checkedNodes.length; i++) 
    { 
     var id = $(checkedNodes[i].outerHTML)[0].id; 

     checked_ids.push(id); 
    } 

    // Do whatever you want with the checked_ids 
} 

Questo vi darà un array di tutti nodo selezionato e loro nodi secondari e foglie; così come le singole foglie selezionate sotto altri nodi.

0
$(document).ready(function(){ 
var jsfields = $('#myTree').jstree('get_selected'); 
$('.jsfields').val(JSON.stringify([jsfields])); 
}) 

<input type="hidden" class="jsfields" value=""/> 

Modificare il valore di $('#myTree') a voi rispettivo albero, questa è la cosa migliore che lavora per me in chiamata AJAX. potrebbe essere necessaria una leggera modifica per popolare il campo di input di forma semplice.