create a dynamic array with the $ .map function in jquery

1

I have attached my code.

What I have in my grid an order column, this column has a combobox ("none, 1,2,3,4"), every time I select a combobox repeated ie with the same value I will launch a message that that order has already been selected, for it is stored in an array to find the repeated elements, then the second case is when my combobox is with a value already selected and I want to edit it.

Example: combobox with value "2" and I want to change it to value "4", the previous value 2 will be replaced by the new value "4", and all that change will be reflected in the array, as long as the value has not been repeated, and once the array is ready it will be shown in the textbox that is in the bottom, well thank you that is my query.

  • in the combobox should appear by default "none", but ignores me.

 jQuery(document).ready(function () {

            var myarray = [];


            var countries = { '0': 'Ninguno', '1': '1', '2': '2', '3': '3', '4': '4' };
            var states = { '1': 'Alabama', '2': 'California', '3': 'Florida', '4': 'Hawaii', '5': 'London', '6': 'Oxford' };
            var statesOfCountry = {
                1: { '1': 'Alabama', '2': 'California', '3': 'Florida', '4': 'Hawaii' },
                2: { '5': 'London', '6': 'Oxford' }
            };
            var mydata = [
                { id: '0', orden: '1', State: '1', Name: "Louise Fletcher" },
                { id: '1', orden: '1', State: '3', Name: "Jim Morrison" },
                { id: '2', orden: '2', State: '5', Name: "Sherlock Holmes" },
                { id: '3', orden: '2', State: '6', Name: "Oscar Wilde" }
            ];

            var grid = jQuery("#list");
          
            grid.jqGrid({
                data: mydata,
                datatype: 'local',
                colModel: [
                    { name: 'Name', width: 800 },
                    {
                        name: 'orden', width: 80, editable: true,
                        edittype: 'select',
                        editoptions: {
                            value: countries
                            , defaultValue: "0",
                            dataEvents: [
                                {
                                    type: 'change',
                                    fn: function (e, rowid) {

                                        var row = $(e.target).closest("tr.jqgrow");
                                        /*var rowid = row.attr('title');*/

                                        var rowcodigo = row.attr('id');

                                        var v = parseInt($(e.target).val());
                                    
                                        myarray.push(v);

                                        validateArray = getDistinctArray(myarray);

                                        document.getElementById("num_orden").value = validateArray;

                                        function getDistinctArray(arr) {
                                            var compareArray = new Array();


                                            if (arr.length >= 1) {
                                                for (i = 0; i < arr.length; i++) {
                                   
                                                    if (compareArray.indexOf(arr[i]) == -1) {
                                              
                                                        compareArray.push(arr[i]);
                                                    } else {


                                                   
                                                        alert("el numero orden " + arr[i] + " para noticias en español ya existe");
                                                        //elimina el numero orden existente en el array
                                                        arr.splice(arr[i]);
                                                 
                                                        $("select#" + rowcodigo + "_orden").prop('selectedIndex', 0);



                                                    }
                                                }
                                            }
                                            return compareArray;
                                        }


                                        var valor_actual = $("select#" + rowcodigo + "_orden").val();
                                        alert(valor_actual);

                                    }
                                }
                            ]
                        },

                    },
                  
                ],
        
                ondblClickRow: function (id, ri, ci) {
                 
                  grid.editRow(id, true, null, null, 'clientArray', null,
                                 function (rowid, response) {  // aftersavefunc
                                    // grid.setColProp('State', { editoptions: { value: states } });
                                 });
                  //  return;
                },
                editurl: 'clientArray',
                sortname: 'Name',
                height: '100%',
                viewrecords: true,
                rownumbers: true,
                sortorder: "desc",
                pager: '#pager',
                caption: "Demonstrate dependend select/dropdown lists (edit on double-click)"
            }); //.navGrid('#pager', { edit: false, add: false, del: false, search: false, refresh: false });
        });
<script src="//cdn.uedsc.com/jquery/1.8.0/jquery-1.8.0.js"></script>
<link href="//cdn.uedsc.com/jqgrid/4.6.0/css/ui.jqgrid.css" rel="stylesheet"/>
<script src="//cdn.uedsc.com/jqgrid/4.6.0/js/jquery.jqGrid.min.js"></script>

<table id="list"></table>
        <input type="text" id="num_orden" value=""/>
    
asked by elizabeth 19.04.2016 в 20:16
source

0 answers