datatables add combobox as a column

与世无争的帅哥 提交于 2019-12-06 02:23:05

This is the solution I used for you guys who experienced the same problem. Creates comboboxes and adds to column 12..Regards.. Ozlem.

        function init(){

            //http://stackoverflow.com/questions/2759837/adding-dropdown-list-to-the-particular-column-using-jquery

            var ind = 0;
            var year    = 2010;
            //var options = getYears(year, 3);
            $.each($('#rulesTable td:nth-child(12)'), function () {

                //creates a combobox
                var select  = document.createElement('select');
                select.setAttribute('class','year');
                select.setAttribute('name',ind+''); 
                select.setAttribute('id','comboYear'+ind); 
                select.innerHTML = '<option value=2010>2010</option><option value=2011>2011</option><option value=2012>2012</option>'; 

                /*for (var i= 0 ; i<options.length; i++){
                    var nextOption = options[i];                        
                    select.appendChild(nextOption);
                }*/
                $(this).append(select);
                $('#comboYear'+ind).change(function () {

                    var comboId = $(this).attr('id');
                    var comboIndex = $(this).attr('name');
                    var yearSelected = $('#'+comboId+' option:selected').text();
                    var propertyId = rulesTableGlobal.fnGetData()[comboIndex][0];
                    //alert(text);
                    upDateRow(propertyId, yearSelected, comboIndex );

                });

                year = year+1;
                ind++;                  

            });

        }

Define your combo box in your HTML like this:

<select id="year">
    <option value="2010">2010</option>
    <option value="2011">2011</option>
    <option value="2012">2012</option>
</select>

Whenever you need the actual selected value of the combobox, you can call

$("#year").val()

It's good practice not to repeat jQuery DOM lookups, so you might want to do it like this:

// Define this on document ready
var selectYear = $("#year");

// Use this syntax to get the current value
selectYear.val()

I cannot see from your example code where you would use this (perhaps in the line var year = "";, if so change that to var year = selectYear.val();), but I hope this gets you on your way. If not, feel free to ask for more information in the comments.

EDIT: as requested a sample to show the year select in a normal table.

<table>
    <tr>
        <td>a cell</td>
        <td>
            <select id="year">
                <option value="2010">2010</option>
                <option value="2011">2011</option>
                <option value="2012">2012</option>
            </select>
        </td>
    </tr>
</table>

use fnRender-

$('#example').dataTable( {
    aoColumns : [ {
        fnRender : function(oObj) {
            return '<select id="year">' + '<option value="2010">2010</option>' + '<option value="2011">2011</option>' + '<option value="2012">2012</option>' + '</select> ';
        }
    } ]
});
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!