2014-10-03 63 views
0

我实现表格为http://www.datatables.net/examples/api/form.html,但在javascript中,我无法从“mRender”中的viewmodel(“vm”)数据绑定值。测试渲染功能正常工作。如何在javascript代码中实现与viewmodel的绑定?DataTables mRender功能与查看模型

function CreateNewDllTable(url, vm) { 
    var oTable = $('#test-table').dataTable({ 
      "bProcessing": true, 
      "bServerSide": true, 
      "bPaginate": false, 
      "scrollY": "250px", 
      "bSort": false, 
      "bFilter": false, 
      "bInfo": false, 
      "sAjaxDataProp": "dataValues", 
      "sAjaxSource": url 
      }, 
      "bAutoWidth": false, 
      "aoColumns": [ 
      { "mData": "name" }, 
      {       
       type: 'text', 
       "mData": "description", 
       "mRender": function (data) { 
        if (data == true) { 
         return '<input type="text"/>'; 
        } else { 
         return '<input type="text"/>'; 
        } 
       } 
      }, 
      { 
       "mRender": function (data) { 
        return "<select class='multiselect'>" 
           + "<option>Value1</option>" 
           + "<option>Value2</option>" 
           + "<option>Value3</option>" 
           + "<option>Value4</option>" 
           + "<option>Value5</option>" 
          + "</select>"; 
       } 
      } 
      ], 
      "fnDrawCallback": function() { 
       $("select.multiselect").multiselect();   
      }, 
    } 

例如在HTML结合:

<select class="multiselect" data-bind=" 
     options: vm.types, 
     value: vm.selectedTypeId, 
     optionsText:'type', 
     optionsValue: 'typeId'"> 
    </select> 

回答

0
 For example you can used this code: 

     "mRender": function() { 
var returnValue = "<select class='multiselect'>"; 
var types = vm.types; 
var listItems = ""; 
for (var i = 0; i < types.length; i++) {           
     listItems += "<option value='" + types[i].typeId + "'" + " selected='selected'" + ">" + types[i].type + "</option>";           
} 
return returnValue.concat(listItems, "</select>");  
}