X-Editable JQuery Plugin - Select Get Source Object
Asked Answered
S

1

9

I am having issues trying to get the selected object rather than the "newValue" that is passed to the success callback.

Here is an example:

$("select").editable({
        type : "select",
        title: 'Select Fruit',
        source : [
            {text : "Apple", value: "option_1"},
            {text : "Orange", value: "option_2"},
            {text : "Mango",value: "option_3"},
            {text : "Strawberry",value: "option_4"}
        ],
        success:function(response,newValue){
            console.log(newValue); // newValue contains the "text" string ok..
            // How do I get the selected source object? eg. {text : "Orange", value: "option_2"}
            // So I can access the object like..
            console.log(newValue.value); // output option_*
        }
    });

Thanks Carl

Synchronism answered 19/7, 2017 at 10:37 Comment(3)
Did you see console.log(response) ?Undertaker
Yes I have tried that, that is the response from the Ajax request (which I am not using)Synchronism
@CarlSmith can make a fiddle for this ?Phosphoprotein
K
5

You can use the display callback to access value, or even the entire selected object:

<a href="#" id="status" data-type="select" data-pk="1" data-title="Select status"></a>    
<script>
  $(function() {

    $("#status").editable({
      type: "select",
      title: 'Select Fruit',
      source: [
        {text : "Apple", value: "option_1"},
        {text : "Orange", value: "option_2"},
        {text : "Mango",value: "option_3"},
        {text : "Strawberry",value: "option_4"}
      ],
      display: function(value, sourceData) {

        if (value) { // value = "option_3" etc.
          $(this).html(value);
        }

        /* OR if you want to access the selected source object  ...

        var selected = $.fn.editableutils.itemsByValue(value, sourceData); 
        if (selected.length) {
           $(this).html(selected[0].value);
        }  */
      }
    });
  });
</script>

Demo: http://jsfiddle.net/6vzrug72/

Kletter answered 21/7, 2017 at 23:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.