【问题标题】:SyntaxError: missing ) after formal parameters when adding if/elseSyntaxError: missing ) 在添加 if/else 时形式参数后
【发布时间】:2018-02-19 11:46:00
【问题描述】:

谁能告诉我我做错了什么,我想在handsontable中为不同的用户提供不同的视图。我将表格定义为

document.addEventListener("DOMContentLoaded", function() {
  var create_table = document.getElementById('create_table');
  var mydata = document.getElementById('mydata').innerHTML;//to get the hidden element
  var logged_user = document.getElementById('logged_user').innerHTML;// to get remote user
  var plan_creator = document.getElementById('plan_creator').innerHTML;//to get the person who has created the plan
  console.log(logged_user  + "  " + plan_creator);
  console.log(mydata);
  var searchResultCount=0;
  var hot,searchFiled,resultCount;
function searchResultCounter(instance, row, col, value, result) {
    Handsontable.plugins.Search.DEFAULT_CALLBACK.apply(this, arguments);

    if (result) {
      searchResultCount++;
    }
}
var buttons = {
    save:document.getElementById('save'),
    load:document.getElementById('load'),
    file:document.getElementById('file_export')
    }
    var objectData = JSON.parse(mydata);//to decode data in JSON format
    console.log(objectData);
    hot = new Handsontable(create_table, {
    data:objectData,
    colHeaders: true,
    rowHeaders: true,
    contextMenu: true,
    minRows: 30,
    minCols: 13,
    maxCols:18,
    maxRows:100,
    copyPaste:true,
    dropdownMenu: true,//plugin to display menu on column header
    filters:true,
    columnSorting:true,//plugin to enable sorting
    sortIndicator:true,//to display sorting is done
    comments:true,//to add comments
    colHeaders:["Testcase Name","Cell Name","Customer","Flops","Title","Status","Mfix CCR","Scenerio Brief Description","Expected Results","CCR Status","CCR No","Remarks","Testcase Path"],
if(logged_user == plan_creator) {
    columns:[//when using this not able to remove column
              {data:'tc_name'},
              {data:'cell_name'},
              {data:'customer_name'},
              {data:'flops' ,type:'numeric'},
              {data:'title'},
              {data:'status'},
              {data:'mfix_ccr'},
              {data:'test_scenerio'},
              {data:'expected_results'},
              {data:'ccr_status'},
              {data:'ccr_num'},
              {data:'remarks'},
              {data:'tc_path'}],
    }
    else{
    columns:[//when using this not able to remove column
              {data:'tc_name' ,readOnly:true } ,
              {data:'cell_name',readOnly:true },
              {data:'customer_name',readOnly:true },
              {data:'flops' ,type:'numeric',readOnly:true },
              {data:'title',readOnly:true },
              {data:'status',readOnly:true },
              {data:'mfix_ccr',readOnly:true },
              {data:'test_scenerio',readOnly:true },
              {data:'expected_results',readOnly:true },
              {data:'ccr_status',readOnly:true },
              {data:'ccr_num',readOnly:true },
              {data:'remarks'},//only remarks can be added by other user
              {data:'tc_path',readOnly:true }],
    }
    search: {
     callbak:searchResultCounter
    }
});
   searchFiled = document.getElementById('search_filed');
   resultCount=document.getElementById('resultCount');
   var exportPlugin=hot.getPlugin('exportFile');
   Handsontable.dom.addEvent(searchFiled, 'keyup', function(event) {
    var queryResult;
    console.log(this.value);
    searchResultCount = 0;
    queryResult = hot.search.query(this.value);
    console.log(queryResult);
    //resultCount.innerText = searchResultCount.toString();
    hot.render();
});
buttons.file.addEventListener('click', function() {// enabling the plugin to download the file
    exportPlugin.downloadFile('csv', {filename: 'MyFile',columnHeaders:true});
  });

当我删除 if/else 语句并仅使用一个场景时,我没有收到任何错误。使用上面的代码时,我收到了错误,但是当我删除 if/else 部分并仅使用 columns 属性时简单的方法,我没有收到这个错误。但是我想对计划的创建者和其他人有不同的看法。

还有其他方法吗?

谢谢

【问题讨论】:

标签: javascript jquery handsontable


【解决方案1】:

构造对象时不能使用if语句,但可以使用三元?:运算符,如下所示:

colHeaders: ... ,
columns: logged_user == plan_creator
    ? /* value in case they are equal */
    : /* value in case they are not equal */,
search: ...

【讨论】:

    【解决方案2】:

    而不是使用 if else like

    if(logged_user == plan_creator) {
    columns:[//when using this not able to remove column
      {data:'tc_name'},
      {data:'cell_name'},
      {data:'customer_name'},
      {data:'flops' ,type:'numeric'},
      {data:'title'},
      {data:'status'},
      {data:'mfix_ccr'},
      {data:'test_scenerio'},
      {data:'expected_results'},
      {data:'ccr_status'},
      {data:'ccr_num'},
      {data:'remarks'},
      {data:'tc_path'}],
    }
    

    其他{}

    你可以使用三元运算符:

    columns: (logged_user === plan_creator)
      ? [//when using this not able to remove column
          {data:'tc_name'},
          ...
        ] 
      : [//when using this not able to remove column
          {data:'tc_name' ,readOnly:true },
          ... 
        ]
    

    【讨论】:

      猜你喜欢
      • 2014-02-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-11
      • 2018-06-15
      • 1970-01-01
      • 2017-06-02
      • 2020-04-22
      相关资源
      最近更新 更多